gpt4 book ai didi

android - 如何在 Canvas 上绘制多个位图

转载 作者:太空狗 更新时间:2023-10-29 14:32:15 25 4
gpt4 key购买 nike

我在使用 Canvas 功能时遇到了问题,我在 Canvas 上绘制 1 个位图没有问题,但是一旦我添加了第二个位图,它就只绘制最后一个位图,不会同时显示这两个位图。请帮忙。这是我的代码。我的 Intent 是在屏幕上独立地为这 2 个不同的位图设置动画。

@Override
protected void onDraw(Canvas canvas) {
football = BitmapFactory.decodeResource(getResources(),
R.drawable.ballicon);

receiver = BitmapFactory.decodeResource(getResources(),
R.drawable.rec);


canvas.drawBitmap(football, translate, null);
canvas.drawBitmap(receiver, translate, null);


Matrix m = canvas.getMatrix();
Log.d(DEBUG_TAG, "Matrix: " + translate.toShortString());
Log.d(DEBUG_TAG, "Canvas: " + m.toShortString());
}

谢谢

最佳答案

这是一个用于绘制多个位图的canvasview

public class AdvanceCanvasView extends View {
private Bitmap bitmap;
private Canvas bitmapCanvas;
private Bitmap mBitmapBrush;
private ArrayList<Bitmap> bitmapArrayList;
private Vector2 mBitmapBrushDimensions;
private Paint paintLine;
private List<Vector2> mPositions = new ArrayList<Vector2>(100);
private HashMap<Integer, Path> pathMap; // current Paths being drawn
private HashMap<Integer, Point> previousPointMap; // current Points
private int i = 0;

private static final class Vector2 {
public Vector2(float x, float y) {
this.x = x;
this.y = y;
}

public final float x;
public final float y;
}

@SuppressLint("UseSparseArrays")
public AdvanceCanvasView(Context context, AttributeSet attrs) {
super(context, attrs); // pass context to View's constructor
}

public AdvanceCanvasView(Context c) {
super(c);
pathMap = new HashMap<>();
previousPointMap = new HashMap<>();
bitmapArrayList = new ArrayList<>();
paintLine = new Paint();
}

@Override
public void onSizeChanged(int w, int h, int oldW, int oldH) {
bitmap = Bitmap.createBitmap(getWidth(), getHeight(),
Bitmap.Config.ARGB_8888);
bitmapCanvas = new Canvas(bitmap);
}

@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(bitmap, 0, 0, null);
for (int i = 0; i < mPositions.size(); i++) {
canvas.drawBitmap(bitmapArrayList.get(i), mPositions.get(i).x, mPositions.get(i).y, null);
}

}

@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getActionMasked();
int actionIndex = event.getActionIndex();

if (action == MotionEvent.ACTION_DOWN
|| action == MotionEvent.ACTION_POINTER_DOWN) {
touchStarted(event.getX(actionIndex), event.getY(actionIndex),
event.getPointerId(actionIndex));
}
else if (action == MotionEvent.ACTION_UP
|| action == MotionEvent.ACTION_POINTER_UP) {
touchEnded(event.getPointerId(actionIndex));
}
else {
touchMoved(event);
}

invalidate();

return true;
}

private void touchStarted(float x, float y, int lineID) {
Path path;
Point point;
path = new Path(); // create a new Path
pathMap.put(lineID, path); // add the Path to Map
point = new Point(); // create a new Point
previousPointMap.put(lineID, point); // add the Point to the Map
path = new Path(); // create a new Path
point = new Point(); // create a new Point
path.moveTo(x, y);
point.x = (int) x;
point.y = (int) y;

} // end method touchStarted

private void touchMoved(MotionEvent event) {
// for each of the pointers in the given MotionEvent
for (int i = 0; i < event.getPointerCount(); i++) {
final float posX = event.getX();
final float posY = event.getY();
mPositions.add(new Vector2(posX - mBitmapBrushDimensions.x / 2, posY - mBitmapBrushDimensions.y / 2));
bitmapArrayList.add(mBitmapBrush);
}
invalidate();

}

private void touchEnded(int lineID) {
Path path = pathMap.get(lineID);
path.reset();
}

public void init(Bitmap bitmap) {
mBitmapBrush = bitmap;
BitmapShader shader = new BitmapShader(mBitmapBrush, Shader.TileMode.MIRROR, Shader.TileMode.MIRROR);
paintLine.setShader(shader);
mBitmapBrushDimensions = new Vector2(mBitmapBrush.getWidth(), mBitmapBrush.getHeight());
}

}

关于android - 如何在 Canvas 上绘制多个位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5428165/

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