How to implement gesture recognition in android we

2020-06-11 03:06发布

问题:

Hello i'm looking for how to detect gestures in android wear, in android i use some code like this, but doesn't work in android wear.. is there a way to override the default gestures actions or just recognize them?

I'm doing just like google developers: https://www.youtube.com/watch?v=naf_WbtFAlY http://youtu.be/sha_w3_5c2c?t=25m1s Is there something wrong with the emulator? (i'm using 32bit emulator: 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);
    }


}

I want swipe left and right, and scroll upp and down gestures:

Solved! check my answer..

回答1:

After one day i got it to work, the lost part was that i must disable the Swipe-To-Dismiss Gesture and add DismissOverlayView to my layout, steps:

  1. Disable the Swipe-To-Dismiss Gesture add AppTheme style to your styles.xml and use it as app style in your 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" >
  1. Add DissmissOverlayView to your main layout

        <android.support.wearable.view.DismissOverlayView 
        android:id="@+id/dismiss_overlay"
        android:layout_height="match_parent"
        android:layout_width="match_parent"/>
    
  2. Use it in your activity like this to get gestures

        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, 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;
            }
            });
        }
    
        // Capture long presses
        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            return mDetector.onTouchEvent(ev) || super.onTouchEvent(ev);
        } 
        }
    


回答2:

Did you checked your code? It works fine. For simple action there is SimpleOnGestureListener that you can use to seed up work (it works on Android Wear as well):

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

        //http://developer.android.com/reference/android/view/GestureDetector.SimpleOnGestureListener.html
//For example:
                public void onLongPress(MotionEvent ev) {
                    //TODO handle your action
                }
            });

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