Android 手势DEMO

今天空闲想学习下android手势相关的内容,因为最近各种浏览器都在支持手势,看起来很炫也很方便。

1、手势的具体内容是在一个交gesture的文件中存放的,我们可以在自己的%androidproject_HOME%/res/raw/下放置我们的手势文件。


2、手势文件的创建在android sdk的sample下有一个GestureBuilder工程,我们可以利用次工程来创建属于我们自己的手势文件。

创建好了gesture文件以后我们就可以放入到自己工程中了。

3、我们创建自己的工程,将main.xml修改为以下以下内容:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">
	<android.gesture.GestureOverlayView
		xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/gesture"
		android:layout_width="fill_parent" android:layout_height="fill_parent"
		android:layout_weight="1" android:gestureStrokeType="multiple"
		android:eventsInterceptionEnabled="false" android:orientation="vertical">
		<TextView android:layout_width="wrap_content"
			android:layout_height="wrap_content" android:text="手势测试...请您滑动..."
			android:layout_gravity="center"></TextView>
	</android.gesture.GestureOverlayView>
</LinearLayout>

在这里注意:android.gesture.GestureOverlayView这个必须写全了,否则会报错,至于为什么自己看错误信息就知道了。

解释项:

gestureStrokeType:设置Stroke的类型。大多数情况下,gestures都是通过一笔完成。然而有一些特别的需求就需要通过多个笔画来实现,例如通过“+”来实现“Add”的行为。

Single:0

Multiple:1

eventsInterceptionEnabled: 可以简单理解为当前应用程序在Gestureoverlay模式下屏蔽其它ViewsEvents的开关。当它设置为True时,所有基于当前应用程序屏幕的操作都被视作为绘制gestures的行为,从而避免用户与应用程序界面的交互造成混乱。

orientation:这个属性作为当前Gesturerecognizing的参考标准(相当于filter的作用),例如:在这个例子中的Items垂直排列,所有基于Overlay的垂直(vertical) gesture,都无法被识别为有效的gesture,其与当前的垂直滚动条操作造成混淆。对于水平 (horizontal)gesture可以立即识别为有效的gesture,或者任何以垂直为起始的操作需要至少包含一段水平的绘制轨迹才可以被正常识别。

Horizontal : 0

Vertical : 1

4、我们创建一个GesturesHandler类来实现OnGesturePerformedListener接口,实现对手势的监听。

import java.util.ArrayList;

import org.wch.gesture.R;

import android.content.Context;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.Prediction;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.util.Log;
import android.widget.Toast;

public class GesturesHandler implements OnGesturePerformedListener {

	private GestureLibrary mLibrary;
	private boolean mLoaded = false;
    private Context context;
	public GesturesHandler(Context context) {
		this.context = context;
		mLibrary = GestureLibraries.fromRawResource(context, R.raw.gestures);
		load();
	}

	private boolean load() {
		mLoaded = mLibrary.load();
		return mLoaded;
	}

	@Override
	public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
		if (!mLoaded) {
			if (!load()) {
				return;
			}
		}
		ArrayList<Prediction> predictions = mLibrary.recognize(gesture);
		if (predictions.size() > 0) {
			Prediction prediction = predictions.get(0);
			Log.v("WCH", "Gesture " + prediction.name
					+ " recognized with score " + prediction.score);
			if (prediction.score > 2.0) {
				Toast.makeText(context, prediction.name, Toast.LENGTH_SHORT).show();
			}
		}
	}
}

说明: recognize()的返回结果是一个prediction集合,包含了所有与gesture相匹配的结果。prediction的score属性代表了与gesture得相似程度(通常情况下不考虑score小于1的结果)。

5、在我们activity中添加手势监听。

import android.app.Activity;
import android.gesture.GestureOverlayView;
import android.os.Bundle;

public class GetstureActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        GestureOverlayView gov = (GestureOverlayView)this.findViewById(R.id.gesture);
        gov.addOnGesturePerformedListener(new GesturesHandler(this));
    }
}
最后我们来看下效果:


随后显示下获取的手势的名称


这样我们的手势demo就完成了,后续扩展就我们就可以根据自己的需求进行了,其他的应该很简单了。

需要源码的留下邮箱。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值