介紹下和手勢和多點觸摸相關的知識。。。。。。
先上個一道菜,手勢的識別。。。。。
- java.lang.Object
- ↳ Android.view.View
- ↳ android.view.ViewGroup
- ↳ android.widget.FrameLayout
- ↳ android.gesture.GestureOverlayView
介紹下GestureOverlayView,這個透明的view就是讓你在上面畫手勢用的,可疊在其他View上面。
Android的gesture的識別和自定義gesture源碼下載地址:
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /pub/Android源碼集錦/2011年/12月/Android的gesture的識別和自定義gesture/
和這個類相關的還有三個接口,分別是
GestureOverlayView.OnGestureListener;
GestureOverlayView.OnGesturePerformedListener(作用:根據在GestureOverlayView上畫的手勢來識別是否匹配手勢庫裡的手勢);
GestureOverlayView.OnGesturingListener.
GestureOverlayView的xml的屬性介紹:
android:gestureStrokeType
設置手勢的筆畫數,它有兩個值,GESTURE_STROKE_TYPE_MULTIPLE(多筆),GESTURE_STROKE_TYPE_SINGLE(一筆)
- public final class GestureLibraries
- static GestureLibrary fromFile(File path)
- static GestureLibrary fromFile(String path)
- static GestureLibrary fromPrivateFile(Context context, String name)
- static GestureLibrary fromRawResource(Context context, int resourceId)
想從SD卡或者raw的資源中直接加載手勢;
下面介紹下手勢的識別功能,先上代碼:
GestureIdentifyDemoActivity.xml
- package com.potato;
-
- import java.util.ArrayList;
-
- import android.app.Activity;
- import android.gesture.Gesture;
- import android.gesture.GestureLibraries;
- import android.gesture.GestureLibrary;
- import android.gesture.GestureOverlayView;
- import android.gesture.Prediction;
- import android.os.Bundle;
- import android.widget.Toast;
-
- 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.main);
-
- // 手勢畫板
- GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gesture_overlay_view_test);
- // 手勢識別的監聽器
- gestures.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() { // 注1
-
- @Override
- public void onGesturePerformed(GestureOverlayView overlay,
- Gesture gesture) {
- //從手勢庫中查詢匹配的內容,匹配的結果可能包括多個相似的結果,匹配度高的結果放在最前面
- ArrayList<Prediction> predictions = mGestureLib
- .recognize(gesture); // 注3
-
- 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();
- }
- }
-
- }
- });
- // 從raw中加載已經有的手勢庫
- mGestureLib = GestureLibraries.fromRawResource(this, R.raw.gestures); // 注2
- if (!mGestureLib.load()) {
- finish();
- }
-
- }
- }
注1:
- gestures.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener()
手勢識別的監聽器。。。。
注2:
- mGestureLib = GestureLibraries.fromRawResource(this, R.raw.gestures);
從res/raw/加載gestures手勢這個文件
注3:
- ArrayList<Prediction> predictions = mGestureLib.recognize(gesture);