识别的区域为GestureOverlayView的区域 ,GestureOverlayView包裹其他控件。
第一步。需要先在sdk 的simple中找一个版本,然后导入其中的GestureBuilder工程,并运行工程,添加手势 。然后再该工程的内存卡中取出生成的手势文件gesture
第二步,将生成的手势文件gesture导入你需要用的工程的res 下的raw文件夹 。然后编写代码
第三步,编写代码
public class GestureOverlayViwActivity extends Activity {
private GestureOverlayView gestureOverlayView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gesture_overlay_viw);
gestureOverlayView=(GestureOverlayView) findViewById(R.id.gestureOverlayView1);
//1.找到预设定的手势文件
//2.加载那个手势文件中的所有手势
//3.匹配识别
//从资源中将收拾文件加载进来
final GestureLibrary library=GestureLibraries.fromRawResource(GestureOverlayViwActivity.this, R.raw.gestures);
library.load();
gestureOverlayView.addOnGesturePerformedListener(new OnGesturePerformedListener() {
@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
//读出手势,识别手势
ArrayList<Prediction> mygesture=library.recognize(gesture);
Prediction prediction =mygesture.get(0);//prediction 为取出的手势
if(prediction.score>=7.0){//5.0为相似度
if(prediction.name.equals("exid")){
finish();
}if(prediction.name.equals("next")){
Toast.makeText(GestureOverlayViwActivity.this, "播放下一首", Toast.LENGTH_SHORT).show();
}if(prediction.name.equals("perivous")){
Toast.makeText(GestureOverlayViwActivity.this, "播放上一首", Toast.LENGTH_SHORT).show();
}
}else
{
Toast.makeText(GestureOverlayViwActivity.this, "没有该手势", Toast.LENGTH_SHORT).show();
}
}
});
}
}
activity_gesture_overlay_viw.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"
tools:context="com.example.gesturedetectordemo.GestureOverlayViwActivity" >
<android.gesture.GestureOverlayView
android:id="@+id/gestureOverlayView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
>
<ImageView
android:id="@+id/img2"
android:layout_width="fill_parent"
android:layout_height="461dp"
android:background="@drawable/image1" />
</android.gesture.GestureOverlayView>
</RelativeLayout>