对于触摸屏,其原生的消息无非按下、抬起、移动这几种,我们只需要简单重载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
例2:
TabHost 动画效果
关键点是重写setCurrentTab(int index) 方法。
代码片段:
import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TabHost;
import com.wjj.ath.R;
/** 继承 TabHost 组件,带有切入切出的滑动动画效果。 */
public class AnimationTabHost extends TabHost {
private Animation slideLeftIn;// 从屏幕左边进来
private Animation slideLeftOut;// 从屏幕左边出去
private Animation slideRightIn;// 从屏幕右边进来
private Animation slideRightOut;// 从屏幕右边出去
/** 记录是否打开动画效果 */
private boolean isOpenAnimation;
/** 记录当前标签页的总数 */
private int mTabCount;
public AnimationTabHost(Context context, AttributeSet attrs) {
super(context, attrs);
/** 初始化默认动画 */
slideLeftIn = AnimationUtils.loadAnimation(context,
R.anim.slide_left_in);
slideLeftOut = AnimationUtils.loadAnimation(context,
R.anim.slide_left_out);
slideRightIn = AnimationUtils.loadAnimation(context,
R.anim.slide_right_in);
slideRightOut = AnimationUtils.loadAnimation(context,
R.anim.slide_right_out);
isOpenAnimation = false;// 动画默认关闭
}
/**
* 设置是否打开动画效果
*
* @param isOpenAnimation
* true:打开
*/
public void setOpenAnimation(boolean isOpenAnimation) {
this.isOpenAnimation = isOpenAnimation;
}
/**
*
* @return 返回当前标签页的总数
*/
public int getTabCount() {
return mTabCount;
}
@Override
public void addTab(TabSpec tabSpec) {
mTabCount++;
super.addTab(tabSpec);
}
// 重写setCurrentTab(int index) 方法,这里加入动画!关键点就在这。
@Override
public void setCurrentTab(int index) {
// 切换前所在页的页面
int mCurrentTabID = getCurrentTab();
if (null != getCurrentView()) {
// 第一次设置 Tab 时,该值为 null。
if (isOpenAnimation) {
// 离开的页面
// 循环时,末页到第一页(边界处理)
if (mCurrentTabID == (mTabCount - 1) && index == 0) {
getCurrentView().startAnimation(slideLeftOut);
}
// 循环时,首页到末页
else if (mCurrentTabID == 0 && index == (mTabCount - 1)) {
getCurrentView().startAnimation(slideRightOut);
}
// 切换到右边的界面,从左边离开
else if (index > mCurrentTabID) {
getCurrentView().startAnimation(slideLeftOut);
}
// 切换到左边的界面,从右边离开
else if (index < mCurrentTabID) {
getCurrentView().startAnimation(slideRightOut);
}
}
}
// 设置当前页
super.setCurrentTab(index);
if (isOpenAnimation) {
// 当前页进来是动画
// 循环时,末页到第一页
if (mCurrentTabID == (mTabCount - 1) && index == 0) {
getCurrentView().startAnimation(slideRightIn);
}
// 循环时,首页到末页(边界处理)
else if (mCurrentTabID == 0 && index == (mTabCount - 1)) {
getCurrentView().startAnimation(slideLeftIn);
}
// 切换到右边的界面,从右边进来
else if (index > mCurrentTabID) {
getCurrentView().startAnimation(slideRightIn);
}
// 切换到左边的界面,从左边进来
else if (index < mCurrentTabID) {
getCurrentView().startAnimation(slideLeftIn);
}
}
}
}
package com.wjj.ath.activity;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import com.wjj.ath.R;
import com.wjj.ath.widget.AnimationTabHost;
public class TabHostActivity extends TabActivity implements OnTabChangeListener {
private GestureDetector gestureDetector;
private AnimationTabHost mTabHost;
private TabWidget mTabWidget;
/** 记录当前分页ID */
private int currentTabID = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTabHost = (AnimationTabHost) findViewById(android.R.id.tabhost);
mTabWidget = (TabWidget) findViewById(android.R.id.tabs);
mTabHost.setOnTabChangedListener(this);
init();
onTabChanged("0");// 人为调用回调方法,初始化选项卡tabs的颜色
gestureDetector = new GestureDetector(new TabHostTouch());
}
private void init() {
setIndicator(R.drawable.icon_1_c, 0, new Intent(this,
TabHostTestOne.class));
setIndicator(R.drawable.icon_2_c, 1, new Intent(this,
TabHostTestTwo.class));
setIndicator(R.drawable.icon_3_c, 2, new Intent(this,
TabHostTestThree.class));
setIndicator(R.drawable.icon_4_c, 3, new Intent(this,
TabHostTestFour.class));
mTabHost.setOpenAnimation(true);
}
private void setIndicator(int icon, int tabId, Intent intent) {
String str = String.valueOf(tabId);
TabHost.TabSpec localTabSpec = mTabHost.newTabSpec(str)
.setIndicator(str, getResources().getDrawable(icon))
.setContent(intent);
mTabHost.addTab(localTabSpec);
}
@Override
public void onTabChanged(String tabId) {
// tabId 为newTabSpec(String tag) 中传入的字符串tag,这里tag是0,1,2,3 可以转换为整形便于判断
int tabID = Integer.valueOf(tabId);
for (int i = 0; i < mTabWidget.getChildCount(); i++) {
if (i == tabID) {
mTabWidget.getChildAt(i).setBackgroundResource(
R.drawable.indicator_selected);
} else {
mTabWidget.getChildAt(i).setBackgroundResource(
R.drawable.indicator_unselected);
}
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
event.setAction(MotionEvent.ACTION_CANCEL);
}
return super.dispatchTouchEvent(event);
}
private class TabHostTouch extends SimpleOnGestureListener {
/** 滑动翻页所需距离 */
private static final int ON_TOUCH_DISTANCE = 80;
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// 右滑动,切换到左边一个tab
if (e2.getX() - e1.getX() >= ON_TOUCH_DISTANCE) {
currentTabID = mTabHost.getCurrentTab() - 1;
if (currentTabID < 0) {// 循环
currentTabID = mTabHost.getTabCount() - 1;
}
}
// 左滑动,切换到右边一个tab
else if (e1.getX() - e2.getX() >= ON_TOUCH_DISTANCE) {
currentTabID = mTabHost.getCurrentTab() + 1;
if (currentTabID >= mTabHost.getTabCount()) {// 循环
currentTabID = 0;
}
}
mTabHost.setCurrentTab(currentTabID);
return false;
}
}
}