老规矩,直接上代码:
import android.view.MotionEvent;
import android.view.View;
import cn.cheng.dierrenge.livewallpaper.R;
/**
* 自定义过滤长按和长距离滑动的触摸监听器
* Created by SecondPersonality on 2021/10/16.
*/
public abstract class FilterLongDownSlideTouchListener implements View.OnTouchListener {
float xDown, yDown;
long timeDown;
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
// 记录初始触摸位置
xDown = event.getRawX();
yDown = event.getRawY();
timeDown = event.getEventTime();
// downEvent();
}
if (event.getAction() == MotionEvent.ACTION_UP) {
// 记录移动距离 只有短距离滑动才有效
float xMove = Math.abs(xDown - event.getRawX());
float yMove = Math.abs(yDown - event.getRawY());
float timeMove = Math.abs(timeDown - event.getEventTime());
// upRealEvent();
if (xMove < 20 && yMove < 20 && timeMove < 400) {
upEvent();
}
}
return false;
}
// public abstract void downEvent();
// protected abstract void upRealEvent();
public abstract void upEvent();
}
以上全部,笔者用之于按钮,长滑或长按可取消本次触摸事件,可供参考哈!
|