如何使用onTouchEvent在图像上绘制矩形?
但矩形不会被图像视图重叠?
答案 0 :(得分:0)
这就是你所需要的!用画布替换你的ImageView(我假设你的意思是ImageView),并在画布上绘制它! Here是一个关于如何使用它的好教程,如果你不知道如何使用画布,我强烈建议你全部阅读!
要在触摸时绘制,请在触摸时创建路径,并在onDraw方法上继续绘制路径!我没有编写以下代码,它来自here。
@Override
public boolean onTouchEvent(MotionEvent event) {
synchronized (_thread.getSurfaceHolder()) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
path = new Path();
path.moveTo(event.getX(), event.getY());
path.lineTo(event.getX(), event.getY());
}else if(event.getAction() == MotionEvent.ACTION_MOVE){
path.lineTo(event.getX(), event.getY());
}else if(event.getAction() == MotionEvent.ACTION_UP){
path.lineTo(event.getX(), event.getY());
_graphics.add(path);
}
return true;
}
}
@Override
public void onDraw(Canvas canvas) {
//Draw your image as bitmap
canvas.drawBitmap(your_image, 0, 0, null);
//Draw your path
for (Path path : _graphics) {
//canvas.drawPoint(graphic.x, graphic.y, mPaint);
canvas.drawPath(path, mPaint);
}
}
祝你好运!