Android SDK获取点击图片的位置

时间:2012-10-23 08:22:57

标签: android

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

我知道我必须使用MotionEvent.getX()MotionEvent.getY(),但如何才能看到用户点击图片的位置?

谢谢!

1 个答案:

答案 0 :(得分: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;
}