热门IT资讯网

android手势

发表于:2024-11-26 作者:热门IT资讯网编辑
编辑最后更新 2024年11月26日,一丶首先创建一个手势库二丶手势实例布局文件:在布局文件中有:MainActivity中public class MainActivity extends Activity {private Gestu

一丶首先创建一个手势库

二丶手势实例

  1. 布局文件:

    在布局文件中有:

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

    }
    }
    });
    }
    }

0