1

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

请,任何人都可以有一些逻辑来处理所有这些。示例应用程序将不胜感激。帮我解决这个问题,我需要尽快完成这项任务。提前致谢。

4

2 回答 2

2

您应该使用它GestureDectector来识别触摸事件和运动事件。您将获得屏幕的初始坐标和最终坐标。然后,您必须调用一种算法来根据您获得的坐标确定滑动手势的方向。这并不难。这是一个关于如何使用的链接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");
}

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

于 2013-05-23T09:28:00.810 回答
0

使用 GestureDetector 时,您必须自己识别每个手势,更好的解决方案是使用 GestureOverlayView 和名为 GestureBuilder 的系统应用程序,允许创建自定义手势

于 2013-05-23T10:13:00.087 回答