1、在res文件夹中创建子文件夹,导入手势文件
2、布局文件
<LinearLayout 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:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="绘制手势"
android:textColor="@android:color/black"
android:textSize="20dp"/>
<android.gesture.GestureOverlayView
android:id="@+id/gestures"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1.0" >
</android.gesture.GestureOverlayView>
</LinearLayout>
3、MainActivity.java继承Activity类并实现OnGesturePerformedListener接口
public class MainActivity extends Activity implements OnGesturePerformedListener{
private GestureLibrary library;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
library = GestureLibraries.fromRawResource(this,R.raw.gestures);//加载手势文件
if(!library.load()){//如果加载失败则退出
finish();
}
GestureOverlayView gesture = (GestureOverlayView)findViewById(R.id.gestures);
gesture.addOnGesturePerformedListener(this);//增加监听器
}
@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList<Prediction> gestures = library.recognize(gesture);//获得全部预测结果
int index = 0;//保存当前预测的索引号
double score = 0.0;//保存当前预测的得分
for (int i = 0; i < gestures.size(); i++) {//获得最佳匹配结果
Prediction result = gestures.get(i);//获得一个预测结果
if(result.score>score){
index = i;
score = result.score;
}
}
Toast.makeText(this, gestures.get(index).name, Toast.LENGTH_SHORT).show();
}
}