android 手势识别(二)

这里写图片描述
这里写图片描述
识别的区域为GestureOverlayView的区域 ,GestureOverlayView包裹其他控件。
第一步。需要先在sdk 的simple中找一个版本,然后导入其中的GestureBuilder工程,并运行工程,添加手势 。然后再该工程的内存卡中取出生成的手势文件gesture
第二步,将生成的手势文件gesture导入你需要用的工程的res 下的raw文件夹 。然后编写代码
第三步,编写代码

public class GestureOverlayViwActivity extends Activity {
private GestureOverlayView gestureOverlayView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gesture_overlay_viw);
        gestureOverlayView=(GestureOverlayView) findViewById(R.id.gestureOverlayView1);
        //1.找到预设定的手势文件
        //2.加载那个手势文件中的所有手势
        //3.匹配识别
        //从资源中将收拾文件加载进来
        final GestureLibrary library=GestureLibraries.fromRawResource(GestureOverlayViwActivity.this, R.raw.gestures);
        library.load();
        gestureOverlayView.addOnGesturePerformedListener(new OnGesturePerformedListener() {

            @Override
            public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
                //读出手势,识别手势
                ArrayList<Prediction> mygesture=library.recognize(gesture);
                Prediction prediction =mygesture.get(0);//prediction 为取出的手势
                if(prediction.score>=7.0){//5.0为相似度
                    if(prediction.name.equals("exid")){
                        finish();
                    }if(prediction.name.equals("next")){
                        Toast.makeText(GestureOverlayViwActivity.this, "播放下一首", Toast.LENGTH_SHORT).show();
                    }if(prediction.name.equals("perivous")){
                        Toast.makeText(GestureOverlayViwActivity.this, "播放上一首", Toast.LENGTH_SHORT).show();
                    }

                }else
                {
                    Toast.makeText(GestureOverlayViwActivity.this, "没有该手势", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }


}

activity_gesture_overlay_viw.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"
    tools:context="com.example.gesturedetectordemo.GestureOverlayViwActivity" >

    <android.gesture.GestureOverlayView
        android:id="@+id/gestureOverlayView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        >
        <ImageView
            android:id="@+id/img2"
            android:layout_width="fill_parent"
            android:layout_height="461dp"
            android:background="@drawable/image1" />
    </android.gesture.GestureOverlayView>

</RelativeLayout>

这里写图片描述
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值