2013-02-27 107 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

相关问题