java – 如何在Android中实现手势识别

前端之家收集整理的这篇文章主要介绍了java – 如何在Android中实现手势识别前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
你好,我正在寻找如何检测手势在 android穿,在 android我使用一些这样的代码,但不工作在android穿..有办法覆盖默认的手势动作或只是认识他们?

我正在像google开发人员:
https://www.youtube.com/watch?v=naf_WbtFAlY
http://youtu.be/sha_w3_5c2c?t=25m1s
仿真器有什么问题吗? (我使用32位仿真器:emulator -avd AndroidWearXC -force-32bit)

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;


public class MainActivity extends Activity {
    private GestureDetector mDetector;

    private static String DEBUG_TAG="MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mDetector = new GestureDetector(this,new GestureDetector.SimpleOnGestureListener() {

            @Override
            public void onLongPress(MotionEvent event) {

                Log.d(DEBUG_TAG," onLongPress: " + event.toString());
            }

            @Override
            public boolean onDown(MotionEvent event) {
                Log.d(DEBUG_TAG," onDown: " + event.toString());
                return true;
            }

            @Override
            public boolean onFling(MotionEvent event1,MotionEvent event2,float velocityX,float velocityY) {
                Log.d(DEBUG_TAG," onFling: " + event1.toString()+event2.toString());
                return true;
            }

            @Override
            public boolean onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float distanceY) {
                Log.d(DEBUG_TAG," onScroll: " + e1.toString()+e2.toString());
                return true;
            }

            @Override
            public void onShowPress(MotionEvent event) {
                Log.d(DEBUG_TAG," onShowPress: " + event.toString());
            }

            @Override
            public boolean onSingleTapUp(MotionEvent event) {
                Log.d(DEBUG_TAG," onSingleTapUp: " + event.toString());
                return true;
            }

            @Override
            public boolean onDoubleTap(MotionEvent event) {
                Log.d(DEBUG_TAG," onDoubleTap: " + event.toString());
                return true;
            }

            @Override
            public boolean onDoubleTapEvent(MotionEvent event) {
                Log.d(DEBUG_TAG," onDoubleTapEvent: " + event.toString());
                return true;
            }

            @Override
            public boolean onSingleTapConfirmed(MotionEvent event) {
                Log.d(DEBUG_TAG," onSingleTapConfirmed: " + event.toString());
                return true;
            }
        });

    }
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return mDetector.onTouchEvent(ev)  || super.onTouchEvent(ev);
    }


}

我想要左右滑动,并滚动upp和down手势:

解决了!检查我的答案..

解决方法

有一天我得到它的工作,失去的一部分是,我必须禁用滑动关闭手势,并添加DismissOverlayView到我的布局,步骤:

>禁用滑动关闭手势
将AppTheme样式添加到您的styles.xml中,并将其用作mainfest中的应用程序样式

styles.xml:

<style name="AppTheme" parent="Theme.DeviceDefault">
    <item name="android:windowSwipeToDismiss">false</item>
    </style>

AndroidManifest.xml中:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

>将DissmissOverlayView添加到您的主布局

<android.support.wearable.view.DismissOverlayView 
    android:id="@+id/dismiss_overlay"
    android:layout_height="match_parent"
    android:layout_width="match_parent"/>

>在您的活动中使用它来获取手势

public class WearActivity extends Activity {
    private DismissOverlayView mDismissOverlay;
    private GestureDetector mDetector;

    public void onCreate(Bundle savedState) {
        super.onCreate(savedState);
        setContentView(R.layout.wear_activity);

        // Obtain the DismissOverlayView element
        mDismissOverlay = (DismissOverlayView) findViewById(R.id.dismiss_overlay);
        mDismissOverlay.setIntroText(R.string.long_press_intro);
        mDismissOverlay.showIntroIfNecessary();

        // Configure a gesture detector
        mDetector = new GestureDetector(this,new SimpleOnGestureListener() {

        @Override
        public void onLongPress(MotionEvent event) {

            Log.d(DEBUG_TAG," onLongPress: " + event.toString());
        }

        @Override
        public boolean onDown(MotionEvent event) {
            Log.d(DEBUG_TAG," onDown: " + event.toString());
            return true;
        }

        @Override
        public boolean onFling(MotionEvent event1,float velocityY) {
            Log.d(DEBUG_TAG," onFling: " + event1.toString()+event2.toString());
            return true;
        }

        @Override
        public boolean onScroll(MotionEvent e1,float distanceY) {
            Log.d(DEBUG_TAG," onScroll: " + e1.toString()+e2.toString());
            return true;
        }

        @Override
        public void onShowPress(MotionEvent event) {
            Log.d(DEBUG_TAG," onShowPress: " + event.toString());
        }

        @Override
        public boolean onSingleTapUp(MotionEvent event) {
            Log.d(DEBUG_TAG," onSingleTapUp: " + event.toString());
            return true;
        }

        @Override
        public boolean onDoubleTap(MotionEvent event) {
            Log.d(DEBUG_TAG," onDoubleTap: " + event.toString());
            return true;
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent event) {
            Log.d(DEBUG_TAG," onDoubleTapEvent: " + event.toString());
            return true;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent event) {
            Log.d(DEBUG_TAG," onSingleTapConfirmed: " + event.toString());
            return true;
        }
        });
    }

    // Capture long presses
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return mDetector.onTouchEvent(ev) || super.onTouchEvent(ev);
    } 
    }
原文链接:https://www.f2er.com/android/126184.html

猜你在找的Android相关文章