android基础--手势识别

 

第一步:建立手势库

使用SDK自带例子GestureBuilder建立手势库(位置:

android-sdk-windows\samples\android-8\GestureBuilder)。使用GestureBuilder之前,需要恢复其到开发环境,然后进行编绎并部署到手机上。此时,就可以使用GestureBuilder建立手势库,生成的手势库文件在SCDard上,默认文件名称为:gestures

第二步:在应用中加载手势库文件,然后开发手势识别代码。

把手势库文件gestures文件拷贝到项目的res/raw目录下。然后在布局文件中添加用于手势绘制的View:

 <android.gesture.GestureOverlayView

    android:id="@+id/gestures"

    android:layout_width="fill_parent“ android:layout_height="0dip"

    android:layout_weight="1.0" />

为View添加手势监听事件:gestureOverlayView.addOnGesturePerformedListener();

得到手势库:mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);

加载手势库:mLibrary.load();

List<Prediction> predictions = mLibrary.recognize(gesture);//从手势库中查询匹配的内容,匹配的结果可能包括多个相似的内容,匹配度高的结果放在最前面

大多数情况下,手势都是通过一笔完成。然而有一些特别的需求就需要通过多个笔画来实现,这时可以使用gestureStrokeType属性进行设置:android:gestureStrokeType="multiple"

public class MainActivity extends Activity {

       private GestureLibrary library;

       private Gesture mgeGesture;

       private GestureOverlayView overlayView;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        library = GestureLibraries.fromRawResource(this, R.raw.gestures);//找到手势库

        library.load();//加载手势库

        overlayView = (GestureOverlayView) this.findViewById(R.id.gestures);

//overlayView.addOnGesturePerformedListener(new GesturePerformedListener());//只针对单笔手势触发

        overlayView.addOnGestureListener(new GestureListener());

    }

    public void recognize(View v){

           myrecognize(mgeGesture);

           overlayView.clear(true);

    }

    private final class GestureListener implements OnGestureListener{

              public void onGestureStarted(GestureOverlayView overlay,

                            MotionEvent event) {

                     Log.i("MainActivity", "onGestureStarted");

              }

              public void onGesture(GestureOverlayView overlay, MotionEvent event) {

                     Log.i("MainActivity", "onGesture");

                    

              }

              public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {

                     Log.i("MainActivity", "onGestureEnded");

                     mgeGesture = overlay.getGesture();

              }

              public void onGestureCancelled(GestureOverlayView overlay,

                            MotionEvent event) {

                     Log.i("MainActivity", "onGestureCancelled");

              }

    }

       protected void onDestroy() {

              super.onDestroy();

              Process.killProcess(Process.myPid());//调用finish()方法关闭activity时调用onDestroy(),在onDestroy()中关闭进程

       }

       private final class GesturePerformedListener implements OnGesturePerformedListener{

              public void onGesturePerformed(GestureOverlayView overlay,Gesture gesture) {

                     myrecognize(gesture);

                    

              }

    }

       //手势识别

       private void myrecognize(Gesture gesture){

              ArrayList<Prediction> predictions = library.recognize(gesture);//在手势库中寻找匹配的手势,可以找到多条,最匹配的放在集合中的第一条

              if(!predictions.isEmpty()){

                     //手势相似度从0到10,例如:1表示相似度为10%,比较低,可以认为是匹配失败的

                     Prediction prediction = predictions.get(0);//得到集合中第一个,即最匹配的那个

                     if(prediction.score>6){

                            if("close".equals(prediction.name)){//关闭应用

                                   finish();//关闭activity

                            }else if("phone".equals(prediction.name)){//打电话

                                   Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:13800138000"));

                                   startActivity(intent);//激活意图所找到的组件

                            }

                     }else{//如果匹配度不到60%就认为时匹配失败

                            Toast.makeText(getApplicationContext(), R.string.low, 1).show();

                     }

              }else{

                     Toast.makeText(getApplicationContext(), R.string.error, 1).show();

              }

       }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值