Android-自定義手勢識別-複雜手勢識別
一、概述
1、手勢互動過程(原理)
(1)觸屏一剎那,觸發MotionEvent事件
(2)被OnTounchListener監聽,在onTouch()中獲得MotionEvent物件
(3)GestureDetector轉發MotionEvent物件至OnGestureListener.
2、MotionEvent:
(1)用於封裝手勢、觸控筆、規則球等動作事件
(2)內部封裝用於記錄橫軸和縱軸座標的屬性X和Y
3、GestureDetector:
(1)識別各種手勢
(2)觸控式螢幕:按下、移動、擡起等
<1>過載onTouch或者設定setOnTouchListener
(3
<1>當接收到使用者觸控訊息時,將訊息交給GestureDetector加工
<2>通過設定監聽器獲得GestureDetector處理後的手勢
4、監聽器:
OnGestureListener:處理單擊類訊息
單擊:onDown(MotionEvent e)
擡起:onSingleTapUp(MotionEvent e)
短按:onShowPress(MotionEvent e)
長按:onLongPress(MotionEvent e)
滾動;onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
滑動:onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
OnDoubleTapListener:處理雙擊類訊息
雙擊:onDoubleTap(MotionEvent e)
雙擊按下和擡起各觸發一次:onDoubleTapEvent(MotionEvent e)
單擊確認:onSingleTapConfirmed(MotionEvent e)
5、GestureOverlayView:
一種用於手勢輸入的透明覆蓋層,可覆蓋在其他控制元件的上方,也可包含其他控制元件。存在3個監聽器介面:
GestureOverlayView.OnGestureListener 手勢監聽器
GestureOverlayView.OnGesturePerformedListener 手勢執行監聽器
GestureOverlayView.OnGesturingListener 手勢執行中監聽器
一些常見的XML屬性設定:
android:eventsInterceptionEnabled 當手勢已經被識別出來時,是否攔截該手勢動作
android:fadeDuration 當用戶畫完手勢效果淡出的時間
android:fadeEnabled 使用者畫完之後手勢是否自動淡出
android:gestureColor=”#000000” 手勢的顏色
android:gestureStrokeType=”multiple” 筆畫的型別
android:gestureStrokeWidth=”2dp” 筆畫的粗細
使用步驟:
(1)使用Gesture Builder 應用程式生成手勢檔案
(2)匯入生成的手勢檔案
(3)匹配識別手勢
二、簡單手勢GestureDetector的使用
<pre name="code" class="java"><span style="font-size:18px;">package com.sqb.demo8;
import com.sqb.app_im3.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.Toast;
public class Activity8Gesture extends Activity{
private ImageView img;
private GestureDetector myGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout8gesture);
img = (ImageView) findViewById(R.id.iv8_1);
//myGestureDetector = new GestureDetector(new myGestureListener());//過時了
myGestureDetector = new GestureDetector(this,new myGestureListener());
img.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
myGestureDetector.onTouchEvent(event);
return true;
}
});
}
class myGestureListener extends SimpleOnGestureListener{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if(e1.getX()-e2.getX() >50){
Toast.makeText(Activity8Gesture.this, "從右向左滑", Toast.LENGTH_SHORT).show();
}else if(e2.getX()- e1.getX() >50){
Toast.makeText(Activity8Gesture.this, "從左向右滑", Toast.LENGTH_SHORT).show();
}
return super.onFling(e1, e2, velocityX, velocityY);
}
}
}</span>
三、複雜手勢GestureOverlayView的使用
1、在Eclipse建立內建的simple demo
(1)File-->new-->Other-->Android-->Android Sample Project-->Build Target 2.3.3也行-->GestureBuilder,然後Eclipse裡面就多了一個GestureBuilder專案了,把專案執行到虛擬機器上。
2、建立手勢檔案並匯入到新的專案中:
(1)開啟模擬器,開啟剛才從Eclipse編譯安裝的Gesture Builder軟體
(2)點選Add gesture新建手勢
(3)將手勢識別庫從sdcard卡中匯出,在res檔案下新建raw檔案,將匯出的gesture檔案貼上到raw檔案下
3、編寫程式碼:
(1)Activity類:
package com.sqb.demo8;
import java.util.ArrayList;
import com.sqb.app_im3.R;
import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.os.Bundle;
import android.widget.Toast;
public class Activity8GestureOverlayView extends Activity{
private GestureOverlayView gov;
private GestureLibrary gl;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout8gestureoverlayview);
//從資原始檔中將手勢庫載入進來
gl = GestureLibraries.fromRawResource(this, R.raw.gestures);
gl.load();
//從xml中取出GestureOverlayView控制元件
gov = (GestureOverlayView) findViewById(R.id.gestureOverlayView1);
//為GestureOverlayView控制元件新增監聽
gov.addOnGesturePerformedListener(new OnGesturePerformedListener() {
@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
//識別手勢,返回一個型別為Prediction的列表
ArrayList<Prediction> list = gl.recognize(gesture);
Prediction pre = list.get(0);
//如果匹配度大於1,表示可以識別,否則提示無法識別
if(pre.score > 1){
//判斷名字是否與手勢庫的名字相同
if(pre.name.equals("haha")){
Toast.makeText(Activity8GestureOverlayView.this, "Recognize the haha", Toast.LENGTH_SHORT).show();
}else if(pre.name.equals("xin")){
Toast.makeText(Activity8GestureOverlayView.this, "Recognize the xin", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(Activity8GestureOverlayView.this, "Can't Recognize", Toast.LENGTH_SHORT).show();
}
}
});
}
}
(2)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/gestureOverlayView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</android.gesture.GestureOverlayView>
</LinearLayout>
參考資料:
GestureOverlayView手勢識別:http://blog.csdn.net/zlqqhs/article/details/8541507