2012-10-23 37 views
2

我有一個ImageView,但我怎麼能看到用戶有點擊/觸及在圖像上。Android SDK獲取點擊圖片

我知道我必須使用MotionEvent.getX()MotionEvent.getY(),但是如何查看用戶點擊圖像的位置?

謝謝!

回答

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; 
} 
+0

你能解釋嗎? –

+0

查看編輯答案。 –

+0

謝謝!這非常有用! –