如何在 android 中使用 onTouchevent 在图像上绘制矩形?

问题描述 投票:0回答:1

如何使用 onTouchEvent 在图像上绘制矩形?

但是矩形不会被图像视图重叠?

android canvas touch-event
1个回答
0
投票

这就是你需要的!将您的 ImageView(我假设您所说的图像是指 ImageView)替换为画布,并将其绘制在您的画布上!

要在触摸时绘制,请在触摸时创建路径,并在 onDraw 方法上继续绘制路径!下面的代码不是我写的,是从这里.

@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);
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.