Android增加、识别用户手势

Android增加、识别用户手势_第1张图片

实例1:增加手势


activity_main.xml

<LinearLayout 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"
    tools:context=".MainActivity" 
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请在下方输入手势" />
    
    <android.gesture.GestureOverlayView 
        android:id="@+id/gesture"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gestureStrokeType="multiple"/>

</LinearLayout>

弹出的增加手势的对话框

save.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        
        <TextView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="填写手势名"/>
        
        <EditText android:id="@+id/gesture_name"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>
        
    </LinearLayout>
    
    <!-- 定义一个图片框来显示手势 -->
    <ImageView android:id="@+id/show"
        android:layout_width="128dp"
        android:layout_height="128dp"
        android:layout_marginTop="10dp"/>

</LinearLayout>

MainActivity.java

package com.example.addgesture;


import java.io.File;
import java.io.IOException;


import android.os.Bundle;
import android.os.Environment;
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.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;


public class MainActivity extends Activity {


EditText editText;
GestureOverlayView gestureView;
String filePath="/sdcard/gestures";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

gestureView=(GestureOverlayView) super.findViewById(R.id.gesture);

//设置手势的绘制颜色
gestureView.setGestureColor(Color.RED);
//设置手势的绘制宽度
gestureView.setGestureStrokeWidth(4);
//为gesture手势完成事件绑定监听器
gestureView.addOnGesturePerformedListener(new OnGesturePerformedListener() {

@Override
public void onGesturePerformed(GestureOverlayView overlay, final Gesture gesture) {
//LayoutInflater作用是将layout的xml布局文件实例转化为View类对象
//加载save.xml的dialog
View saveDialog=getLayoutInflater().inflate(R.layout.save, null);

//获取save.xml里边的ImageView
ImageView imageView=(ImageView) saveDialog.findViewById(R.id.show);
//获取save.xml里边的EditText保存的手势名字
final EditText gestureName=(EditText) saveDialog.findViewById(R.id.gesture_name);

//根据Gesture包含的手势创建一个位图
Bitmap bitmap=gesture.toBitmap(128, 128, 10, 0xFFFF0000);
imageView.setImageBitmap(bitmap);

//使用对话框显示saveDialog组件
new AlertDialog.Builder(MainActivity.this).setView(saveDialog).setPositiveButton("保存", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
//Toast.makeText(MainActivity.this, gestureName.getText().toString(), Toast.LENGTH_LONG).show();

//获取指定文件对应的手势库,如果该文件没有则创建一个新的
GestureLibrary gestureLib=GestureLibraries.fromFile(filePath);
//添加手势
gestureLib.addGesture(gestureName.getText().toString(), gesture);
//保存手势库
gestureLib.save();
}
}).setNegativeButton("取消",null).show();
}
});
}
}

另外在AndroidManifest.xml加上权限

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

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

实例2:读取我们添加的手势


activity_main.xml

<LinearLayout 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"
    tools:context=".MainActivity" 
    android:orientation="vertical">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入手势" />


    <android.gesture.GestureOverlayView android:id="@+id/my_gesture"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gestureStrokeType="multiple"/>
    
</LinearLayout>

MainActivity.java

package com.example.recognisegesture;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;


import android.os.Bundle;
import android.os.Environment;
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.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.graphics.Color;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.Toast;


public class MainActivity extends Activity{


GestureOverlayView gestureView;
String filePath="/sdcard/gestures";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

gestureView=(GestureOverlayView) super.findViewById(R.id.my_gesture);

//设置手势绘制颜色
gestureView.setGestureColor(Color.RED);
//设置绘制的宽度
gestureView.setGestureStrokeWidth(4);

final GestureLibrary lib=GestureLibraries.fromFile(filePath);
if(lib.load()){
Toast.makeText(MainActivity.this, "手势文件装载成功",Toast.LENGTH_LONG);
}
else{
Toast.makeText(MainActivity.this, "手势文件装载失败",Toast.LENGTH_LONG);
}


//添加手势绘制完成的事件
gestureView.addOnGesturePerformedListener(new OnGesturePerformedListener() {

@Override
public void onGesturePerformed(GestureOverlayView overlayView, Gesture gesture) {
//识别用户所绘制的手势
ArrayList<Prediction> predictions=lib.recognize(gesture);

ArrayList<String> result=new ArrayList<String>();

//遍历所有的prediction
for(Prediction pre:predictions){
//只有相似度大于2.0的才会被输出
if(pre.score>2.0){
result.add("匹配手势名称:"+pre.name+"相似度为"+pre.score);
}
}
if(result.size()>0){
ArrayAdapter<Object> adapter=new ArrayAdapter<Object>(MainActivity.this, android.R.layout.simple_dropdown_item_1line, result.toArray());

new AlertDialog.Builder(MainActivity.this).setAdapter(adapter, null)
.setPositiveButton("确定", null).show();
}
else{
Toast.makeText(MainActivity.this, "无法匹配到手势",Toast.LENGTH_LONG);
}
}
});
}
}

你可能感兴趣的:(Android增加、识别用户手势)