您的位置:首页 > 移动开发

android 手势应用之我的第一个Demo APP

2015-12-16 09:49 363 查看
最近项目进度不是很紧,有时间来研究下android的手势识别方面的知识,其实这个东西目前在android很多应用中都可以看到了,用到的最多的就是APP,在登录的时候,我这个demo 其实 比较简单,主要是学习下怎么用的,具体的使用场景要看具体的项目了

这个demo的重点是在手势的识别上,因此在手势的新增和修改我都用的aosp的手势识别APP,如果想看怎么操作的可以去看源码(development/apps/GestureBuilder).可以到我的网盘下载 http://pan.baidu.com/disk/home#path=%252FGestureBuilder .

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<!--suppress ALL -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.baopingx.myapplication">

<uses-sdk android:minSdkVersion="17" android:maxSdkVersion="23" android:targetSdkVersion="23"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- SDCard读取数据权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".getCustomeGesture"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>


getCustomeGesture.java

package com.example.baopingx.myapplication;

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

import android.Manifest;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class getCustomeGesture extends AppCompatActivity {
private static final String TAG = "wilson_gesture";
private GestureLibrary library;
private Gesture mgesture;
private GestureOverlayView overlayView;
private TelephonyManager tm = null;
private String gesPath = "" ;
private Button mbutton = null;
private short REQUEST_WRITE_EXTERNAL_STORAGE = 0;
private static List<String> permissions = new ArrayList<String>();

/*
* 程序预制了几个手势
*   1 当画C字母的时候 会进行打电话处理
*   2 当画叉的时候 程序会退出
*   3 当画V的时候,会打开摄像头
* */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.get_custome_gesture);

if (!Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
Toast.makeText(this, "SDcard not exits!", Toast.LENGTH_SHORT).show();
this.finish();
}
init();
gesPath= new File(Environment.getExternalStorageDirectory(),
"gestures").getAbsolutePath();
File f = new File(gesPath);
if (!f.exists())
{
Log.d(TAG, "use prepare GestureLibraries" );
library = GestureLibraries.fromRawResource(this, R.raw.gestures);//用预制的代替
}

library = GestureLibraries.fromFile(f);
if(!library.load()){
Toast.makeText(getApplicationContext(), "gesture file load failed", 1).show();
this.finish();
}
final Set<String> entries = library.getGestureEntries();
Log.i(TAG, "current GestureLibraries record = " + entries.size());
}

public void init()
{
mbutton = (Button) this.findViewById(R.id.reconize);
overlayView = (GestureOverlayView) this.findViewById(R.id.gestures);
overlayView.addOnGestureListener(new GestureListener());//为view添加手势监听事件

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!hasPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)&&!hasPermission(Manifest.permission.READ_EXTERNAL_STORAGE)) {
permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
if (!permissions.isEmpty()) {
requestPermissions(permissions.toArray(new String[permissions.size()]), REQUEST_WRITE_EXTERNAL_STORAGE);
}
}
}
}

@TargetApi(Build.VERSION_CODES.M)
public boolean hasPermission(String permission) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED;
}
else
{
Log.d(TAG, "not M build.do nothing");
return true;
}
}

public void find(View v) {
recognize(mgesture);
overlayView.clear(true);//clear before gesture
}

private final class GestureListener implements GestureOverlayView.OnGestureListener {
public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
Log.i(TAG, "onGestureStarted()");
mbutton.setEnabled(true);

}

public void onGesture(GestureOverlayView overlay, MotionEvent event) {
Log.i(TAG, "onGesture()");
}

public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
Log.i(TAG, "onGestureEnded()");
mgesture = overlay.getGesture();
}

public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
Log.i(TAG, "onGestureCancelled()");
}
}

private final class GesturePerformedListener implements GestureOverlayView.OnGesturePerformedListener {
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
recognize(gesture);
}
}

private void recognize(Gesture gesture) {
//从手势库中查询匹配的内容,匹配的结果可能包括多个相似的内容,匹配度高的结果放在最前面。
ArrayList<Prediction> predictions = library.recognize(gesture);
if (!predictions.isEmpty()) {
Prediction prediction = predictions.get(0);
if (prediction.score >= 5) {//匹配率
if ("call".equals(prediction.name)) {
Log.i(TAG, "prediction.name = "+prediction.name);
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:13866668888"));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
//check whether call service can use now
tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
switch (tm.getSimState())
{
case TelephonyManager.SIM_STATE_ABSENT:
Log.e(TAG, "no sim card");
break;
case TelephonyManager.SIM_STATE_UNKNOWN:
Log.e(TAG, "unkonw status");
break;
case TelephonyManager.SIM_STATE_READY:
Log.i(TAG, "sim card ready");
Log.i(TAG, "now begin to start activity call");
startActivity(intent);
}
return;
}
}else if("exit".equals(prediction.name)){
Log.i(TAG, "prediction.name = "+prediction.name);
finish();//关闭Activity
}
else {
if ("video".equals(prediction.name)) {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
startActivity(intent);
}
}
}else{
Toast.makeText(getApplicationContext(), R.string.low, 1).show();
}
}else{
Log.i(TAG, "predictions.isEmpty");
Toast.makeText(getApplicationContext(), R.string.notfind, 1).show();
}
}

@Override
protected void onDestroy() {
super.onDestroy();
android.os.Process.killProcess(android.os.Process.myPid());//关闭应用
}
}


get_custome_gesture.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"
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="com.example.baopingx.myapplication.getCustomeGesture">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<android.gesture.GestureOverlayView
android:id="@+id/gestures"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gestureStrokeType="multiple" />

<Button
android:id="@+id/reconize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:onClick="find"
android:text="@string/recognize"
android:enabled="false" />

</LinearLayout>
</RelativeLayout>


res/values/dimens.xml

<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="fab_margin">16dp</dimen>
<dimen name="gesture_thumbnail_inset">8dip</dimen>
<dimen name="gesture_thumbnail_size">64dip</dimen>
</resources>


strings.xml

<resources>
<string name="app_name">wilsonStudio</string>
<string name="recognize">recognize</string>
<string name="low">low</string>
<string name="notfind">notfind</string>
</resources>


res/raw/gesture

在这里下载 http://pan.baidu.com/disk/home#list/path=%2Fgesture
我预制
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: