Android Open Source - android-chess Chess Image View






From Project

Back to project page android-chess.

License

The source code is released under:

MIT License

If you think the Android project android-chess listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package jwtc.android.chess;
/*  w w w.  ja  va  2s . c  o m*/
//import com.larvalabs.svgandroid.SVG;

import jwtc.chess.board.ChessBoard;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Picture;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class ChessImageView extends View {

  public static Bitmap[][] _arrPieceBitmaps = new Bitmap[2][6];
  public static Bitmap _bmpBorder, _bmpSelect, _bmpSelectLight;
  public static Bitmap _bmpTile;
  
  //public static SVG _svgTest = null;
  
  // 5 colorschemes with 2 colors each
  public static int[][] _arrColorScheme = new int[6][3];
  public static int _colorScheme = 0;
  //public static Bitmap _bmpBorder, _bmpSelect;
  //public static Bitmap[] _arrFieldBitmaps = new Bitmap[2];
  public static Paint _paint = new Paint();
  public static Matrix _matrix = null;
  public static Matrix _matrixTile = null;
  
  static {
    _paint.setFlags(Paint.ANTI_ALIAS_FLAG);
    _paint.setFilterBitmap(true);
  }
  
  private ImageCacheObject _ico;
  
  public ChessImageView(Context context) {
    super(context);
    setFocusable(true);
  }
  public ChessImageView(Context context, AttributeSet atts) {
    super(context, atts);
    setFocusable(true);
  }
  
  public void init(){
    
  }
  
  public void setICO(ImageCacheObject ico){
    _ico = ico;
  }
  public ImageCacheObject getICO(){
    return _ico;
  }
  
    public void onDraw(Canvas canvas) {
        
      if(_arrColorScheme[0][0] == 0){
        return;
      }
      
      if(_matrix == null){
      ChessImageView._matrix = new Matrix();
    
      float scale = 1.0F;
      Bitmap bmp = ChessImageView._arrPieceBitmaps[ChessBoard.WHITE][ChessBoard.PAWN]; // any dynamic
      
      scale = (float)getWidth() / bmp.getWidth();
      Log.i("paintBoard", "init " + scale + " : " + bmp.getWidth() + ", " + getWidth());
      
      ChessImageView._matrix.setScale(scale, scale);
      
      if(ChessImageView._bmpTile != null){
        ChessImageView._matrixTile = new Matrix();
        bmp = ChessImageView._bmpTile;
        scale = (float)getWidth() / bmp.getWidth();
        ChessImageView._matrixTile.setScale(scale, scale);
      }
    }
      
      Bitmap bmp;
        ImageCacheObject ico = _ico;
        
        // first draw field background
        if(ico == null)
          Log.e("err", "err");
        
        //_paint.setColor(Color.TRANSPARENT);
        if(hasFocus()){
          _paint.setColor(0xffff9900);
          canvas.drawRect(new Rect(0, 0, getWidth(), getHeight()), _paint);
        } else {
          _paint.setColor(ico._fieldColor == 0 ? _arrColorScheme[_colorScheme][0] : _arrColorScheme[_colorScheme][1]);
          canvas.drawRect(new Rect(0, 0, getWidth(), getHeight()), _paint);
          if(ico._selected){
            _paint.setColor(_arrColorScheme[_colorScheme][2]);
            canvas.drawRect(new Rect(0, 0, getWidth(), getHeight()), _paint);
          }
        }
        
        if(ChessImageView._bmpTile != null){
          canvas.drawBitmap(_bmpTile, _matrixTile, _paint);
        }

        
        //_paint.setColor(Color.BLACK);
        /*
        if(ico._selected || hasFocus()){
          canvas.drawBitmap(_bmpBorder, _matrix, _paint);
        }
        */
        
        if(ico._selectedPos){
          //if(ico._fieldColor == 0){ 
          //  canvas.drawBitmap(_bmpSelect, _matrix, _paint);
          //} else {
            canvas.drawBitmap(_bmpSelectLight, _matrix, _paint);
          //}
         }
        
        if(ico._bPiece){

          bmp = _arrPieceBitmaps[ico._color][ico._piece];
          
          canvas.drawBitmap(bmp, _matrix, _paint);
          
          //Picture picture = _svgTest.getPicture();
          //canvas.drawPicture(picture);
          
          //canvas.save();
          //picture.draw(canvas);
          //canvas.restore();
          //canvas.scale(2.0f, 2.0f);
          //canvas.save();
          
          
      }
        //////////////////////////////////////
        if(ico._coord != null){
          _paint.setColor(0x99ffffff);
          //if(ImageCacheObject._flippedBoard){
          //  canvas.drawRect(getWidth() - 14, getHeight() - 14, getWidth(), getHeight(), _paint);
          //} else {
            canvas.drawRect(0, getHeight() - 14,  _paint.measureText(ico._coord) + 4, getHeight(), _paint);
          //}
          _paint.setColor(Color.BLACK);
          
          _paint.setTextSize(getHeight() > 50 ? (int)(getHeight()/5) : 10);
          //if(ImageCacheObject._flippedBoard){
          //  canvas.drawText(ico._coord, getWidth() - 12, getHeight() - 2, _paint);
          //} else {
            canvas.drawText(ico._coord, 2, getHeight() - 2, _paint);
          //}
        }
        //////////////////////////////////////
    }
    
    @Override
    protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect){
      //Log.i("ChessImageView", "onFocusChanged");
      super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
    }

}




Java Source Code List

jwtc.android.chess.ChessFieldView.java
jwtc.android.chess.ChessImageView.java
jwtc.android.chess.ChessPreferences.java
jwtc.android.chess.ChessViewBase.java
jwtc.android.chess.ChessView.java
jwtc.android.chess.GamesListView.java
jwtc.android.chess.HtmlActivity.java
jwtc.android.chess.ImageCacheObject.java
jwtc.android.chess.MyBaseActivity.java
jwtc.android.chess.MyPGNProvider.java
jwtc.android.chess.PGNView.java
jwtc.android.chess.SaveGameDlg.java
jwtc.android.chess.UI.java
jwtc.android.chess.convergence.Connection.java
jwtc.android.chess.convergence.ConvergenceActivity.java
jwtc.android.chess.convergence.RestServer.java
jwtc.android.chess.iconifiedlist.IconifiedTextListAdapter.java
jwtc.android.chess.iconifiedlist.IconifiedTextView.java
jwtc.android.chess.iconifiedlist.IconifiedText.java
jwtc.android.chess.ics.CustomCommands.java
jwtc.android.chess.ics.ICSChatDlg.java
jwtc.android.chess.ics.ICSChessView.java
jwtc.android.chess.ics.ICSClient.java
jwtc.android.chess.ics.ICSConfirmDlg.java
jwtc.android.chess.ics.ICSMatchDlg.java
jwtc.android.chess.ics.TelnetSocket.java
jwtc.android.chess.ics.TimesealInputStream.java
jwtc.android.chess.ics.TimesealOutputStream.java
jwtc.android.chess.ics.TimesealPipe.java
jwtc.android.chess.ics.TimesealingSocket.java
jwtc.android.chess.puzzle.ChessViewPractice.java
jwtc.android.chess.puzzle.ChessViewPuzzle.java
jwtc.android.chess.puzzle.MyPuzzleProvider.java
jwtc.android.chess.puzzle.practice.java
jwtc.android.chess.puzzle.puzzle.java
jwtc.android.chess.tools.FileListView.java
jwtc.android.chess.tools.PGNProcessor.java
jwtc.android.chess.tools.importactivity.java
jwtc.android.chess.tools.pgntool.java
jwtc.android.chess.main.java
jwtc.android.chess.options.java
jwtc.android.chess.setup.java
jwtc.android.chess.start.java
jwtc.android.timeseal.TimesealingSocket.java
jwtc.android.timeseal.streams.a.java
jwtc.android.timeseal.streams.b.java
jwtc.android.timeseal.streams.c.java
jwtc.chess.ChessPuzzleProvider.java
jwtc.chess.GameControl.java
jwtc.chess.JNI.java
jwtc.chess.Move.java
jwtc.chess.PGNColumns.java
jwtc.chess.PGNEntry.java
jwtc.chess.PGNProvider.java
jwtc.chess.Pos.java
jwtc.chess.Valuation.java
jwtc.chess.algorithm.SearchAlgorithmRunner.java
jwtc.chess.algorithm.UCIWrapper.java
jwtc.chess.board.BoardConstants.java
jwtc.chess.board.BoardHashKeys.java
jwtc.chess.board.BoardMembers.java
jwtc.chess.board.BoardStatics.java
jwtc.chess.board.ChessBoard.java