在具有彩色背景的 Canvas 上绘制位图(来自可绘制对象)
Draw a bitmap (from a drawable) on a Canvas with a colored background
我正在做一个国际象棋应用程序,当我绘制棋盘的图块时,它们不会在(透明)背景中着色。基本上是我想要的,它类似于显示带有彩色背景的图像(具有透明背景)的 ImageView 中发生的情况。
这是代码
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);
}
}
这就是棋盘的样子(左图)
如果我在 drawBitmap call
之前注释掉这两行,我得到的板子就是正确的图像。
ColorFilter filter = new LightingColorFilter(squareColor.getColor(), Color.TRANSPARENT);
squareColor.setColorFilter(filter);
我的作品是透明背景的普通图片,没有在正方形中绘制。我怎样才能让作品后面变成红色? (就像发生在具有背景颜色或彩色视图上的相同图像的 ImageView 上一样)
如果 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);
}
}
我正在做一个国际象棋应用程序,当我绘制棋盘的图块时,它们不会在(透明)背景中着色。基本上是我想要的,它类似于显示带有彩色背景的图像(具有透明背景)的 ImageView 中发生的情况。
这是代码
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);
}
}
这就是棋盘的样子(左图)
如果我在 drawBitmap call
之前注释掉这两行,我得到的板子就是正确的图像。
ColorFilter filter = new LightingColorFilter(squareColor.getColor(), Color.TRANSPARENT);
squareColor.setColorFilter(filter);
我的作品是透明背景的普通图片,没有在正方形中绘制。我怎样才能让作品后面变成红色? (就像发生在具有背景颜色或彩色视图上的相同图像的 ImageView 上一样)
如果 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);
}
}