如何实现在Android Wear的手势识别 [英] How to implement gesture recognition in android wear

查看:388
本文介绍了如何实现在Android Wear的手势识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我正在寻找如何检测机器人磨损手势,在android系统我用一些code这样,但在Android的磨损不工作..有没有办法来替代默认的手势操作或只是承认他们?

我做的只是像谷歌开发者:
https://www.youtube.com/watch?v=naf_WbtFAlY
http://youtu.be/sha_w3_5c2c?t=25m1s
是不是有什么毛病模拟器? (我用32位模拟器:模拟器-avd AndroidWearXC -force了32bit

 进口android.app.Activity;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.GestureDetector;
进口android.view.MotionEvent;
公共类MainActivity延伸活动{
    私人GestureDetector mDetector;    私人静态字符串DEBUG_TAG =MainActivity;
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
        mDetector =新GestureDetector(这一点,新GestureDetector.SimpleOnGestureListener(){            @覆盖
            公共无效onLong preSS(MotionEvent事件){                Log.d(DEBUG_TAGonLong preSS:+ event.toString());
            }            @覆盖
            公共布尔onDown(MotionEvent事件){
                Log.d(DEBUG_TAGonDown:+ event.toString());
                返回true;
            }            @覆盖
            公共布尔onFling(MotionEvent EVENT1,MotionEvent EVENT2,
                                   浮velocityX,浮动velocityY){
                Log.d(DEBUG_TAGonFling:+ event1.toString()+ event2.toString());
                返回true;
            }            @覆盖
            公共布尔onScroll(MotionEvent E1,E2 MotionEvent,浮distanceX,
                                    浮动distanceY){
                Log.d(DEBUG_TAGonScroll:+ e1.toString()+ e2.toString());
                返回true;
            }            @覆盖
            公共无效昂秀preSS(MotionEvent事件){
                Log.d(DEBUG_TAG昂秀preSS:+ event.toString());
            }            @覆盖
            公共布尔onSingleTapUp(MotionEvent事件){
                Log.d(DEBUG_TAGonSingleTapUp:+ event.toString());
                返回true;
            }            @覆盖
            公共布尔onDoubleTap(MotionEvent事件){
                Log.d(DEBUG_TAGonDoubleTap:+ event.toString());
                返回true;
            }            @覆盖
            公共布尔onDoubleTapEvent(MotionEvent事件){
                Log.d(DEBUG_TAGonDoubleTapEvent:+ event.toString());
                返回true;
            }            @覆盖
            公共布尔onSingleTapConfirmed(MotionEvent事件){
                Log.d(DEBUG_TAGonSingleTapConfirmed:+ event.toString());
                返回true;
            }
        });    }
    @覆盖
    公共布尔onTouchEvent(MotionEvent EV){
        返回mDetector.onTouchEvent(EV)|| super.onTouchEvent(EV);
    }
}

我要刷卡左,右,和滚动UPP和向下的手势:

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


解决方案

在某一天我得到它的工作,失去的部分是,我必须禁用轻扫到辞退手势,并添加DismissOverlayView到我的布局,步骤:


  1. 禁用轻扫到辞退手势
    AppTheme风格添加到您的styles.xml并在mainfest使用它作为应用风格

styles.xml:

 <样式名称=AppTheme父=Theme.DeviceDefault>
    <项目名称=机器人:windowSwipeToDismiss>假LT; /项目>
    < /风格>

AndroidManifest.xml中:

 <应用
    机器人:allowBackup =真
    机器人:图标=@绘制/ ic_launcher
    机器人:标签=@字符串/ APP_NAME
    机器人:主题=@风格/ AppTheme>

<醇开始=2>

  • 添加到DissmissOverlayView主布局

     &LT; android.support.wearable.view.DismissOverlayView
        机器人:ID =@ + ID / dismiss_overlay
        机器人:layout_height =match_parent
        机器人:layout_width =match_parent/&GT;


  • 使用它在你的活动这样得到的手势

     公共类WearActivity延伸活动{
        私人DismissOverlayView mDismissOverlay;
        私人GestureDetector mDetector;    公共无效的onCreate(捆绑savedState){
            super.onCreate(savedState);
            的setContentView(R.layout.wear_activity);        //获取DismissOverlayView元素
            mDismissOverlay =(DismissOverlayView)findViewById(R.id.dismiss_overlay);
            mDismissOverlay.setIntroText(R.string.long_ press_intro);
            mDismissOverlay.showIntroIfNecessary();        //配置手势检测
            mDetector =新GestureDetector(这一点,新SimpleOnGestureListener(){        @覆盖
            公共无效onLong preSS(MotionEvent事件){            Log.d(DEBUG_TAGonLong preSS:+ event.toString());
            }        @覆盖
            公共布尔onDown(MotionEvent事件){
                Log.d(DEBUG_TAGonDown:+ event.toString());
                返回true;
            }        @覆盖
            公共布尔onFling(MotionEvent EVENT1,MotionEvent EVENT2,
                                   浮velocityX,浮动velocityY){
                Log.d(DEBUG_TAGonFling:+ event1.toString()+ event2.toString());
                返回true;
            }        @覆盖
            公共布尔onScroll(MotionEvent E1,E2 MotionEvent,浮distanceX,
                                    浮动distanceY){
                Log.d(DEBUG_TAGonScroll:+ e1.toString()+ e2.toString());
                返回true;
            }        @覆盖
            公共无效昂秀preSS(MotionEvent事件){
                Log.d(DEBUG_TAG昂秀preSS:+ event.toString());
            }        @覆盖
            公共布尔onSingleTapUp(MotionEvent事件){
                Log.d(DEBUG_TAGonSingleTapUp:+ event.toString());
                返回true;
            }        @覆盖
            公共布尔onDoubleTap(MotionEvent事件){
                Log.d(DEBUG_TAGonDoubleTap:+ event.toString());
                返回true;
            }        @覆盖
            公共布尔onDoubleTapEvent(MotionEvent事件){
                Log.d(DEBUG_TAGonDoubleTapEvent:+ event.toString());
                返回true;
            }        @覆盖
            公共布尔onSingleTapConfirmed(MotionEvent事件){
                Log.d(DEBUG_TAGonSingleTapConfirmed:+ event.toString());
                返回true;
            }
            });
        }    //捕获长presses
        @覆盖
        公共布尔onTouchEvent(MotionEvent EV){
            返回mDetector.onTouchEvent(EV)|| super.onTouchEvent(EV);
        }
        }


  • 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..

    解决方案

    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);
          } 
          }
      

    这篇关于如何实现在Android Wear的手势识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆