使用一些瀏覽器或者輸入法應用時會有一些手勢操做,還能夠自定義手勢。這些神奇的操做是怎麼作的呢?這一篇重點記錄手勢的識別和建立。這篇的內容使用到的是android.gesture包,具體的例子參考的是Sample中GestureBuilder程序。html
一、手勢建立
手勢建立主要用到GestureOverlayView和GestureLibrary。GestureOverlayView的父類爲android.widget.FrameLayout,是手勢繪圖區。GestureLibrary類主要對手勢進行保存、刪除等操做的,存放手勢的倉庫。下面給出建立手勢的例子,以下圖,能夠定義如圖手勢打開csdn.netjava
1.一、建立繪圖區
- GestureOverlayView overlay = (GestureOverlayView) findViewById(R.id.gestures_overlay);
-
- overlay.setGestureStrokeType(GestureOverlayView.GESTURE_STROKE_TYPE_MULTIPLE);
-
- overlay.setFadeOffset(2000);
- overlay.setGestureColor(Color.CYAN);
- overlay.setGestureStrokeWidth(6);
-
- overlay.addOnGestureListener(new GesturesProcessor());
1.二、監聽繪製
- private class GesturesProcessor implements GestureOverlayView.OnGestureListener {
- public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
- mDoneButton.setEnabled(false);
- mGesture = null;
- }
-
- public void onGesture(GestureOverlayView overlay, MotionEvent event) {
- }
-
- public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
- mGesture = overlay.getGesture();
- if (mGesture.getLength() < LENGTH_THRESHOLD) {
- overlay.clear(false);
- }
- mDoneButton.setEnabled(true);
- }
-
- public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
- }
- }
1.三、保存手勢
- public void addGesture(View v) {
- if (mGesture != null) {
- final TextView input = (TextView) findViewById(R.id.gesture_name);
- final CharSequence name = input.getText();
- if (name.length() == 0) {
- input.setError(getString(R.string.error_missing_name));
- return;
- }
-
-
-
-
-
-
-
- final GestureLibrary store = GestureBuilderActivity.getStore();
- store.addGesture(name.toString(), mGesture);
- store.save();
-
- setResult(RESULT_OK);
-
- final String path = new File(Environment.getExternalStorageDirectory(),
- "gestures").getAbsolutePath();
- Toast.makeText(this, getString(R.string.save_success, path), Toast.LENGTH_LONG).show();
- } else {
- setResult(RESULT_CANCELED);
- }
-
- finish();
-
- }
二、手勢識別
手勢識別也是通過建立繪圖區、監聽繪製、比對結果三個過程,這裏直接給出代碼。
- public class GesturePerformedActivity extends Activity {
-
- private final File mStoreFile = new File(
- Environment.getExternalStorageDirectory(), "gestures");
-
-
- GestureLibrary mGestureLib;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
-
- super.onCreate(savedInstanceState);
-
- setContentView(R.layout.gesture_perform);
-
- GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures_overlay);
-
- gestures.setGestureStrokeType(GestureOverlayView.GESTURE_STROKE_TYPE_MULTIPLE);
-
- gestures.setFadeOffset(2000);
- gestures.setGestureColor(Color.CYAN);
- gestures.setGestureStrokeWidth(6);
-
-
- 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(GesturePerformedActivity.this,
- prediction.name, Toast.LENGTH_SHORT).show();
- }
- }
- }
- });
-
- if (mGestureLib == null) {
- mGestureLib = GestureLibraries.fromFile(mStoreFile);
- mGestureLib.load();
- }
- }
-
- }
示例下載android
* iOS入門羣:83702688
* android開發進階羣:241395671
*/
最後還有點問題,就是多筆畫識別問題,這裏沒有很好的解決,使用OnGesturePerformedListener接聽結束,有的多筆畫會識別失敗,尤爲簡單的橫線豎線組合,複雜的卻沒有問題。若是使用GestureOverlayView.OnGestureListener監聽結束,每一筆畫都會調用,不能處理未知筆畫數的識別。若是哪位同窗完美解決了多筆畫問題,請多指教,謝謝。瀏覽器