xml文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.android_gestureoverlayview.MainActivity" > <android.gesture.GestureOverlayView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/gestureOverlayView"
>
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher"/>
</android.gesture.GestureOverlayView>
</RelativeLayout>
源代码:
package com.example.android_gestureoverlayview; 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.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
/**
* GestureOverlayView:
* 1.使用安卓自带的文件Gestures Builder生成手势文件(D:\JDK\Android SDK\samples\android-15\GestureBuilder)
* Gestures Builder一般在sdk中(生成手势文件)
* 1.1.导入:File-New-Android-Android Sample project
* 1.2.安装:将该项目安装到模拟器中,进行手势的自定义添加
* 1.3.在虚拟机的mnt/sdcard--(shell/emulated/0)中找到gestures文件,该文件包含自定义手势,将文件导出
* 2.加入项目
* 2.1新建一个文件夹res//raw,将gestures文件加入
* 3.GestureOverlayView
* 3.1一种手势输入的透明覆盖层,可覆盖在其他控件的上方,也可包含其他控件,存在3个监听器接口
* GestureOverlayView.OnGestureListener 手势监听器
* GestureOverlayView.OnGesturePerformedListener 手势执行监听器
* GestureOverlayView.OnGesturingListener 手势执行中监听器
*
*
*/
public class MainActivity extends Activity {
private GestureOverlayView gestureOverlayView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gestureOverlayView = (GestureOverlayView) findViewById(R.id.gestureOverlayView);
/**
* 执行以下步骤:
* 1.找到刚才的预设定的手势文件
* 2.加载那个手势文件中的所有手势
* 3.匹配 识别
*/
//从资源中将手势库文件加载进来
final GestureLibrary library = GestureLibraries.fromRawResource(MainActivity.this, R.raw.gestures);
library.load();
//GestureOverlayView.OnGesturePerformedListene 手势执行监听器
gestureOverlayView.addOnGesturePerformedListener(new OnGesturePerformedListener() { @Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
// TODO Auto-generated method stub
//读出手势库内容 识别手势 Prediction 预测
ArrayList<Prediction> mygesture = library.recognize(gesture);
Prediction prediction = mygesture.get(0);
//prediction 里面有两个参数 name,score(0.0-10)(相似度)值越大,要求越严格
if(prediction.score >= 5.0){
if(prediction.name.equals("exit")){
System.exit(0);
}else if(prediction.name.equals("privious")){
Toast.makeText(MainActivity.this, "播放上一首", Toast.LENGTH_SHORT).show();
}else if(prediction.name.equals("next")){
Toast.makeText(MainActivity.this, "播放下一首", Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(MainActivity.this, "没有该手势", 1).show();
}
}
}); } }