[英]Draw and remove a bitmap icon on canvas
我正在开发一个应用程序,用户可以在其中标记汽车上的损坏图标。 这里的汽车基本上是一个图像,损坏图标也是一个图像。 我可以使用以下代码绘制用户触摸汽车的图像:
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);
这会将现有位图设置为全透明。
我的解决方案如下:每次用户单击图像时 - 将损坏边界(x,y,w,h)存储在列表中并将其绘制在画布上如果用户再次单击相同的损坏图像 - 从列表中删除它并重新绘制(干净的画布 - Canvas.drawColor(Color.WHITE))一切:汽车和列表中的剩余图像
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.