检测触摸位图 [英] Detect touch on bitmap
问题描述
映入眼帘,
有谁我如何去检测时对位图这是一个帆布内的用户presses?
Does anyone how I go about detecting when a user presses on a bitmap which is inside a canvas?
感谢
推荐答案
您应该有一些像这样:
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;
}
}
这是一个开始,但你需要处理情况下MotionEvent.ACTION_MOVE
和情况下MotionEvent.ACTION_UP
,以确保你妥善处理用户输入。该的onTouchEvent
方法被调用每次用户:把手指向下,移动手指已经在屏幕上或举起手指。每次事件进行的X和其中,手指是Y坐标。例如,如果你想检查有人窃听你的位图里面,你应该做类似设置一个布尔值,触摸开始ACTION_DOWN位图里面,然后检查ACTION_UP说你还是位图内。
That's a start, but you need to handle case MotionEvent.ACTION_MOVE
and case MotionEvent.ACTION_UP
to make sure you properly deal with the user input. The onTouchEvent
method gets called every time the user: puts a finger down, moves a finger already on the screen or lifts a finger. Each time the event carries the X and Y coordinates of where the finger is. For example if you want to check for someone tapping inside your bitmap, you should do something like set a boolean that the touch started inside the bitmap on ACTION_DOWN, and then check on ACTION_UP that you're still inside the bitmap.
这篇关于检测触摸位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!