gpt4 book ai didi

android - 在 Canvas 上绘制而不清除先前绘制的位图

转载 作者:行者123 更新时间:2023-12-04 23:45:03 26 4
gpt4 key购买 nike

我需要在 Canvas 上绘制位图,然后开始在上面绘制。当我在 Canvas 上绘制其他对象时,先前绘制的位图会被清除。为了避免位图被清除,我必须在每个 ondraw() 调用中绘制它。必须有其他方法来更新先前绘制的图形,否则它会非常有效,因为我可能必须绘制许多位图。

    @Override
protected void onDraw(Canvas mCanvas) {
for (Pair<Path, Paint> p : paths) {
mCanvas.drawPath(p.first, p.second);
}
if(merge){
canvas.drawBitmap(bmp, transform, new Paint());
}

}

那么,在不丢失之前绘制的绘图上绘制的最有效方法是什么。

最佳答案

我尝试通过使用 BitmapDrawable 实现棋子来编写此代码。 , 然后只调用 invalidateDrawable在被移动的部分。
效果很好,但是当我检查哪些部分被重绘时,结果发现一切似乎都被重绘了(或者至少所有 onDraw 函数都被调用了)。
检查documentation of the invalidate methodView再次上课,我发现了以下内容:

This method was deprecated in API level 28.The switch to hardware accelerated rendering in API 14 reduced the importance of the dirty rectangle. In API 21 the given rectangle is ignored entirely in favor of an internally-calculated area instead. Because of this, clients are encouraged to just call invalidate().


这向我表明,也许根本没有必要担心这种情况。似乎 Android 在内部计算出要重绘的内容,并且有效地这样做了。
以防万一,您可以在 my GitHub 上找到工作版本.
此外,这是应用程序中最相关的部分:
public class RedrawTest extends View {
private class ChessPiece extends BitmapDrawable {
final int id;
public ChessPiece(Context context, int _id) {
super(getResources(), getBitmapFromAsset(context, "chess_piece.png"));
id = _id;
}
}

final Bitmap chessboard;
final ChessPiece[] chessPieces;
final Rect boardSize;
final Paint paint = new Paint();
final ViewOverlay overlay;

final int borderSize = 32;
final int nChestPieces = 6;

private static Bitmap getBitmapFromAsset(Context context, String filePath) {
AssetManager assetManager = context.getAssets();
InputStream inputStream;
Bitmap bitmap = null;
try {
inputStream = assetManager.open(filePath);
bitmap = BitmapFactory.decodeStream(inputStream);
} catch (IOException e) {
// handle exception
}
return bitmap;
}

public RedrawTest(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
chessboard = getBitmapFromAsset(context, "chessboard.jpg");
boardSize = new Rect(
borderSize, borderSize,
chessboard.getWidth()-borderSize, chessboard.getHeight()-borderSize
);
overlay = getOverlay();
chessPieces = new ChessPiece[nChestPieces];
for (int i=0; i<nChestPieces; ++i) {
chessPieces[i] = new ChessPiece(context, i);
chessPieces[i].setBounds(getBoardRect(0, i));
overlay.add(chessPieces[i]);
}
}

public void redraw(int i, int j, int pieceId) {
chessPieces[pieceId].setBounds(getBoardRect(i, j));
invalidateDrawable(chessPieces[pieceId]);
}

private Rect getBoardRect(int i, int j) {
return new Rect(
boardSize.left + j*boardSize.width()/8 + 5,
boardSize.top + i*boardSize.height()/8 + 5,
boardSize.left + (j+1)*boardSize.width()/8 - 5,
boardSize.top + (i+1)*boardSize.height()/8 - 5);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(this.chessboard, 0, 0, paint);
}
}

关于android - 在 Canvas 上绘制而不清除先前绘制的位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28374445/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com