Android基础:手势识别
我们开启activity,通常是这样的:通过点击某个按钮来开启,这里我们不设置按钮,通过监听手势(左滑/右滑),来实现activity的切换。
效果图
有3个Activity,分别是A,B,C;开启顺序:A–B– C,–代表可互相开启。
逻辑:创建BaseActivity,根据手势判断是左滑还是右滑,调用对应的方法。
- 创建2个私有变量,代表上一个activity和下一个activity,并提供设置方法
- 创建2个方法,分别表示进入上一个activity和下一个activity
创建手势监听器, OnGestureListener 是接口
3.1 判断化的是否太慢,不做处理,弹出toast
3.2 如果向左滑,则加载下一个activity
3.3 如果向右滑,则加载上一个activity添加监听
- 让GestureDetector处理事件
BaseActivity
package com.android.gesturedetector;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.widget.Toast;
public class BaseActivity<T> extends Activity {
// 1 创建2个私有变量,代表上一个activity和下一个activity,并提供设置方法
private Class<T> nextActivity;
private Class<T> preActivity;
private GestureDetector detector;
public void setPreActivity(Class<T> preActivity) {
this.preActivity = preActivity;
}
public void setNextActivity(Class<T> nextActivity) {
this.nextActivity = nextActivity;
}
// 2 创建2个方法,分别表示进入上一个activity和下一个activity
public void toNextActivity(Class<T> preActivity) {
if (preActivity != null) {
Intent intent = new Intent(this, preActivity);
startActivity(intent);
finish();
// Pending正要发生的
overridePendingTransition(R.anim.right_other_to_left,R.anim.self_to_left);
}
}
public void toPreActivity(Class<T> nextActivity) {
if (nextActivity != null) {
Intent intent = new Intent(this, nextActivity);
startActivity(intent);
finish();
// Pending正要发生的
overridePendingTransition(R.anim.left_other_to_right,R.anim.self_to_right);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 3 创建手势监听器, OnGestureListener 是接口
OnGestureListener listener = new SimpleOnGestureListener() {
// shift + alt + s --->>>override/.. 重写的方法
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return super.onSingleTapUp(e);
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
super.onLongPress(e);
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
// TODO Auto-generated method stub
return super.onScroll(e1, e2, distanceX, distanceY);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) {
//3.1 判断化的是否太慢,不做处理,弹出toast
if (Math.abs(e1.getRawX() - e2.getRawX()) < 200) {
Toast.makeText(getBaseContext(), "你滑的太慢了", 0).show();
}
//3.2 如果向左滑,则加载下一个activity
if ((e1.getRawX() - e2.getRawX()) > 200) {
toNextActivity(nextActivity);
}
//3.3 如果向右滑,则加载上一个activity
if ((e2.getRawX() - e1.getRawX()) > 200) {
toPreActivity(preActivity);
}
return super.onFling(e1, e2, velocityX, velocityY);
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
super.onShowPress(e);
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return super.onDown(e);
}
@Override
public boolean onDoubleTap(MotionEvent e) {
// TODO Auto-generated method stub
return super.onDoubleTap(e);
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
// TODO Auto-generated method stub
return super.onDoubleTapEvent(e);
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
// TODO Auto-generated method stub
return super.onSingleTapConfirmed(e);
}
};
//4 添加监听
detector = new GestureDetector(this, listener);
}
// 5 让GestureDetector处理事件
@Override
public boolean onTouchEvent(MotionEvent event) {
return detector.onTouchEvent(event);
}
}
## ActivityA ##
public class ActivityA extends BaseActivity<ActivityB> {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
setNextActivity(ActivityB.class);
}
}
ActivityB
public class ActivityB extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
setPreActivity(ActivityA.class);
setNextActivity(ActivityC.class);
}
}
,
ActivityC
public class ActivityC extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c);
setPreActivity(ActivityB.class);
}
}