单次触摸感应 4 个事件



按钮通过onDraw绘制在画布上。在下面的方法中,我得到了绘制按钮的位置并检测其中的触摸。一旦感应到snapShot();就被调用。我已将snapShot();的内容替换为System.out.println("snapShot(); is called");.每一次触摸,我都会不断地打印出四行。我不明白这种方法是如何连续多次调用 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);
};
因为当您

的手指向下,移动和抬起时,会调用onTouch。 您需要检查 MotionEvent 以确定哪个手指在做什么。 在不引起移动事件的情况下触摸也可能非常困难 - 请参阅触摸斜坡。

http://www.mybringback.com/tutorial-series/3279/android-the-basics-32-androids-ontouchlistener-and-motionevent/

安卓ACTION_MOVE阈值

最新更新