9 使用GestureOverlayView进行手势识别

9-1 使用GestureOverlayView进行手势识别(一)

1.使用Gestures Builder生成手势文件

2.加入项目

3.GestureOverlayView

  拓展:笔画颜色、多笔画识别等

在模拟器上安装Gestures Bulider,然后添加手势

      

    

GestureOverlayView

  一种用于手势输入的透明覆盖层,可覆盖在其他控件的上方,也可包含其他控件

  存在3个监听器接口
    GestureOverlayView.OnGestureListener 手势监听器
    GestureOverlayView.OnGesturePerformedListener --手势执行监听器
    GestureOverlayView.OnGesturingListener --手势执行中监听器

GestureOverlayView一些常见的XML属性设置:

  eventsInterceptionEnabled
    定义当手势已经被识别出来时,是否拦截该手势动作

  fadeDuration
    当用户画完后,手势效果淡出的时间

  fadeEnabled
    用户画完后,手势是否自动淡出

  gestureColor
    手势的颜色

  gestureStrokeType
    笔画的类型

  gestureStrokeWidth
    笔画的粗细

  android:gestureStrokeType="single/multiple"

    single:写完一笔,松开再写一笔,第一笔会消失;
    multiple:写完一笔,松开再写一笔,第一笔不会消失。

 

9-2 使用GestureOverlayView进行手势识别(二)

MainActivity.java

package com.example.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.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity {

    // 1.声明控件
    GestureOverlayView gestureOverlayView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 2.绑定控件
        gestureOverlayView = (GestureOverlayView) findViewById(R.id.gestureOverlayView1);
        /*
         * 3.找到刚才预设定的手势文件 4.加载那个手势文件中的所有手势 5.匹配 识别
         */
        // 从资源中将手势文件加载进来
        final GestureLibrary library = GestureLibraries.fromRawResource(MainActivity.this, R.raw.gestures);
        library.load();
        // 6.添加监听器
        gestureOverlayView.addOnGesturePerformedListener(new OnGesturePerformedListener() {

            @Override
            public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
                // TODO Auto-generated method stub
                // 读出手势中内容 识别手势
                ArrayList<Prediction> myArrayList = library.recognize(gesture);// 返回的是集合
                // 遍历手势
                Prediction prediction = myArrayList.get(0);
                // prediction——相似度
                if (prediction.score >= 5.0) {//值越小,越好识别(Ps:取值范围:0.00——10.00)
                    if (prediction.name.equals("exit")) {
                        finish();
                    }
                    if (prediction.name.equals("next")) {
                        Toast.makeText(MainActivity.this, "播放下一首", Toast.LENGTH_LONG).show();

                    }
                    if (prediction.name.equals("pervious")) {
                        Toast.makeText(MainActivity.this, "播放上一首", Toast.LENGTH_LONG).show();
                    }
                } else {
                    Toast.makeText(MainActivity.this, "没有该手势", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}
<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" >

    <android.gesture.GestureOverlayView
        android:id="@+id/gestureOverlayView1"
        android:layout_width="300dp"
        android:gestureColor="#FF0000"
        android:gestureStrokeType="multiple"
        android:gestureStrokeWidth="5"
        android:layout_height="300dp" >

        <ImageView
            android:id="@+id/img"
            android:layout_width="300dp"
            android:layout_height="250dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_launcher" />
    </android.gesture.GestureOverlayView>

</RelativeLayout>

           

转载于:https://www.cnblogs.com/crazyzx/articles/5351369.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值