gpt4 book ai didi

java - 多次绘制位图

转载 作者:行者123 更新时间:2023-11-29 23:06:53 25 4
gpt4 key购买 nike

我正在尝试多次绘制位图,类似于此:

enter image description here

下面的代码让位图移动:

public class TheChainView extends View {
Bitmap bitmap;
float x = 200;
float y = 200;

public TheChainView(Context context, AttributeSet attrs) {
super(context, attrs);
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heart);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = event.getX();
y = event.getY();
invalidate();
break;
case MotionEvent.ACTION_MOVE:
x = event.getX();
y = event.getY();
invalidate();
break;
}
return true;
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(bitmap, x, y, null);
}
}

当我触摸屏幕时,如何使位图多次绘制

最佳答案

这可能不是一个好的解决方案,但您可以在某些数据结构中保存坐标,如下所示:

   public class TheChainView extends View {
Bitmap bitmap;
List<Point> points = new ArrayList<>();

public TheChainView(Context context, AttributeSet attrs) {
super(context, attrs);
bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.heart);
points.add(new Point(200, 200));
}

@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
points.add(new Point(event.getX(), event.getY()));
invalidate();
break;
}
return true;
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (Point p : points) {
canvas.drawBitmap(bitmap, p.x, p.y, null);
}
}

static class Point {
float x, y;
Point(float x, float y) {
this.x = x;
this.y = y;
}
}
}

关于java - 多次绘制位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56416344/

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