验证中...
本周日【珠海源创会】一起聊聊:PingCAP分布式事务、支付宝移动端实践、GSBN技术框架选型,点此报名占座
gistfile1.txt
原始数据 复制代码
public class DragViewOnTouchListener implements View.OnTouchListener {
private int lastX = 0;
private int lastY = 0; //手指在屏幕上的座标
private boolean isDraged = false; //View是否被移动过
private boolean isDrag = false; //判断是拖动还是点击
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
isDrag = false;
isDraged = false;
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
int dx = (int) event.getRawX()-lastX;
int dy = (int) event.getRawY()-lastY; //手指在屏幕上移动的距离
if (isDraged){
isDrag = true; //如果已经被拖动过,那么无论本次移动的距离是否为零,都判定本次事件为拖动事件
}else{
if (dx == 0 && dy == 0){
isDraged = false; //如果移动的距离为零,则认为控件没有被拖动过
}else{
isDraged = true;
isDrag = true;
}
}
int l = v.getLeft()+dx;
int b = v.getBottom()+dy;
int r = v.getRight()+dx;
int t = v.getTop()+dy;
if(l < 0){//处理按钮被移动到父布局的上下左右四个边缘时的情况,防止控件被拖出父布局
l = 0;
r = l + v.getWidth();
}
if(t < 0){
t = 0;
b = t + v.getHeight();
}
v.layout(l, t, r, b);
lastX = (int) event.getRawX();
lastY = (int) event.getRawY();
v.postInvalidate();
break;
}
return isDrag;
}
}

评论列表( 0 )

你可以在登录后,发表评论

搜索帮助

12_float_left_people 12_float_left_close