最近想实现android左滑弹出菜单框,右滑消失菜单这个个功能。了解了一下android 的滑动事件,必须是在view组件或者activity上实现,同时必须实现ontouchlistener, ongesturelistener这个两个接口。
public class myrelativelayout extends relativelayout implements gesturedetector.ongesturelistener{ private float mposx, mposy, mcurposx, mcurposy; private static final int fling_min_distance = 20;// 移动最小距离 private static final int fling_min_velocity = 200;// 移动最大速度 //构建手势探测器 gesturedetector mygesture = new gesturedetector(this); public myrelativelayout(context context){ super(context) } public myrelativelayout(context context, attributeset attrs, int defstyle) { super(context, attrs, defstyle); // todo auto-generated constructor stub } public myrelativelayout(context context, attributeset attrs) { super(context, attrs); // todo auto-generated constructor stub } @override public boolean ontouchevent(motionevent arg0) { // todo auto-generated method stub return mdetector.ontouchevent(arg0); } @override public boolean onsingletapup(motionevent e) { // todo auto-generated method stub return false; } @override public boolean onscroll(motionevent e1, motionevent e2, float distancex, float distancey) { // todo auto-generated method stub return false; } @override public boolean ondown(motionevent e) { // todo auto-generated method stub return false; } @override public void onshowpress(motionevent e) { // todo auto-generated method stub } @override public boolean onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) { // todo auto-generated method stub // e1:第1个action_down motionevent // e2:最后一个action_move motionevent // velocityx:x轴上的移动速度(像素/秒) // velocityy:y轴上的移动速度(像素/秒) // x轴的坐标位移大于fling_min_distance,且移动速度大于fling_min_velocity个像素/秒 //向左 if (e1.gety() - e2.gety() > fling_min_distance){ // && math.abs(velocityx) > fling_min_velocity) { collapse(); } //向上 if (e2.gety() - e1.gety() > fling_min_distance && math.abs(velocityx) > fling_min_velocity) { } return false; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
android -- ndk (stack corruption detected)
Android Span富文本图文混排 - ImageSpan(图文垂直居中)
Element DateTimePicker日期时间选择器的使用示例
【Appium踩坑】小米手机,启动报错:exited with code 255 writing to settings requires:android.permission.WRITE_SECUR
android 拍照 预览图与 照片分辨率(可视区域)不一致
[PAT顶级]1025 Keep at Most 100 Characters (35分)
网友评论