歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android的gesture的識別和自定義gesture

Android的gesture的識別和自定義gesture

日期:2017/3/1 10:49:46   编辑:Linux編程
介紹下和手勢和多點觸摸相關的知識。。。。。。

先上個一道菜,手勢的識別。。。。。

  1. java.lang.Object
  2. ↳ Android.view.View
  3. ↳ android.view.ViewGroup
  4. ↳ android.widget.FrameLayout
  5. ↳ 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(一筆)

  1. public final class GestureLibraries
  1. static GestureLibrary fromFile(File path)
  2. static GestureLibrary fromFile(String path)
  3. static GestureLibrary fromPrivateFile(Context context, String name)
  4. static GestureLibrary fromRawResource(Context context, int resourceId)

想從SD卡或者raw的資源中直接加載手勢;


下面介紹下手勢的識別功能,先上代碼:

GestureIdentifyDemoActivity.xml

  1. package com.potato;
  2. import java.util.ArrayList;
  3. import android.app.Activity;
  4. import android.gesture.Gesture;
  5. import android.gesture.GestureLibraries;
  6. import android.gesture.GestureLibrary;
  7. import android.gesture.GestureOverlayView;
  8. import android.gesture.Prediction;
  9. import android.os.Bundle;
  10. import android.widget.Toast;
  11. public class GestureIdentifyDemoActivity extends Activity {
  12. // 手勢庫
  13. GestureLibrary mGestureLib;
  14. /** Called when the activity is first created. */
  15. @Override
  16. public void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.main);
  19. // 手勢畫板
  20. GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gesture_overlay_view_test);
  21. // 手勢識別的監聽器
  22. gestures.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() { // 注1
  23. @Override
  24. public void onGesturePerformed(GestureOverlayView overlay,
  25. Gesture gesture) {
  26. //從手勢庫中查詢匹配的內容,匹配的結果可能包括多個相似的結果,匹配度高的結果放在最前面
  27. ArrayList<Prediction> predictions = mGestureLib
  28. .recognize(gesture); // 注3
  29. if (predictions.size() > 0) {
  30. Prediction prediction = (Prediction) predictions.get(0);
  31. // 匹配的手勢
  32. if (prediction.score > 1.0) {
  33. Toast.makeText(GestureIdentifyDemoActivity.this,
  34. prediction.name, Toast.LENGTH_SHORT).show();
  35. }
  36. }
  37. }
  38. });
  39. // 從raw中加載已經有的手勢庫
  40. mGestureLib = GestureLibraries.fromRawResource(this, R.raw.gestures); // 注2
  41. if (!mGestureLib.load()) {
  42. finish();
  43. }
  44. }
  45. }
注1:
  1. gestures.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener()
手勢識別的監聽器。。。。

注2:

  1. mGestureLib = GestureLibraries.fromRawResource(this, R.raw.gestures);
從res/raw/加載gestures手勢這個文件

注3:

  1. ArrayList<Prediction> predictions = mGestureLib.recognize(gesture);
Copyright © Linux教程網 All Rights Reserved