这里写链接内容* 对于手势识别这高大上的名词,一看就很牛叉的样子。我也没有在项目中使用过。于是,看到慕课网有相应的讲解,就根据讲解写了一个Demo,学习一下Android 手势识别的使用。
* 代码很少,也没什么说的。主要就是对几个api的使用。
* 首先是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=".MainActivity" >
<android.gesture.GestureOverlayView
android:id="@+id/gestureOverlayView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gestureStrokeType="multiple" >
<ImageView
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="@string/app_name"
android:src="@drawable/abc" />
</android.gesture.GestureOverlayView>
</RelativeLayout>
-
- 顺便说一下,在
</android.gesture.GestureOverlayView>
中的ImageView
和外面的ImageView
一样,不会被控件GestureOverlayView
干扰到。
- 顺便说一下,在
- 然后是Activity的代码:
package com.duck.systemservice;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
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.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnTouchListener {
private Context context;
private int count = 0;
private GestureDetector detector;
private android.gesture.GestureOverlayView gestureOverlayView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
setContentView(R.layout.activity_main);
setTitle("想不想被干?");
gestureOverlayView = (GestureOverlayView) findViewById(R.id.gestureOverlayView1);
final ImageView imageView = (ImageView) findViewById(R.id.imageview);
imageView.setOnTouchListener(this);
detector = new GestureDetector(context, new SimpleOnGestureListener() {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) {
// 向左滑动,向右滑动
// vtoast((e2.getX() - e1.getX()) + "");
if (e2.getX() - e1.getX() > 80) {
// 如果向左滑动了80px
vtoast("从左向右滑动了啊啊啊啊");
} else if (e1.getX() - e2.getX() > 82) {
vtoast("从右向左滑动了啊啊啊啊");
}
return super.onFling(e1, e2, velocityX, velocityY);
}
});
// /
final GestureLibrary library = GestureLibraries.fromRawResource(
context, R.raw.gestures);
library.load();
gestureOverlayView
.addOnGesturePerformedListener(new OnGesturePerformedListener() {
@Override
public void onGesturePerformed(GestureOverlayView overlay,
Gesture gesture) {
ArrayList<Prediction> recognize = library
.recognize(gesture);
for (Prediction prediction : recognize) {
System.out
.println("prediction.Name: " + prediction);
if (prediction.score > 5.0) {
if (prediction.name.equals("next")) {
vtoast("next");
imageView.setImageResource(R.drawable.bcd);
} else if (prediction.name.equals("exit")) {
vtoast("exit");
}
}
}
}
});
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// vtoast("duckckckckkc: " + count++);
switch (v.getId()) {
case R.id.imageview:
detector.onTouchEvent(event);
break;
default:
break;
}
return true;// 消费事件
}
void vtoast(String text) {
Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
}
}
-
- 上面
Activity
中用到了一个raw文件夹,里面有一个gestures
文件。这个文件不是这个程序生成的,是通过Gestures Builder
这个程序生成的,这个程序是Android SDk
中自带的一个Sample
。当然,也可以去下载这个app,下载之后,就可以使用,去生成gestures
了。这个程序的目的是生成手势识别库, 也就是说,你想要被识别的手势,必须是被手势识别库 所包含的,否则就不能被识别。当然,这个手势识别库
里面的内容,也完全是你自己去定义的。只是手势识别必须是这样的一个流程:首先,将需要识别的手势放进库里。(这一步怎么做?通过Gestures Builder
这个app去完成。);然后,就是在你的项目里面去做手势识别。
- 上面
- 以上代码运行ok.内容很少。由于这里面需要一个
gestures
文件,所以这个代码直接拷过去用不了,于是就将项目源码也放这里了。