Android中添加和识别手势操作

实现思路:将手势写入GestureLibrary中,对其他手势进行匹配对比,进行识别

  • 先在布局中加入GestureOverlayView组件
 <android.gesture.GestureOverlayView
        android:id="@+id/add_gesture"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gestureStrokeType="multiple"
        />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#AFAFAF"
        />

    <android.gesture.GestureOverlayView
        android:id="@+id/recognize_gesture"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gestureStrokeType="multiple"
        />
android:gestureStrokeType="" 
    multiple:手势需要多笔完成
    single:手势一笔完成
  • 代码:
package top.helloworlds.gesture;

import android.content.DialogInterface;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    GestureOverlayView addGesture;
    GestureOverlayView recognizeGesture;
    GestureLibrary library;

    //设置手势库存储的路径
    final String LIBRARY_PATH = "/mnt/sdacrd/Gesture";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        //添加手势
        addNewGesture();
        //识别手势
        recognizeTheGesture();
    }

    private void recognizeTheGesture() {
        //读取手势库
        library = GestureLibraries.fromFile(LIBRARY_PATH);
        if (library.load()) {
            Toast.makeText(MainActivity.this, "装载成功", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivity.this, "装载失败", Toast.LENGTH_SHORT).show();
        }

        //获取手势编辑组件
        recognizeGesture = (GestureOverlayView) findViewById(R.id.recognize_gesture);
        //为组件添加监听器
        recognizeGesture.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() {
            @Override
            public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
                //识别绘制的手势库
                ArrayList<Prediction> predictions = library.recognize(gesture);
                ArrayList<String> result = new ArrayList<String>();
                //遍历所有找到的Prediction对象
                for (Prediction pred : predictions) {
                    //当相识度大于2.0时输出
                    if (pred.score > 2.0) {
                        result.add("与"+pred.name+"相似的为"+pred.score);
                    }
                }

                if (result.size() > 0) {
                    //使用一个带List的对话框来显示所有匹配的手势
                    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_SHORT).show();
                }
            }
        });

    }

    private void addNewGesture() {

        //获取手势编辑视图
        addGesture = (GestureOverlayView) findViewById(R.id.add_gesture);
        //设置手势绘制的颜色
        addGesture.setGestureColor(Color.RED);
        //设置手势的绘制宽度
        addGesture.setGestureStrokeWidth(4);
        //添加监听事件
        addGesture.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() {
            @Override
            public void onGesturePerformed(GestureOverlayView overlay, final Gesture gesture) {
                //加载布局文件所代表的视图
                View saveDialog = getLayoutInflater().inflate(R.layout.dialog, null);
                ImageView imageView = (ImageView) saveDialog.findViewById(R.id.imageView);
                final EditText editText = (EditText) saveDialog.findViewById(R.id.editText);

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

                //使用对话框显示布局文件
                new AlertDialog.Builder(MainActivity.this)
                        .setView(saveDialog)
                        .setPositiveButton("保存", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //获取指定文件对应的手势库
                                GestureLibrary gestureLibrary = GestureLibraries.fromFile(LIBRARY_PATH);
                                //添加手势
                                gestureLibrary.addGesture(editText.getText().toString(), gesture);
                                //保存手势库
                                gestureLibrary.save();
                            }
                        })
                        .setNegativeButton("取消", null)
                        .show();
            }
        });

    }


}

你可能感兴趣的:(android,手势识别,手势,手势操作)