/**
* @author wangxx
* @data 2020/4/22
*/
public class FloatingListener implements View.OnTouchListener {
public static final String TAG="FloatingListener";
private boolean isMove;
//开始时的坐标和结束时的坐标(相对于自身控件的坐标)
private int mStartX, mStartY, mStopX, mStopY;
//按下事件跟结束事件
private long mLastTime, mCurrentTime;
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
isMove = false;
mStartX = (int) event.getRawX();
mStartY = (int) event.getRawY();
mLastTime = System.currentTimeMillis();
break;
case MotionEvent.ACTION_MOVE:
isMove = true;
break;
case MotionEvent.ACTION_UP:
mStopX = (int) event.getRawX();
mStopY = (int) event.getRawY();
mCurrentTime = System.currentTimeMillis();
//判断时间
if (mCurrentTime - mLastTime < 500) {
//判断移动距离
if (Math.abs(mStartX - mStopX) >= 10 || Math.abs(mStartY - mStopY) >= 10) {
isMove = true;
} else {
isMove = false;
}
} else {
isMove = true;
}
break;
default:
break;
}
LOG.i(TAG,"isMove="+isMove);
//如果是移动事件不触发OnClick事件,防止移动的时候一放手形成点击事件
return isMove;
}
}
网友评论