android - 如何处理android中的对角线手势?

标签 android gesture-recognition diagonal

我在计算 android View 中的对角线滑动事件时遇到了问题。 谁能给我一些逻辑来识别对角线手势。 我的要求是:一旦用户在手机上沿对角线滑动,我需要触发不同的事件。 喜欢: 1) 从左上到右下。 2) 从右上到左下。 3) 从左下到右。 4) 从右下到左上。

拜托,任何人都可以有一些逻辑来处理所有这些。 示例应用程序将不胜感激。帮助我摆脱困境,我需要尽快完成这项任务。 提前致谢。

最佳答案

您应该使用 GestureDector 来识别触摸事件和 Action 事件。您将获得屏幕的初始坐标和最终坐标。然后,您将不得不调用一种算法,根据您获得的坐标来确定滑动手势的方向。这并不难。这是 a link关于如何使用 GestureDetector

这是一个用于识别左右滑动和触摸事件的示例:

您可以在 onFling 方法中改进此代码,使其按照您的方式工作。

package com.ex.gessture;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

public class GestureListenerActivity extends Activity {

FrameLayout frame;
GestureDetector mGestureDetector;
String TAG = "GestureListenerActivity";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    frame = (FrameLayout) findViewById(R.id.frame);

    mGestureDetector = new GestureDetector(this, mGestureListener);

    frame.setOnTouchListener(new FrameLayout.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            return mGestureDetector.onTouchEvent(event);
        }
    });
}

/**
 * Gesture Event Handler
 */
private SimpleOnGestureListener mGestureListener = new SimpleOnGestureListener() {

    int swipe_Min_Distance = 100;
    int swipe_Max_Distance = 350;
    int swipe_Min_Velocity = 100;
/*      
    @Override
    public boolean onDown(MotionEvent e) {
        Log.i(TAG, "[CALLBACK_GL] boolean onDown(e:" + e + ")");
        return super.onDown(e);
    }
*/
    @Override
    public boolean onDown(MotionEvent e) {
        return true;
    }

    @Override
    public boolean onSingleTapUp(MotionEvent e)
    {

        /**
         *
         * Do your stuff
         *
         */

        return super.onSingleTapUp(e);
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        Log.i(TAG, "[CALLBACK_GL] boolean onSingleTapConfirmed(e:" + e + ")");
        return super.onSingleTapConfirmed(e);
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        Log.i(TAG, "[CALLBACK_GL] boolean onDoubleTap(e:" + e + ")");


        return super.onDoubleTap(e);
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        Log.i(TAG, "[CALLBACK_GL] boolean onFling(e1:" + e1 + ", e2:" + e2 + ", velocityX:" + velocityX
                + ", velocityY:" + velocityY + "");

        final float xDistance = Math.abs(e1.getX() - e2.getX());
        final float yDistance = Math.abs(e1.getY() - e2.getY());

        if(xDistance > this.swipe_Max_Distance || yDistance > this.swipe_Max_Distance)
            return false;

        velocityX = Math.abs(velocityX);
        velocityY = Math.abs(velocityY);
        boolean result = false;

        if(velocityX > this.swipe_Min_Velocity && xDistance > this.swipe_Min_Distance){
            if(e1.getX() > e2.getX()) // right to left
                Log.i(TAG, "Swipe Left");

            else
                Log.i(TAG, "Swipe Right");
        }   

        //return super.onFling(e1, e2, velocityX, velocityY);
        return true;
    }

    @Override
    public void onShowPress(MotionEvent e) {
        Log.i(TAG, "[CALLBACK_GL] void onShowPress(e:" + e + ")");
        super.onShowPress(e);
    }

    @Override
    public void onLongPress(MotionEvent e) {
        Log.i(TAG, "[CALLBACK_GL] void onLongPress(e:" + e + ")");

        super.onLongPress(e);
    }
};

}    

一个例子: 在 onFling 中使用此条件进行 TopRightToBottomLeft 滑动:

// TopRightToBottomLeft
if((e1.getX() > e2.getX()) && (e1.getY() > e2.getY())) {
    Log.i(TAG, "Swipe TopRightToBottomLeft");
}

您可以对其他事件进行类似的尝试。

关于android - 如何处理android中的对角线手势?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16709096/

相关文章:

java - 在android中顺序上传文件更安全/更好还是用多个线程同时上传文件更好?

python - spdiags() 函数在 Python 中未按预期工作

c - 遍历矩阵的相邻对角线

java - 在来自 Android 的 EWS 中将 UID 字符串转换为十六进制数组和 base64 字符串 ItemId

android - AdvertisingIdClient : Error while reading from SharedPreferences java. lang.SecurityException:不再支持 MODE_WORLD_READABLE

java - IllegalArgumentException : Parse error java. util.Date

Android:如何处理从右到左的滑动手势

Android - 向左|向右滑动。手势检测

c# - HMM 使用 Accord.Net 进行手势识别

python - NumPy - 二维矩阵次对角线上元素的总和