增加手势和识别手势

来源:互联网 发布:catia v6软件下载 编辑:程序博客网 时间:2024/04/30 07:59

gestureoverlayview组件是Android提供给开发者所使用绘制手势的控件,但是这个控件不是标准的控件需要使用全名,android.gesture.GestureOverlayView。

下方代码是增加手势和识别手势的实例代码:

一、增加手势

1,xml界面文件

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <TextView        android:id="@+id/textview"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="请在下方绘制手势" />    <!-- 非绘图组件,而是绘制手势的一个组件。他不是标准的绘图组件,使用全名 -->    <android.gesture.GestureOverlayView        android:id="@+id/gesture"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:gestureStrokeType="single" ><!-- 是否需要一笔完成,multiple(多笔) -->    </android.gesture.GestureOverlayView></RelativeLayout>--save.xml--<pre name="code" class="java">  <LinearLayout android:orientation="horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <TextView android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginRight="8dip"            android:text="MyGesture"/>        <EditText android:id="@+id/gesture_name"            android:layout_width="match_parent"            android:layout_height="wrap_content"/>    </LinearLayout>    <ImageView android:id="@+id/show"        android:layout_width="128dp"        android:layout_height="128dp"        android:layout_marginTop="10dp"/>

JAVA 代码:

package com.example.gesturesave;import android.annotation.SuppressLint;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.content.DialogInterface.OnClickListener;import android.gesture.Gesture;import android.gesture.GestureLibraries;import android.gesture.GestureLibrary;import android.gesture.GestureOverlayView;import android.gesture.GestureOverlayView.OnGesturePerformedListener;import android.graphics.Bitmap;import android.graphics.Color;import android.os.Bundle;import android.view.View;import android.widget.EditText;import android.widget.ImageView;import android.widget.Toast;@SuppressLint("SdCardPath")public class MainActivity extends Activity {EditText editText;GestureOverlayView gestureOverlayView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);editText = (EditText) findViewById(R.id.gesture_name);gestureOverlayView = (GestureOverlayView) findViewById(R.id.gesture);gestureOverlayView.setGestureColor(Color.RED);gestureOverlayView.setGestureStrokeWidth(4);gestureOverlayView.addOnGesturePerformedListener(new OnGesturePerformedListener() {@Overridepublic void onGesturePerformed(GestureOverlayView arg0,final Gesture gesture) {View saveDialogView = getLayoutInflater().inflate(R.layout.save, null);ImageView imageView = (ImageView) saveDialogView.findViewById(R.id.show);final EditText gestureName = (EditText) saveDialogView.findViewById(R.id.gesture_name);Bitmap bitmap = gesture.toBitmap(128, 128, 0,0xffff0000);imageView.setImageBitmap(bitmap);new AlertDialog.Builder(MainActivity.this).setView(saveDialogView).setPositiveButton("保存", new OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int which) {// 核心代码//获取指定文件对应的手势库GestureLibrary gestureLibrary = GestureLibraries.fromFile("/sdcard/mygesture");//手势库gestureLibrary.addGesture(gestureName.getText().toString(), gesture);// 保存手势库Toast.makeText(MainActivity.this,""+gestureLibrary.save(), Toast.LENGTH_SHORT).show();//MainActivity.this.startActivity(new Intent(MainActivity.this, MainActivityLoad.class));}}).setNegativeButton("取消", null).show();}});}}
这样就可以向手机内部写入一个手势。手势是一个文件,手势是不能通过数据库的形式存储的。

二、识别手势

main.xml:

    <android.gesture.GestureOverlayView
        android:id="@+id/gesture"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </android.gesture.GestureOverlayView>

JAVA代码:

package com.example.gesturesave;import java.util.ArrayList;import android.os.Bundle;import android.annotation.SuppressLint;import android.app.Activity;import android.app.AlertDialog;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.view.Menu;import android.widget.ArrayAdapter;import android.widget.Toast;@SuppressLint("SdCardPath")public class MainActivityLoad extends Activity {GestureOverlayView gestureOverlayView;GestureLibrary gestureLibrary;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);gestureLibrary = GestureLibraries.fromFile("/sdcard/mygesture");if (gestureLibrary.load()) {Toast.makeText(this, "加载成功", 8000).show();} else {Toast.makeText(this, "加载失败", 8000).show();}gestureOverlayView = (GestureOverlayView) findViewById(R.id.gesture);gestureOverlayView.addOnGesturePerformedListener(new OnGesturePerformedListener() {@Overridepublic void onGesturePerformed(GestureOverlayView overlayView, Gesture gesture) {ArrayList<Prediction> predictions = gestureLibrary.recognize(gesture);ArrayList<String> result = new ArrayList<String>();for (Prediction pred : predictions) {if (pred.score > 2.0) {result.add(" 与" + pred.name + "相似度为"+ pred.score);}if (result.size() > 0) {ArrayAdapter adapter = new ArrayAdapter(MainActivityLoad.this,android.R.layout.simple_dropdown_item_1line,result.toArray());new AlertDialog.Builder(MainActivityLoad.this).setAdapter(adapter, null).setPositiveButton("保存", null).show();}else{Toast.makeText(MainActivityLoad.this, "没有找到匹配的手势", 8000).show();}}}});}}

当然,这里面用到了一些需要得到访问权限的东西,所以还需要向manifest文件中添加一些权限。

<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

这样你就可以增加和识别手势了。

0 0
原创粉丝点击