简单的手势识别

编程入门 行业动态 更新时间:2024-10-11 11:23:58

简单的<a href=https://www.elefans.com/category/jswz/34/1767590.html style=手势识别"/>

简单的手势识别

自己写的一个简单的手势判断

/*手势处理目前能够识别的手势:单击长按双击快速左滑快速右滑快速上滑快速下滑双指缩小双指放大*/
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;@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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){};
}

更多推荐

简单的手势识别

本文发布于:2024-03-05 09:50:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1711967.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:手势   简单

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!