gpt4 book ai didi

android - 在 Canvas 上绘制和删除位图图标

转载 作者:行者123 更新时间:2023-11-30 02:14:30 28 4
gpt4 key购买 nike

我正在开发一个应用程序,用户可以在其中标记汽车上的损坏图标。这里的车基本上是一个图像,损坏图标也是一个图像。我可以使用以下代码绘制用户触摸汽车的图像:

public class TestActivity extends Activity {
private Button btn_save, btn_resume;
private ImageView iv_canvas;
private Bitmap baseBitmap;
private Canvas canvas;
private Paint paint;
public Bitmap bt;
public Bitmap ct;
boolean ismove = false;
private static final int SWIPE_MIN_DISTANCE = 50;
int i = 0;
private float x = 0;

public HashMap<String, String> testholdmap = new HashMap<String, String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// The initialization of a brush, brush width is 5, color is red
paint = new Paint();
paint.setStrokeWidth(5);
paint.setColor(Color.RED);

iv_canvas = (ImageView) findViewById(R.id.iv_canvas);

iv_canvas.setOnTouchListener(touch);

bt = BitmapFactory.decodeResource(getResources(),
R.drawable.damage_mark_r_ic);
}

private View.OnTouchListener touch = new OnTouchListener() {
// Coordinate definition finger touch
float startX;
float startY;

@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
// Press the user action
case MotionEvent.ACTION_DOWN:
// The first drawing initializes the memory image, specify the
// background is white
if (baseBitmap == null) {
baseBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.b_ferrari_f152_000_0000).copy(
Bitmap.Config.ARGB_8888, true);
canvas = new Canvas(baseBitmap);

}
// Recording began to touch point coordinate
startX = event.getX();
startY = event.getY();
break;
// The user's finger on the screen of mobile action
case MotionEvent.ACTION_MOVE:

break;
case MotionEvent.ACTION_UP:

float stopX = event.getX();
float stopY = event.getY();
if (startX == stopX) {

canvas.drawBitmap(bt, startX, startY, paint);

// The pictures to the ImageView
iv_canvas.setImageBitmap(baseBitmap);
} else if (startX - stopX > 50 || stopX - startX > 50) {

baseBitmap = null;
}
break;
default:
break;
}
return true;
}
};

}

我的问题是弄清楚如果已经绘制了这个图标,如何删除它。我希望它以这种方式运行:我在 x,y 位置画了一个损坏图标,如果用户再次点击 x,y 位置,那么这个损坏图标将被删除。

我怎样才能做到这一点?

最佳答案

使用这个:

canvas.drawColor(0, Mode.CLEAR);

overlayBitmap.eraseColor(Color.TRANSPARENT);

这会将现有位图设置为全透明。

关于android - 在 Canvas 上绘制和删除位图图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29569255/

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