2013-02-27 93 views
0

我讀過其他問題如何獲取位圖的位置以及如何定義OnTouchEvent,但使用Bitmap對象無法設置偵聽器,我該如何做?如何將偵聽器添加到畫布中的位圖? (Android)

public boolean onTouchEvent(MotionEvent event){ 
int action = event.getAction(); 
int x = event.getX() // or getRawX(); 
int y = event.getY(); 

switch(action){ 
case MotionEvent.ACTION_DOWN: 
    if (x >= xOfYourBitmap && x < (xOfYourBitmap + yourBitmap.getWidth()) 
      && y >= yOfYourBitmap && y < (yOfYourBitmap + yourBitmap.getHeight())) { 
     //tada, if this is true, you've started your click inside your bitmap 
    } 
    break; 
} 
} 

回答

0

你必須繪製一個View內位圖(例如平原View和繪製Bitmap作爲背景或者作爲ImageView的源/圖像)。然後,您可以將onTouchListener附加到此View,並在該聽衆中處理您的onTouchEvent

+0

...但以這種方式我不能使用畫布 – Roran 2013-02-27 21:24:33

+0

我不明白。你想用帆布做什麼?在你的問題中,你只是想知道點擊正確的位置。 – 2013-02-27 21:27:07

+0

我的課使用畫布的onDraw方法,我想添加一個監聽器在它繪製的位圖 – Roran 2013-02-27 21:34:35

相關問題