2

我有一个ImageView,但我如何才能看到用户在图像上单击/触摸的位置。

我知道我必须使用MotionEvent.getX()and MotionEvent.getY(),但是我怎样才能看到用户点击了图像的位置?

谢谢 !

4

1 回答 1

2

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

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;
}
于 2012-10-23T08:24:28.020 回答