重写onTouchEvent方法,将返回写成true消费掉触摸事件,
然后根据当前触摸的坐标点位移View
@Override
public boolean onTouchEvent(MotionEvent event) {
int rawX = (int)event.getRawX();
int rawY = (int)event.getRawY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
lastX = rawX;
lastY = rawY;
break;
case MotionEvent.ACTION_MOVE:
int offsetX = rawX - lastX;
int offsetY = rawY - lastY;
offsetLeftAndRight(offsetX);
offsetTopAndBottom(offsetY);
lastX = rawX;
lastY = rawY;
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
效果
TIM图片20190306171533.gif
网友评论