2016-08-03 7 views
0

私はチェスアプリをやっています。ボードのタイルを描くと、(透明な)背景に色付けされません。基本的に私が望むのは、ImageViewで、背景色が透明な画像(透明な背景)を表示する場合と同様です。私はdrawBitmap call前に、これらの2行をコメントアウトした場合、私が手色付きの背景を持つキャンバスにビットマップを描画します。

これは、コード

private final Paint squareColor; 
private Rect tileRect; 
private Drawable pieceDrawable; 

public Tile(final int col, final int row) { 
    this.col = col; 
    this.row = row; 

    this.squareColor = new Paint(); 
    squareColor.setColor(isDark() ? Color.RED : Color.WHITE); 


} 

public void draw(final Canvas canvas) { 
    if(pieceDrawable != null) { 

     Bitmap image = ((BitmapDrawable) pieceDrawable).getBitmap(); 
     ColorFilter filter = new LightingColorFilter(squareColor.getColor(), Color.TRANSPARENT); 
     squareColor.setColorFilter(filter); 
     canvas.drawBitmap(image, null, tileRect, squareColor); 
    } else { 
     canvas.drawRect(tileRect, squareColor); 
    } 
} 

であり、これはチェス盤(左画像)のように見える方法です

1 2

右側の画像としてボード。

ColorFilter filter = new LightingColorFilter(squareColor.getColor(), Color.TRANSPARENT); 
squareColor.setColorFilter(filter); 

私の作品は、ピースが、正方形で描かれていない透明な背景を持つ通常のイメージです。私はどのようにしての背後にある赤の色をにすることができますか? (同じイメージを背景色またはカラー表示にしたImageViewの場合と同じように)

答えて

1

pieceDrawableがnullの場合にのみ、背景を描画します。コードを次のように変更します。

public void draw(final Canvas canvas) { 
    canvas.drawRect(tileRect, squareColor); // Draws background no matter if place is empty. 
    if(pieceDrawable != null) { 
     Bitmap image = ((BitmapDrawable) pieceDrawable).getBitmap(); 
     ColorFilter filter = new LightingColorFilter(squareColor.getColor(), Color.TRANSPARENT); 
     squareColor.setColorFilter(filter); 
     canvas.drawBitmap(image, null, tileRect, squareColor); 
    } 
} 
+0

Works!どうもありがとう。将来的に、タイルの色をドロウアブル/ビットマップに置き換えたい場合には、どうすればよいのでしょうか? (木製のイメージ/マスクなど) – BlackBox

+1

'drawRect()'呼び出しを 'drawBitmap()'で置き換えるだけです。 'Canvas'では、' draw'呼び出しがもう一方の上に描画されます。 –

関連する問題