区分单点触控和多点触控Android

时间:2019-01-04 07:48:39

标签: android android-studio multi-touch ontouch

我不知道我的问题对这个论坛是否有效。如果不是这种情况,我会提前道歉。

如何区分单点和多点触摸(用两个手指)?

我想确定确切是一个按钮将出现在屏幕的一角。

情况A: 如果用户触摸并按住此视图,则应执行拖动事件

情况B: 如果用户执行多点触摸时用两根手指握住屏幕,而一只手指在按钮上,另一只手指在另一侧的角上,则会启动不同的屏幕。

我以前在案例B的屏幕上保留以下代码。

使用以下代码也可以实现案例A。

// touch listener which handles onLongPress(two points hold) and drag events
private View.OnTouchListener mOnTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent event) {
        final int displayWidth = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay().getWidth();
        final int displayHeight = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE))
                .getDefaultDisplay().getHeight();

        // coordinates for drag listener
        float dX = 0;
        float dY = 0;

        // coordinates of first
        float firx = 0;
        float firy = 0;

        // coordinates of second
        float secx = 0;
        float secy = 0;

        Log.d(TAG, "onTouch->(Event : " + event.toString() + ")");



        // captures first touch in hold
        if (event.getActionMasked() == MotionEvent.ACTION_DOWN) {
            firx = event.getX();
            firy = event.getY();
            dX = view.getX() - event.getRawX();
            dY = view.getY() - event.getRawY();
            Log.d(TAG, "1st cor:" + firx + ":" + firy);
        }

        // captures drag event
        if (event.getActionMasked() ==MotionEvent.ACTION_MOVE){
            //handleAppSwap( Math.round(dX),Math.round(event.getRawX() + dX));
            Log.d(TAG, "Drag event happens" );
        }
        // captures second touch event in hold
        if (event.getActionMasked() == MotionEvent.ACTION_POINTER_DOWN && event.getActionIndex() == 0) {
            Log.d(TAG, "captures second ");
            secx = event.getX();
            secy = event.getY();
            Log.d(TAG, "2nd cor:" + secx + ":" + secy);
            mSecondFingerTimeDown = System.currentTimeMillis();
        }

        // release event  on screen corners
        if (event.getActionMasked() == MotionEvent.ACTION_POINTER_UP && event.getActionIndex() == 0) {
            if (((firx < X_OFFSET && firy < Y_OFFSET + 100) && (secx > displayWidth - X_OFFSET && secy > displayHeight - Y_OFFSET)) || ((secx < X_OFFSET && secy < Y_OFFSET) && (firx > displayWidth - X_OFFSET && firy > displayHeight - Y_OFFSET))) {
                if ((System.currentTimeMillis() - mSecondFingerTimeDown) >= 5000) {
                    Log.d(TAG, "HOLD AND RELEASE........");
                    mEngHandler.removeCallbacksAndMessages(null);
                    Message msg = mEngHandler.obtainMessage(ACTION_BOTTOM_RIGHT_RELEASE);
                    mEngHandler.sendMessage(msg);
                }
            }

        }
        return false;
    }
};

预先感谢。任何链接或代码都会有帮助

0 个答案:

没有答案