源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android实现Gesture手势识别用法分析

  • 时间:2021-04-05 04:25 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android实现Gesture手势识别用法分析
本文实例分析了Android实现Gesture手势识别用法。分享给大家供大家参考。具体如下: 很高兴能在Android1.6的sdk看到手势识别这一功能,之前一直在想,如何在android中实现nds游戏那样用手势(准确点应该是笔势)来控制游戏角色?现在总算看到一点曙光了,不过手势要做到笔势那样随心所欲地控制游戏人物,还有很多细节问题需要处理。 在Android1.6的模拟器里面预装了一个叫Gestures Builder的程序,这个程序就是让你创建自己的手势的(Gestures Builder的源代码在sdk问samples里面有,有兴趣可以看看)。创建的手势将被保存到/sdcard/gestures里面,把这个文件复制到你的工程/res/raw下,你就可以在你的工程里面使用这些手势了。复制到/res/raw下的手势是只读的,也就是说你不能修改或增加手势了,如果想实现增改的话,可以直接加载sd卡里面的gestures文件。 在例子中,我创建了这样的手势: [img]http://files.jb51.net/file_images/article/201609/2016915235820623.png?2016815235858[/img] 第二步:在layout里面创建GestureOverlayView,这个透明的view就是让你在上面画手势用的,可以叠在其他View上面:
<?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:
mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (!mLibrary.load()) {
  finish();
}
第四步:增加响应函数OnGesturePerformedListener:
GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
gestures.addOnGesturePerformedListener(this);
以上四步就可以实现简单的Gesture识别原型了: 程序运行结果如下,书写一个a字,程序识别出,然后toast一个a出来: [img]http://files.jb51.net/file_images/article/201609/2016915235903291.png?2016815235934[/img] 完整代码如下:
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相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/367.htm]Android编程之activity操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/375.htm]Android视图View技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/848.htm]Android操作SQLite数据库技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/845.htm]Android操作json格式数据技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/402.htm]Android数据库操作技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/325.htm]Android文件操作技巧汇总[/url]》、《[url=http://www.1sucai.cn/Special/224.htm]Android编程开发之SD卡操作方法汇总[/url]》、《[url=http://www.1sucai.cn/Special/410.htm]Android开发入门与进阶教程[/url]》、《[url=http://www.1sucai.cn/Special/423.htm]Android资源操作技巧汇总[/url]》及《[url=http://www.1sucai.cn/Special/124.htm]Android控件用法总结[/url]》 希望本文所述对大家Android程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部