android开发:手势开发实例GestureOverlayView,GestureLibrary

直接上源码吧,目前手势在实用程序中使用的还是比较少,没有被大量利用起来,所以这里简单的实现了添加手势,以及匹配手势:

//添加手势的后台代码
package com.lanxin.testtouch;

import android.content.Context;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;

import java.io.File;
import java.util.Date;

/**
 * Created by Administrator on 2016/7/17 0017.
 */
public class addAcitvity extends AppCompatActivity {
    private static final String TAG = "addAcitvityLog";
    GestureOverlayView gov;
    String filepath;
    GestureLibrary mGesture;
    Context mContext;

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

        init();

    }

    private void init() {
        mContext  = this;
        filepath = Environment.getExternalStorageDirectory()+"/lanxin/gesture";//保存目录
        Log.d(TAG, filepath);

        File file = new File(filepath);
        if(!file.exists())
            file.mkdirs();


        gov = (GestureOverlayView)findViewById(R.id.gestureOverlayView);
        gov.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() {
            @Override
            public void onGesturePerformed(GestureOverlayView gestureOverlayView, Gesture gesture) {
                Log.i(TAG, "onGesturePerformed:"+filepath);
                if(gesture.getLength() > 1){
                    File mStoreFile = new File(filepath, "gesture");//创建一个文件
                    mGesture = GestureLibraries.fromFile(mStoreFile);//装载文件
                    mGesture.addGesture("new_test", gesture);//添加手势
                    boolean b = mGesture.save();//保存
                    String str = b ? "保存成功":"保存失败";
                    Log.d(TAG,str);
                }

            }
        });
    }
}

 

下面是匹配手势的后台代码:

package com.lanxin.testtouch;

import android.content.Context;
import android.content.Intent;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

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


public class MainActivity extends AppCompatActivity {
    private static final String TAG = "GestureLog";
    GestureOverlayView gov;
    String filename;
    GestureLibrary mGesture;
    Context mContext;

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

        init();

    }

    private void init() {
        mContext  = this;
        filename = Environment.getExternalStorageDirectory()+"/lanxin/gesture/gesture_1468729525348";
        Log.d(TAG,filename);

        mGesture = GestureLibraries.fromFile(filename);

        if (!mGesture.load()) {
            Toast.makeText(this, "Could not load " + filename, Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(this,addAcitvity.class);
            startActivity(intent);
            finish();
        }

        Log.v(TAG, ">>>>>>>mGesture>>>>>>>>>:");
        Log.v(TAG, "  方向样式: " + mGesture.getOrientationStyle());
        Log.v(TAG, "  顺序类型: " + mGesture.getSequenceType());
        for( String gestureName : mGesture.getGestureEntries() ) {
            Log.v(TAG, "For gesture " + gestureName);
            int i = 1;
            for( Gesture gesture : mGesture.getGestures(gestureName) ) {
                Log.v(TAG, "    " + i + ": 手势ID: " + gesture.getID());
                Log.v(TAG, "    " + i + ": 手势数量: " + gesture.getStrokesCount());
                Log.v(TAG, "    " + i + ": 手势长度: " + gesture.getLength());
                i++;
            }
        }
        Log.v(TAG, ">>>>>>>end>>>>>>>>>:");

        gov = (GestureOverlayView)findViewById(R.id.gestureOverlayView);
        gov.addOnGesturePerformedListener(new GestureOverlayView.OnGesturePerformedListener() {
            @Override
            public void onGesturePerformed(GestureOverlayView gestureOverlayView, Gesture gesture) {
                Log.i(TAG, "onGesturePerformed");
                ArrayList<Prediction> data = mGesture.recognize(gesture);//手势识别

                //手势匹配
                if(data.size() > 0){
                    Prediction pre = data.get(0);//读取数据
                    if (pre.score > 1.0) {
                        Toast.makeText(mContext, pre.name, Toast.LENGTH_SHORT).show();
                        for(int i=0;i < data.size();i++)
                            Log.v(TAG, "prediction " + data.get(i).name + " - score = " + data.get(i).score);
                    }
                }


            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

还有一个xml的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main" tools:context=".MainActivity">

    <TextView android:text="Hello World!" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView" />

    <android.gesture.GestureOverlayView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/gestureOverlayView"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="67dp"></android.gesture.GestureOverlayView>
</RelativeLayout>

Leave a Comment