GestureOverlayView手势库的应用

来源:互联网 发布:mac地址重复 后果 编辑:程序博客网 时间:2024/06/07 11:50

1、手势布局可以覆盖和包含其他控件

 <android.gesture.GestureOverlayView        android:id="@+id/gesture_overlay_view_test"        android:layout_width="match_parent"        android:layout_height="fill_parent"        android:eventsInterceptionEnabled="false"        android:fadeDuration="10"        android:gestureStrokeType="multiple"        android:gestureStrokeWidth="1" >        <TextView            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:text="@string/hello"            android:textSize="20sp" />        </Button>    </android.gesture.GestureOverlayView>

2、为手势库初始化值:

public class GestureBuilderDemoActivity extends Activity {    private static GestureLibrary mGestureLib;    // 存放手势的文件    private final File mStoreFile = new File(Environment.getExternalStorageDirectory(), "gestures");    Button mBtnIdentify;    static GestureLibrary getStore() {        return mGestureLib;    }    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        if (mGestureLib == null) {            mGestureLib = GestureLibraries.fromFile(mStoreFile); // 注1        }

3、使用手势布局

}public class GestureIdentifyDemoActivity extends Activity {    // 手势库    GestureLibrary mGestureLib;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.gesture_identify);        // 手势画板        GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gesture_overlay_view_test);        // 手势识别的监听器        gestures.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() {            @Override            public void onGesturePerformed(GestureOverlayView overlay,                    Gesture gesture) {                //从手势库中查询匹配的内容,匹配的结果可能包括多个相似的结果,匹配度高的结果放在最前面                  ArrayList<Prediction> predictions = mGestureLib                        .recognize(gesture);                if (predictions.size() > 0) {                    Prediction prediction = (Prediction) predictions.get(0);                    // 匹配的手势                    if (prediction.score > 1.0) {                        Toast.makeText(GestureIdentifyDemoActivity.this,                                prediction.name, Toast.LENGTH_SHORT).show();                    }                }            }        });        //为手势库再初始化。否则会奔溃        mGestureLib = GestureBuilderDemoActivity.getStore();        // 从raw中加载已经有的手势库        //mGestureLib = GestureLibraries.fromRawResource(this, R.raw.gestures);//      if (!mGestureLib.load()) {//          finish();//      }    }}

4、如果没有为手势库赋值,会报错:
at com.potato.MytestActivity$1.onGesturePerformed(MytestActivity.java:34)

0 0
原创粉丝点击