按钮是通过onDraw绘制在画布上的。在下面的方法中,我将获取绘制的按钮的位置,并检测其中的触摸。一旦它被检测到,就会调用snapShot();。我已经用System.out.println("snapShot(); is called");替换了snapShot();的内容。每次触摸时,我都会打印出四行。我不明白这个方法是如何连续调用snapShot()的?
public boolean onTouch(View view, MotionEvent me) {
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.camera);
DisplayMetrics metrics = getResources().getDisplayMetrics();
int w = metrics.widthPixels;
int h = metrics.heightPixels;
int heightOffset = - bitmap.getHeight() + h;
int widthOffset = w - bitmap.getWidth();
//See if the motion event is on a Marker
if((me.getRawX() >= widthOffset && me.getRawX() < (widthOffset + bitmap.getWidth())
&& me.getRawY() >= heightOffset && me.getRawY() < (heightOffset + bitmap.getHeight())))
{
snapShot();
return true;
}
return super.onTouchEvent(me);
};发布于 2012-10-12 21:55:51
因为当手指向下、移动和抬起时,都会调用onTouch。您需要检查MotionEvent以确定哪个手指在做什么。在不引起移动事件的情况下触摸它也是非常困难的-请参见触摸斜率。
http://www.mybringback.com/tutorial-series/3279/android-the-basics-32-androids-ontouchlistener-and-motionevent/
https://stackoverflow.com/questions/12860370
复制相似问题