如何使用gesture呢,如何判断手势。向上或先下运动呢,,最后有个案例,,让我分析一下,学习学习。。我弄不明白什么是gesture,这个如何使用。。
解决方案 »
- listview项的长按事件在真机里面没反应
- ??cocos2dx android游戏开发的好方式
- 请教如何配置每个activity一个进程
- 哪能下载到完整版安装的开发文件啊?东东西西的,都不好使。新版。
- 电脑上安装GOOGLE ADT后的问题
- 如何实现搜狐新闻客户端actionbar样式
- 浏览器实现无图模式怎么搞
- 配置AndroidStudio出现问题,求救
- 类似微信朋友圈的评论功能,为啥listview太长会把评论输入框挤出界面?
- 求大神:新建android项目时,总是出错,说是The import android.support cannot be resolved,不知道怎么解决。
- 请问一下多个activity 跳回主界面的解决方法 谢谢
- android开发点心桌面,Go桌面ex之类的软件
楼主可以直接用这个程序绘制gesture,生成的手势库文件在SCDard上,默认文件名称为:gestures
接下来在应用中加载手势库文件,然后开发手势识别代码。
把手势库文件gestures文件拷贝到你的项目的res/raw目录下。以下是我写的手势识别代码,楼主可以参考下
package com.litian.gesture;import java.util.List;import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private GestureOverlayView overlayView;
private GestureLibrary library;
private Gesture gesture;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//1.得到手势库(raw文件夹下的gestures文件)
library = GestureLibraries.fromRawResource(this, R.raw.gestures);
//2.加载手势库
library.load();
//3.获取绘图区域GestureOverlayView
overlayView = (GestureOverlayView)this.findViewById(R.id.gestures);
//4.为View添加手势监听事件
//只针对单笔绘制事件
//overlayView.addOnGesturePerformedListener(new MyGesturePerformedListener());
//针对多笔绘制事件
overlayView.addOnGestureListener(new MyOnGestureListener());
}
//处理按钮响应事件
public void find(View v){
if(gesture != null){
recognize(gesture);
}
overlayView.clear(true);
}
//手势识别
private void recognize(Gesture gesture) {
//从手势库中查询匹配的内容,匹配的结果可能包括多个相似的内容,匹配度高的结果放在最前面
List<Prediction> predictions = library.recognize(gesture); //predictions:预言,预言的事物
//开始判断匹配的结果
if(!predictions.isEmpty()){
Prediction prediction = predictions.get(0); //取第一个值(即最匹配的值)
if(prediction.score >= 6){
if("x".equals(prediction.name)){
//画XX
Toast.makeText(this, R.string.x, Toast.LENGTH_SHORT).show();
}else if("o".equals(prediction.name)){
//画O
Toast.makeText(this, R.string.x, Toast.LENGTH_SHORT).show();
}else if("cock".equals(prediction.name)){
//画小鸡鸡
Toast.makeText(this, R.string.cock, Toast.LENGTH_SHORT).show();
}else if("five".equals(prediction.name)){
//画五角星
Toast.makeText(this, R.string.five, Toast.LENGTH_SHORT).show();
}else if("ok".equals(prediction.name)){
//画勾
Toast.makeText(this, R.string.ok, Toast.LENGTH_SHORT).show();
}else if("three".equals(prediction.name)){
//画三角形
Toast.makeText(this, R.string.three, Toast.LENGTH_SHORT).show();
}else if("li".equals(prediction.name)){
//写“李”字
Toast.makeText(this, R.string.li, Toast.LENGTH_SHORT).show();
}else if("tian".equals(prediction.name)){
//写“田”字
Toast.makeText(this, R.string.tian, Toast.LENGTH_SHORT).show();
}
}else{
//匹配度过低
Toast.makeText(this, R.string.low, Toast.LENGTH_SHORT).show();
}
}else{
//匹配度过低
Toast.makeText(this, R.string.not_find, Toast.LENGTH_SHORT).show();
}
} //自定义手势监听事件内部类(多笔绘制)
private class MyOnGestureListener implements OnGestureListener{ @Override
public void onGestureStarted(GestureOverlayView overlay,
MotionEvent event) {
//Log.i(TAG, "onGestureStarted()");
} @Override
public void onGesture(GestureOverlayView overlay, MotionEvent event) {
//Log.i(TAG, "onGesture()");
} @Override
public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
//Log.i(TAG, "onGestureEnded()");
//监听手势结束事件
gesture = overlay.getGesture();
} @Override
public void onGestureCancelled(GestureOverlayView overlay,
MotionEvent event) {
//Log.i(TAG, "onGestureCancelled()");
}
}
//Gesture执行完成监听器(单笔绘制)
private class MyGesturePerformedListener implements OnGesturePerformedListener{ //Gesture执行完成事件
@Override
public void onGesturePerformed(GestureOverlayView overlay,
Gesture gesture) {
//Log.i(TAG, "onGesturePerformed()");
//监听此事件,gesture由系统传入
recognize(gesture);
}
} //销毁时做一些必要的处理
@Override
protected void onDestroy() {
super.onDestroy();
//myPid的解释:Returns the identifier of this process,
//which can be used with killProcess and sendSignal.
android.os.Process.killProcess(android.os.Process.myPid());
}
}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
另外,布局文件为:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<android.gesture.GestureOverlayView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gestureStrokeType="multiple"
android:id="@+id/gestures"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_label"
android:onClick="find"
/>
</LinearLayout>