android activity和view判断滑动
实例代码:
//手指按下的点为(x1, y1)手指离开屏幕的点为(x2, y2) float x1 = 0; float x2 = 0; float y1 = 0; float y2 = 0; @override public boolean ontouchevent(motionevent event) { //继承了activity的ontouchevent方法,直接监听点击事件 if(event.getaction() == motionevent.action_down) { //当手指按下的时候 x1 = event.getx(); y1 = event.gety(); } if(event.getaction() == motionevent.action_up) { //当手指离开的时候 x2 = event.getx(); y2 = event.gety(); if(y1 - y2 > 50) { toast.maketext(mainactivity.this, "向上滑", toast.length_short).show(); } else if(y2 - y1 > 50) { toast.maketext(mainactivity.this, "向下滑", toast.length_short).show(); } else if(x1 - x2 > 50) { toast.maketext(mainactivity.this, "向左滑", toast.length_short).show(); } else if(x2 - x1 > 50) { toast.maketext(mainactivity.this, "向右滑", toast.length_short).show(); } } return super.ontouchevent(event); }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
如对本文有疑问, 点击进行留言回复!!
网友评论