错误执行的Andr​​oid枪王 [英] Error in implementing Android double Tap

查看:127
本文介绍了错误执行的Andr​​oid枪王的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现OnDoubleTapListener在我的活动类,并且覆盖三种方法如下。

I implemented OnDoubleTapListener in my Activity class and override three methods as follow.

@Override
    public boolean onDoubleTap(MotionEvent e) {
        // TODO Auto-generated method stub
        return true;
    }



    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        // TODO Auto-generated method stub
        if(e.getAction() == 1){
            Toast.makeText(getApplicationContext(),"Double tap happened", Toast.LENGTH_SHORT).show();
        }
        return true;
    }



    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        // TODO Auto-generated method stub
        return true;
    }

但是当我运行这在真实设备什么都没有发生。什么是错误?此外,我怎么能找到具体的录音项目(双人间录音项目)?

But when I run this in real device nothing happened. What is the error ? Also how could I find the particular taped item (Double taped item)?

我看到用onTouchEvent(MotionEvent五)一些方法教程和计数两次触摸之间的时间差。什么是做这个过程中,正确的方法是什么?

I saw some tutorials used onTouchEvent(MotionEvent e) method and count the time difference in between two touches. What is the correct way to do this process?

推荐答案

GestureDetector会消耗你已经有孩子,例如,触摸事件,如果你不想,你可以用下面的code相反,我做不要使用 ACTION_UP 所以我并不需要消耗事件(只有第二个水龙头将被消耗)

GestureDetector will consume the touch events you already have on children for example, if you do not want that you can use the following code instead, I do not use ACTION_UP so I do not need to consume the event (only the second tap will be consumed)

public class DoubleTapDetector {

    public interface OnDoubleTapListener {
        boolean onDoubleTap(MotionEvent e);

        boolean onDoubleTapEvent(MotionEvent e);
    }

    private int mDoubleTapSlopSquare;

    private static final int DOUBLE_TAP_TIMEOUT = ViewConfiguration.getDoubleTapTimeout();
    private static final int DOUBLE_TAP_MIN_TIME = 40;

    private static final int DOUBLE_TAP_SLOP = 100;

    private OnDoubleTapListener mDoubleTapListener;

    private MotionEvent mCurrentDownEvent;

    public DoubleTapDetector(Context context, OnDoubleTapListener listener) {
        mDoubleTapListener = listener;
        init(context);
    }

    private void init(Context context) {
        if (mDoubleTapListener == null) {
            throw new NullPointerException("OnDoubleTapListener must not be null");
        }

        int doubleTapSlop;
        if (context == null) {
            doubleTapSlop = DOUBLE_TAP_SLOP;
        } else {
            final ViewConfiguration configuration = ViewConfiguration.get(context);
            doubleTapSlop = configuration.getScaledDoubleTapSlop();
        }
        mDoubleTapSlopSquare = doubleTapSlop * doubleTapSlop;
    }

    public boolean onTouchEvent(MotionEvent ev) {
        final int action = ev.getAction();

        boolean handled = false;

        switch (action) {
        case MotionEvent.ACTION_DOWN:
            if ((mCurrentDownEvent != null) &&
                isConsideredDoubleTap(mCurrentDownEvent, ev)) {
                // This is a second tap
                // Give a callback with the first tap of the double-tap
                handled |= mDoubleTapListener.onDoubleTap(mCurrentDownEvent);
                // Give a callback with down event of the double-tap
                handled |= mDoubleTapListener.onDoubleTapEvent(ev);
            } else {
                // This is a first tap
            }

            if (mCurrentDownEvent != null) {
                mCurrentDownEvent.recycle();
            }
            mCurrentDownEvent = MotionEvent.obtain(ev);
            break;
        }

        return handled;
    }

    private boolean isConsideredDoubleTap(MotionEvent firstDown, MotionEvent secondDown) {
        final long deltaTime = secondDown.getEventTime() - firstDown.getEventTime();
        if (deltaTime > DOUBLE_TAP_TIMEOUT || deltaTime < DOUBLE_TAP_MIN_TIME) {
            return false;
        }

        int deltaX = (int) firstDown.getX() - (int) secondDown.getX();
        int deltaY = (int) firstDown.getY() - (int) secondDown.getY();
        return (deltaX * deltaX + deltaY * deltaY < mDoubleTapSlopSquare);
    }

}

用法:

DoubleTapDetector doubleTapDetector = new DoubleTapDetector(conversationWindow, new DoubleTapDetector.OnDoubleTapListener() {
        @Override
        public boolean onDoubleTap(MotionEvent e) {
            // Double tap detected.
            return false;
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            return false;
        }
    });

someView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent e) {
            return doubleTapDetector.onTouchEvent(e);
        }
    });

这篇关于错误执行的Andr​​oid枪王的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆