HappyLifeLife.com
HappyLifeLife.com
HappyLifeLife.com 登录 HappyLifeLife.com 注册 HappyLifeLife.com
爱新闻 爱生活
爱分享 爱学习
爱读书 爱探索
爱音乐 爱宇宙
爱电影 爱地球
爱阅读 爱世界
爱运动 爱科技
资料宝库

Android 手势识别
HappyLifeLife.com HappyLifeLife.com HappyLifeLife.com HappyLifeLife.com HappyLifeLife.com HappyLifeLife.com HappyLifeLife.com HappyLifeLife.com HappyLifeLife.com HappyLifeLife.com
关健词: andro 手势
1. android 有一个手势识别的类:OnGestureListener
2. 在 GestureDetector() 中使用上面的class 即可 系统就会把手势交由该类来处理
public class SampleGuest implements OnGestureListener {
Activity activity;
public SampleGuest(Activity a){
activity = a;
}
public boolean onTouchEvent(MotionEvent me){
return gesture.onTouchEvent(me);
}
@Override
public boolean onDown(MotionEvent e) {
Log.d("TAG","[+++++++++++][onDown]");
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if ((e1.getX() - e2.getX() > VALUE_DISTANCE)
&& Math.abs(velocityX) > VALUE_SPEED) {
Log.d("TAG","[+++++++++++][onFling][Fling left]");
} else if ((e2.getX() - e1.getX() > VALUE_DISTANCE)
&& Math.abs(velocityX) > VALUE_SPEED) {
Log.d("TAG","[+++++++++++][onDown][Fling right]");
}
return true;
}
@Override
public void onLongPress(MotionEvent e) {
Log.d("TAG","[+++++++++++][onLongPress]");
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
Log.d("TAG","[+++++++++++][onScroll]");
return true;
}
@Override
public void onShowPress(MotionEvent e) {
Log.d("TAG","[+++++++++++][onShowPress]");
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.d("TAG","[+++++++++++][onSingleTapUp]");
return true;
}
}
public boolean onDown(MotionEvent e){}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY){}
public void onLongPress(MotionEvent e)
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY)
public void onShowPress(MotionEvent e)
public boolean onSingleTapUp(MotionEvent e)
对于触摸屏,其原生的消息无非按下、抬起、移动这几种,我们只需要简单重载onTouch或者设置触摸侦听器setOnTouchListener即可进行处理。不过,为了提高我们的APP的用户体验,有时候我们需要识别用户的手势,Android给我们提供的手势识别工具GestureDetector就可以帮上大忙了。
GestureDetector的工作原理是,当我们接收到用户触摸消息时,将这个消息交给GestureDetector去加工,我们通过设置侦听器获得GestureDetector处理后的手势。
GestureDetector提供了两个侦听器接口,OnGestureListener处理单击类消息,OnDoubleTapListener处理双击类消息。
OnGestureListener的接口有这几个:
abstract boolean onDown(MotionEvent e);
abstract boolean onSingleTapUp(MotionEvent e);
abstract void onShowPress(MotionEvent e);
abstract void onLongPress(MotionEvent e);
abstract boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY);
abstract boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY);
OnDoubleTapListener的接口有这几个:
abstract boolean onDoubleTap(MotionEvent e);
abstract boolean onDoubleTapEvent(MotionEvent e);
abstract boolean onSingleTapConfirmed(MotionEvent e);
有时候我们并不需要处理上面所有手势,方便起见,Android提供了另外一个类SimpleOnGestureListener实现了如上接口,我们只需要继承SimpleOnGestureListener然后重载感兴趣的手势即可。
应用
STEP 1: 创建手势侦听对象
package noodies.blog.csdn.net;
import android.content.Context;
import android.view.MotionEvent;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.widget.Toast;
public class MyGestureListener extends SimpleOnGestureListener {
private Context mContext;
MyGestureListener(Context context) {
mContext = context;
}
@Override
public boolean onDown(MotionEvent e) {
Toast.makeText(mContext, "DOWN " + e.getAction(), Toast.LENGTH_SHORT).show();
return false;
}
@Override
public void onShowPress(MotionEvent e) {
Toast.makeText(mContext, "SHOW " + e.getAction(), Toast.LENGTH_SHORT).show();
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
Toast.makeText(mContext, "SINGLE UP " + e.getAction(), Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
Toast.makeText(mContext, "SCROLL " + e2.getAction(), Toast.LENGTH_SHORT).show();
return false;
}
@Override
public void onLongPress(MotionEvent e) {
Toast.makeText(mContext, "LONG " + e.getAction(), Toast.LENGTH_SHORT).show();
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Toast.makeText(mContext, "FLING " + e2.getAction(), Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
Toast.makeText(mContext, "DOUBLE " + e.getAction(), Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
Toast.makeText(mContext, "DOUBLE EVENT " + e.getAction(), Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
Toast.makeText(mContext, "SINGLE CONF " + e.getAction(), Toast.LENGTH_SHORT).show();
return false;
}
}
STEP 2: 设置手势识别
我们可以在Activity里设置手势识别:
package noodies.blog.csdn.net;
import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
public class GestureTestActivity extends Activity {
private GestureDetector mGestureDetector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mGestureDetector = new GestureDetector(this, new MyGestureListener(this));
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
}
也可以在自定义的View里面设置手势识别:
package noodies.blog.csdn.net;
import android.content.Context;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
public class MyView extends View {
private GestureDetector mGestureDetector;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
mGestureDetector = new GestureDetector(context, new MyGestureListener(context));
setLongClickable(true);
this.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return mGestureDetector.onTouchEvent(event);
}
});
}
}
陷阱
对于自定义View,使用手势识别有两处陷阱可能会浪费你的不少时间。
1:View必须设置longClickable为true,否则手势识别无法正确工作,只会返回Down, Show, Long三种手势
2:必须在View的onTouchListener中调用手势识别,而不能像Activity一样重载onTouchEvent,否则同样手势识别无法正确工作
测试结果
下面是各种操作返回的手势序列,数值0表示触摸屏按下,1表示抬起
单击:down 0, single up 1, single conf 0
短按:down 0, show 0, single up 1
长按:down 0, show 0, long 0
双击:down 0, single up 1, double 0, double event 0, down 0, double event 1
滚动:down 0, (show 0), scrool 2...
滑动:down 0, (show 0), scrool 2..., fling 1
HappyLifeLife.com HappyLifeLife.com HappyLifeLife.com HappyLifeLife.com HappyLifeLife.com HappyLifeLife.com HappyLifeLife.com HappyLifeLife.com HappyLifeLife.com HappyLifeLife.com
首篇 上一篇 总共539篇,当前第140 下一篇 末篇
天天快乐生活[HappyLifeLife.com]
欢迎来访 快乐空间 热点新闻 我的分享 读书频道 七彩生活 精彩世界 快乐搜索 
ICP备15040518 | ©1999-2018 HappyLiveLife.com 版权所有 | 服务 | 爱新闻 | 爱分享 | 在线搜索 | 招贤纳士
欢迎来访 快乐空间 热点新闻 我的分享 读书频道 七彩生活 精彩世界 快乐搜索 
ICP备15040518 | ©1999-2018 HappyLiveLife.com 版权所有 | 服务 | 爱新闻 | 爱分享 | 在线搜索 | 招贤纳士