我有一个ImageView,但是如何查看用户单击/触摸图像的位置。

我知道必须使用MotionEvent.getX()MotionEvent.getY(),但是如何查看用户单击图像的位置?

谢谢 !

最佳答案

您必须对图像视图进行子类化,并在用户触摸的位置显式绘制一个指示器。

public class TouchableImageView extends ImageView {

  // Constructors should come here

  // Override onTouch to remember the touch position in our variable touchLocation
  // Set touchLocation to null when getting the ACTION_UP event

  // Override onDraw to draw something at touchLocation. You should create a proper Paint object
  // in your constructors and use it here
  public void onDraw(Canvas c) {
    if (touchLocation!=null) {
      canvas.drawCircle(touchLocation.x, touchLocation.y, 10, indicatorPaint);
    }
  }

  private Point touchLocation;
  private Paint indicatorPaint;
}

10-08 03:05