展会信息港展会大全

Android Gesture手势识别
来源:互联网   发布日期:2016-01-14 12:32:44   浏览:2484次  

导读:在layout里面创建GestureOverlayView,这个透明的view就是让你在上面画手势用的,可叠在其他View上面:?xml version=1.0 encoding=utf-8? LinearLayout xmlns:android=http://schemas.android.com/a......

在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);

-------------------------------------------------------------------------------

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();

}

}

}

}

赞助本站

人工智能实验室

相关热词: Gesture 手势

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港