一、概念
-
手勢:其實是指用戶手指或觸摸筆在屏幕上的連續觸碰行為,Andoird對兩種手勢行為都提供了支持:
-
Andorid提供了手勢檢測,並為手勢檢測提供了相應的監聽器;
-
Android允許開發者添加手勢,並提供了相應的API識別用戶手勢;
-
二、手勢檢測
-
Gesture類:代表了一個手勢檢測器;
-
GestureDectector.OnGestureListener類:代表一個監聽器、負責對用戶的手勢行為提供響應;
-
boolean onDown(MotionEvent e):當觸碰事件按下時觸發的方法;
-
boolean onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY):當用戶在觸摸屏上”拖過”時觸發該方法,velocityX,velocityY代表“拖過”動作的橫向、縱向上的速度;
-
abstract void onLongPress(MotionEvent e):當用戶在屏幕上長按時觸發該方法;
-
abstract void onScroll(MotionEvent e1,MotionEvent e2,float distanceX,float diastanceY):當用戶在屏幕上“滾動”時觸發該方法;
-
void onShowPress(MotionEvent e):當用戶在屏幕上按下,而且還未移動和松動的時候觸發該方法;
-
boolean onSingleTapUp(MotionEvent e):當用戶在觸摸屏上的輕擊事件將會觸發該方法;
-
三、使用步驟
-
創建一個GestureDetector對象,創建對象時候必須創建一個GestureDectector.OnGestureListener監聽器實例;
-
為應用程序的Activity(偶爾也可以為特定組件)的TouchEvent事件綁定監聽器,在事件處理中制定把Activity(或特定組件)上的TouchEvent事件交給GestureDetector處理;
-
使用實例(chapter08/GestureTest)
MainActivity.java文件
- public class MainActivity extends Activity implements OnGestureListener {
- // 定義手勢檢測實例
- GestureDetector detector;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main_activity);
- // 創建手勢檢測器
- detector = new GestureDetector(this, this);
- }
- @Override
- public boolean onTouchEvent(MotionEvent event) {
- // 將該Activity上的觸碰事件交給GestureDetector處理
- return detector.onTouchEvent(event);
- }
- @Override
- public boolean onDown(MotionEvent e) {
- // 觸碰時間按下時觸發該方法
- Toast.makeText(this, "OnDown", Toast.LENGTH_LONG).show();
- return false;
- }
- @Override
- public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
- float velocityY) {
- // 當用戶在屏幕上“拖動”時觸發該方法
- Toast.makeText(this, "onFling", Toast.LENGTH_LONG).show();
- return false;
- }
- @Override
- public void onLongPress(MotionEvent e) {
- // 當用戶在屏幕上長按時觸發該方法
- Toast.makeText(this, "onLongPress", Toast.LENGTH_LONG).show();
- }
- @Override
- public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
- float distanceY) {
- // 當屏幕“滾動”時觸發該方法
- Toast.makeText(this, "onScroll", Toast.LENGTH_LONG).show();
- return false;
- }
- @Override
- public void onShowPress(MotionEvent e) {
- // 當用戶在觸摸屏幕上按下、而且還未移動和松開時觸發該方法
- Toast.makeText(this, "onShowPress", Toast.LENGTH_LONG).show();
- }
- @Override
- public boolean onSingleTapUp(MotionEvent e) {
- // 在屏幕上的輕擊事件將會觸發該方法
- Toast.makeText(this, "onSingleTapUp", Toast.LENGTH_LONG).show();
- return false;
- }
- }
四、增加手勢
-
Android除了提供了手勢之外,還允許應用程序把用戶手勢(多個持續的觸摸事件在屏幕上形成特定的形狀)添加到制定文件中,以備以后使用;
-
GestureLibrary類:代表手勢庫,並提供了GestureLibraries工具來創建手勢庫,提供了如下4個靜態方法從不同的位置加載手勢:
-
static GestureLibrary from(String path):從path代表的文件中加載手勢庫;
-
static GestureLibrary fromFile(File path):從path代表的文件加載手勢庫;
-
static GestureLibrary fromPrivateFile(Context context,String name):從制定應用程序的數據文件夾中name文件中加載手勢庫;
-
static GestureLibrary fromRawResoure(Context context,int resourceId):從resourceId所代表的資源中加載手勢庫;
-
-
獲取GestureLibrary對象之后,該對象提供了如下方法來添加手勢和識別手勢:
-
void addGesture(String entryName,Gesture gesture):添加一個名為name的手勢;
-
Set<String> getGestureEntries():獲取手勢庫中所有的手勢名稱;
-
ArrayList<Guesture> getGestures(String entryName):獲取entryName名稱對應的全部手勢;
-
ArrayList<Prediction> recongize(Guesture gesture):從當前手勢庫中識別與gesture匹配的全部手勢;
-
void removeEntry(String entryName):刪除手勢庫中entryName對應的手勢;
-
void removeGesture(String entryName,Gesture gesture):刪除手勢庫中entryName,gesture對應的手勢庫;
-
boolean save():當向手勢庫中添加手勢或從中刪除手勢后調用該方法保存手勢庫;
-
-
為了監聽GestureOverlayView組件上的手勢事件,Android為GestureOverlayView提供了OnGestureLisnter、OnGesturePerformedListener、OnGesturingListener三個監聽器接口,分別用於響應手勢事件開始、結束、完成、取消事件;
-
使用實例(chapter08/AddGesture)
main_activity.xml文件
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center_horizontal"
- android:text="請在下面屏幕上繪制手勢" />
- <!-- android:gestureStrokeType手勢是否需要一筆完成-->
- <android.gesture.GestureOverlayView
- android:id="@+id/gesture"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:gestureStrokeType="multiple" />
- </LinearLayout>
sava.xml文件
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginRight="8dip"
- android:text="請輸入手勢名稱" />
- <!-- 定義一個文本框讓用戶輸入手勢名 -->
- <EditText
- android:id="@+id/gesture_name"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- </LinearLayout>
- <!-- 定義一個圖片框來顯示手勢 -->
- <ImageView
- android:id="@+id/show"
- android:layout_width="match_parent"
- android:layout_height="128dp"
- android:layout_marginTop="10dp"
- android:layout_weight="0.29" />
- </LinearLayout>
MainActivity.java文件
- public class MainActivity extends Activity {
- EditText editText;
- GestureOverlayView gestureOverlayView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main_activity);
- // 使用文本編輯器
- editText = (EditText) findViewById(R.id.gesture_name);
- // 獲取手勢編輯視圖
- gestureOverlayView = (GestureOverlayView) findViewById(R.id.gesture);
- // 設置手勢繪圖的顏色
- gestureOverlayView.setGestureColor(Color.RED);
- // 設置手勢的繪制寬度
- gestureOverlayView.setGestureStrokeWidth(4);
- // 為gesture的手勢完成事件綁定事件監聽器
- gestureOverlayView.addOnGesturePerformedListener(new OnGesturePerformedListener() {
- @Override
- public void onGesturePerformed(GestureOverlayView overlay,final Gesture gesture) {
- // 加載save.xml界面布局代表的視圖
- View saveDialog = getLayoutInflater().inflate(R.layout.save, null);
- // 獲取saveDialog里的show組件
- ImageView imageView = (ImageView) saveDialog.findViewById(R.id.show);
- // 獲取saveDialog的gesture_name組件
- final EditText gestureEditText = (EditText) saveDialog.findViewById(R.id.gesture_name);
- // 根據Gesture包含的手勢創建一個位圖
- Bitmap bitmap = gesture.toBitmap(128, 128, 10,0xffff0000);
- imageView.setImageBitmap(bitmap);
- // 使用對話框顯示saveDialog組件
- new AlertDialog.Builder(MainActivity.this).setView(saveDialog).setPositiveButton("保存", new OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog,int which) {
- // 獲取制定文件對應的手勢庫
- GestureLibrary guestureLibrary = GestureLibraries.fromFile(Environment
- .getExternalStorageDirectory().getPath()+ "/mygestures");
- // 添加手勢
- guestureLibrary.addGesture(gestureEditText.getText().toString(), gesture);
- guestureLibrary.save();
- }
- }).setNegativeButton("取消", null).show(); }
- });
- }
- }
五、識別用戶手勢
-
recoginze(Gesture guesture)方法:識別手勢,該方法將會返回該手勢庫中所有與ges匹配的手勢—兩個手勢的圖形越相似,相似度越高;
-
recogniza(Gusture ges)方法返回為ArrayList<Prediction>,啟動Prediction封裝了手勢的匹配信息,Predictin對象的name屬性代表了匹配的手勢名,score屬性代表了手勢的相似度;
-
應用實例(/chapter08/RecognizeGesture)
main_activity.xml文件:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <android.gesture.GestureOverlayView
- android:id="@+id/gesture"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:gestureStrokeType="multiple" />
- </LinearLayout>
MainActivity.java文件
- public class MainActivity extends Activity {
- // 定義手機編輯組件
- GestureOverlayView gestureOverlayView;
- // 記錄手機上已有的手勢庫
- GestureLibrary gestureLibrariLibrary;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main_activity);
- gestureOverlayView = (GestureOverlayView) findViewById(R.id.gesture);
- gestureLibrariLibrary = GestureLibraries.fromFile(Environment.getExternalStorageDirectory().getPath() + "/mygestures");
- if (gestureLibrariLibrary.load()) {
- Toast.makeText(MainActivity.this, "手勢文件裝在成功", Toast.LENGTH_LONG).show();
- } else {
- Toast.makeText(MainActivity.this, "手勢文件裝在失敗", Toast.LENGTH_LONG).show();
- }
- // 定義手勢編輯組件綁定監聽器
- gestureOverlayView.addOnGesturePerformedListener(new OnGesturePerformedListener() {
- @Override
- public void onGesturePerformed(GestureOverlayView overlay,Gesture gesture) {
- // 識別用戶剛剛所繪制的手勢
- ArrayList<Prediction> predictions = gestureLibrariLibrary.recognize(gesture);
- ArrayList<String> result = new ArrayList<String>();
- // 遍歷所有找到的Prediction對象
- for (Prediction prediction : predictions) {
- // 只有相似度大於0.2的手勢才會被輸出
- if (prediction.score > 0.2) {
- result.add("與手勢{" + prediction.name + "}相似度為:"+ prediction.score);
- }
- }
- if (result.size() > 0) {
- ArrayAdapter<Object> arrayAdapter = new ArrayAdapter<Object>(MainActivity.this,
- android.R.layout.simple_dropdown_item_1line,
- result.toArray());
- new AlertDialog.Builder(MainActivity.this).setAdapter(arrayAdapter, null).setPositiveButton("確定", null).show();
- } else {
- Toast.makeText(MainActivity.this, "無法能找到匹配的手勢",
- Toast.LENGTH_LONG).show();
- }
- }});
- }
- }