温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

android手势

发布时间:2020-04-10 08:47:12 来源:网络 阅读:604 作者:秋寒526 栏目:移动开发

一丶首先创建一个手势库

二丶手势实例

  1. 布局文件:

    在布局文件中有:

    <android.gesture.GestureOverlayView
            android:id="@+id/gv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#000000"
            android:gestureStrokeWidth="10"
            android:gestureColor="#ff0000"
            />

  2. MainActivity中

    public class MainActivity extends Activity {
        
        private GestureOverlayView gv;//手势控件
        private GestureLibrary gestureLibrary;//加载手势库
        private boolean loadStatus;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //拿到控件
            gv = (GestureOverlayView) findViewById(R.id.gv);
          
     //创建加载手势库的工具
            gestureLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
            //加载手势库
            loadStatus = gestureLibrary.load();

            //给gv加一个监听器
            //监听一种手势
            gv.addOnGesturePerformedListener(new OnGesturePerformedListener() {
                
                @Override
                public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
                    //手势加载成功
                    if(loadStatus){
                        //识别手势 Prediction(手势相似度)
                        ArrayList<Prediction> pres = gestureLibrary.recognize(gesture);
                        if(!pres.isEmpty()){
                            Prediction pre = pres.get(0);
                            if(pre.score > 6){
                                if("close".equals(pre.name)){
                                    finish();
                                }else if("tel".equals(pre.name)){
                                    //打电话
                                    Intent intent  = new Intent();
                                    intent.setAction(Intent.ACTION_CALL);
                                    intent.setData(Uri.parse("tel://110"));
                                    startActivity(intent);
                                }else if("right".equals(pre.name)){
                                    Toast.makeText(getApplicationContext(), "正确", 0).show();
                                }
                            }else{
                                Toast.makeText(getApplicationContext(), "手势不匹配", 0).show();
                            }
                        }else{
                            Toast.makeText(MainActivity.this, "手势库加载失败", 0).show() ;
                        }
                        
                    }
                }
            });
        }
    }

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI