Android Gesture 手势识别使用实例 - Android - mobile - ITeye论坛

主题:Android Gesture 手势识别使用实例 精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文

raymondlueng
等级: 一钻会员
raymondlueng的博客
性别: [男]
文章: 125
积分: 980
来自: 广州

发表时间:2009-10-20
< > 猎头职位: 陕西: 西安:senior java engineer
相关文章:

gesture手势相关操作
玩转Android---事件监听篇---第2篇
手写输入法学习


推荐群组: Android
更多相关推荐
Android

很高兴能在Android1.6的sdk看到手势识别这一功能,之前一直在想,如何在android中实现nds游戏那样用手势(准确点应该是笔势)来控制游戏角色?现在总算看到一点曙光了,不过手势要做到笔势那样随心所欲地控制游戏人物,还有很多细节问题需要处理。

在Android1.6的模拟器里面预装了一个叫Gestures Builder的程序,这个程序就是让你创建自己的手势的(Gestures Builder的源代码在sdk问samples里面有,有兴趣可以看看)。创建的手势将被保存到/sdcard/gestures里面,把这个文件复制到你的工程/res/raw下,你就可以在你的工程里面使用这些手势了。复制到/res/raw下的手势是只读的,也就是说你不能修改或增加手势了,如果想实现增改的话,可以直接加载sd卡里面的gestures文件。

在例子中,我创建了这样的手势:

第二步:在layout里面创建GestureOverlayView,这个透明的view就是让你在上面画手势用的,可以叠在其他View上面:

Xml代码 收藏代码

<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<android.gesture.GestureOverlayView
android:id="@+id/gestures"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1.0"
/>
</LinearLayout>

第三步:载入Gesture:

Java代码 收藏代码

mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (!mLibrary.load()) {
finish();
}

第四步:增加响应函数OnGesturePerformedListener:

Java代码 收藏代码

GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
gestures.addOnGesturePerformedListener(this);

以上四步就可以实现简单的Gesture识别原型了:

程序运行结果如下,书写一个a字,程序识别出,然后toast一个a出来:

完整代码如下:

Java代码 收藏代码

package com.ray.test;

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.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.os.Bundle;
import android.widget.Toast;

public class TestGesture extends Activity implements OnGesturePerformedListener{

GestureLibrary mLibrary;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
gestures.addOnGesturePerformedListener(this);
mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (!mLibrary.load()) {
finish();
}
}

@Override
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList predictions = mLibrary.recognize(gesture);

// We want at least one prediction
if (predictions.size() > 0) {
Prediction prediction = (Prediction) predictions.get(0);
// We want at least some confidence in the result
if (prediction.score > 1.0) {
// Show the spell
Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT).show();
}
}

}
}

文章参考了android博客上面的这篇文章:

http://feedproxy.google.com/~r/blogspot/hsDu/~3/Rrgh3YnIqig/gestures-on-android-16.html
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。

更多相关文章

  1. android 资料地址
  2. android中怎么把控件隐藏
  3. Android:控件的隐藏显示失效了
  4. android手动拖动滚动条快速滑动
  5. Android(安卓)Studio 2.2 正式起航
  6. 【Android】TabHost与RadioGroup结合完成的菜单
  7. Android(安卓)Studio 系列(二)使用Android(安卓)Studio 导入整个an
  8. Android(安卓)sms 发送、接收及格式
  9. SeekBar自定义

随机推荐

  1. android调用系统相机并调整照片大小保存,
  2. Android 继承SQLiteOpenHelper自定义DBHe
  3. Android(安卓)R.java类的手动生成
  4. Android——自定义Log显示
  5. Android最新资源官方下载地址
  6. android 网络语音电话合集 此文为备份
  7. Android Application - Painting
  8. Android Studio放大缩小字体
  9. some step in studying android
  10. Android(安卓)小应用之一个activity实现