简单的手势识别
程序员文章站
2024-03-24 14:52:40
...
自己写的一个简单的手势判断
/*
手势处理
目前能够识别的手势:
单击
长按
双击
快速左滑
快速右滑
快速上滑
快速下滑
双指缩小
双指放大
*/
public class GestureListener implements View.OnTouchListener {
private boolean zoomBegin = true;
private boolean ItsTime = false;
private Timer clockTimer = null;
private Timer singleTimer = null;
private boolean move = false;
private float PressDownPoint_X = 0f;
private float PressDownPoint_Y = 0f;
private Long PressTime = 0L;
private float preX = 0f;
private float preY = 0f;
//缩放起始距离
private float w0 = 0f;
private float h0 = 0f;
private int slide_Left_step = 0;
private int slide_Right_step = 0;
private int slide_Up_step = 0;
private int slide_Down_step = 0;
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
int action = motionEvent.getAction();
switch (action)
{
//按下
case MotionEvent.ACTION_DOWN:
ActionDownHandler(motionEvent);
break;
//移动
case MotionEvent.ACTION_MOVE:
ActionMoveHandler(motionEvent);
break;
//抬起
case MotionEvent.ACTION_UP:
ActionUpHandler();
break;
default:
break;
}
return true;
}
private void ActionDownHandler(MotionEvent motionEvent)
{
/*
按下为一切手势判断的开始
*/
PressDownPoint_X = motionEvent.getX();
PressDownPoint_Y = motionEvent.getY();
preX = PressDownPoint_X;
preY = PressDownPoint_Y;
/*
启动计时
按下1秒不抬起 算长按
*/
if(clockTimer != null) {
clockTimer.cancel();
clockTimer = null;
}
ItsTime = false;
clockTimer = new Timer();
clockTimer.schedule(new TimerTask() {
@Override
public void run() {
//长按触发
onLongPress(PressDownPoint_X,PressDownPoint_Y);
ItsTime = true;
clockTimer = null;
}
}, 500);
}
private void ActionUpHandler()
{
/*
抬起为一切手势判断的结束
*/
if(!ItsTime && ! move) {
clockTimer.cancel();
clockTimer = null;
if(singleTimer == null)
{
singleTimer = new Timer();
singleTimer.schedule(new TimerTask() {
@Override
public void run() {
//单击事件触发
onPress(PressDownPoint_X, PressDownPoint_Y);
singleTimer = null;
}
},350);
}
Long now = System.currentTimeMillis();
if(PressTime == 0)
PressTime = now;
else{
Long IntervalTime = now - PressTime;
PressTime = now;
if(IntervalTime < 300)
{
//触发双击 设成0使其不会连续触发
PressTime = 0L;
if(singleTimer != null)
{
singleTimer.cancel();
singleTimer = null;
}
onDoublePress(PressDownPoint_X, PressDownPoint_Y);
}
}
}
ItsTime = false;
zoomBegin = true;
//根据步长判断是否触发
if(slide_Right_step >= 3)
{
SlideRight();
}
if(slide_Left_step >= 3)
{
SlideLeft();
}
if(slide_Up_step > 3)
{
SlideUp();
}
if(slide_Down_step > 3)
{
SlideDown();
}
slide_Right_step = 0;
slide_Left_step = 0;
slide_Down_step = 0;
slide_Up_step = 0;
move = false;
}
private void ActionMoveHandler(MotionEvent motionEvent)
{
if(motionEvent.getPointerCount() == 1)
{
//单指滑动
float Now_X = motionEvent.getX();
float Now_Y = motionEvent.getY();
if(Math.abs(Now_X - PressDownPoint_X) > 10 || Math.abs(Now_Y - PressDownPoint_Y) > 10)
{
cancelTimer();
}
int step_len = 15;
if(Now_X - preX > step_len && Math.abs(Now_Y - PressDownPoint_Y) < 50){
//向右滑动
slide_Right_step++;
}
if(Now_X - preX < 0 - step_len && Math.abs(Now_Y - PressDownPoint_Y) < 50){
//向左滑动
slide_Left_step++;
}
if(Now_Y - preY < 0 - step_len && Math.abs(Now_X - PressDownPoint_X) < 50){
//向上滑动
slide_Up_step++;
}
if(Now_Y - preY > step_len && Math.abs(Now_X - PressDownPoint_X) < 50){
//向下滑动
slide_Down_step++;
}
//存下之前点
preX = Now_X;
preY = Now_Y;
}
if(motionEvent.getPointerCount() == 2)
{
cancelTimer();
float Ax0 = motionEvent.getX(0);
float Ax1 = motionEvent.getX(1);
float Ay0 = motionEvent.getY(0);
float Ay1 = motionEvent.getY(1);
//记录第一次的值
if(zoomBegin) {
w0 = Math.abs(Ax0 - Ax1);
h0 = Math.abs(Ay0 - Ay1);
zoomBegin = false;
}
float w1 = Math.abs(Ax0 - Ax1);
float h1 = Math.abs(Ay0 - Ay1);
//大于0 缩小X轴范围 小于0 扩大X轴范围
float w = w1 - w0;
float h = h1 - h0;
//缩放过小 忽略不计
if(Math.abs(w) <= 50)
{
w = 0;
}
if(Math.abs(h) <= 50)
{
h = 0;
}
//计算X Y的缩放比例
float n_X = (w0 + w) / w0;
float n_Y = (h0 + h) / h0;
Zoom(n_X,n_Y);
}
}
private void cancelTimer()
{
move = true;
if(clockTimer != null) {
clockTimer.cancel();
clockTimer = null;
}
}
public void onLongPress(float x,float y){};
public void onPress(float x,float y){};
public void onDoublePress(float x,float y){};
public void SlideRight(){};
public void SlideLeft(){};
public void SlideUp(){};
public void SlideDown(){};
public void Zoom(float x,float y){};
}