自定义可拖拽的LinearLayout

来源:互联网 发布:勇者斗恶龙10国服mac 编辑:程序博客网 时间:2024/05/16 02:04
参照谷歌官方Demo写的一个可拖拽的LinearLayout


参考博客
http://developer.android.com/intl/zh-cn/training/gestures/scale.html
http://stackoverflow.com/questions/9398057/android-move-a-view-on-touch-move-action-move
http://javatechig.com/android/how-to-drag-a-view-in-android

控件相对于屏幕的位置
http://blog.csdn.net/sjf0115/article/details/7306284

多点触控
http://hukai.me/android-training-course-in-chinese/input/gestures/scale.html



一.只是简单的实现拖拽可以用如下代码
public boolean onTouchEvent(MotionEvent ev) {
// Let the ScaleGestureDetector inspect all events.
// mScaleDetector.onTouchEvent(ev);

final int action = MotionEventCompat.getActionMasked(ev);

switch (action) {
case MotionEvent.ACTION_DOWN: {
             mLastTouchX = ev.getX();
mLastTouchy= ev.getY();

break;
}

case MotionEvent.ACTION_MOVE: {
final float x = ev.getX();
final float y = ev.getY();

// Calculate the distance moved
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
layout((int) (getLeft() + dx), (int) (getTop() + dy), (int) (getRight() + dx), (int) (getBottom() + dy));
invalidate();

break;
}
case MotionEvent.ACTION_UP: {
break;
}
case MotionEvent.ACTION_CANCEL: {
break;
}
case MotionEvent.ACTION_POINTER_UP: {
break;
}
}
return true;
}
二.bug--当该自定义容器中有可点击的子控件(例如 Button)事件会被该控件消费,该自定义容器就不能处理事件,所以要在onInterceptTouchEvent中拦截事件

做如下修改




@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
int action = event.getAction() & MotionEvent.ACTION_MASK;
//为了使手指按在Button等可点击的控件上任可以滑动,需要拦截滑动实践
//并且为了使坐标准确,在此处记录按下的点
switch (action) {
case MotionEvent.ACTION_MOVE:
return true;
case MotionEvent.ACTION_DOWN:
             mLastTouchX = ev.getX();
mLastTouchy= ev.getY();

return false;
}
return false;
}
public boolean onTouchEvent(MotionEvent ev) {
// Let the ScaleGestureDetector inspect all events.
// mScaleDetector.onTouchEvent(ev);

final int action = MotionEventCompat.getActionMasked(ev);

switch (action) {
case MotionEvent.ACTION_DOWN: {
break;
}

case MotionEvent.ACTION_MOVE: {
final float x = ev.getX();
final float y = ev.getY();

// Calculate the distance moved
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
layout((int) (getLeft() + dx), (int) (getTop() + dy), (int) (getRight() + dx), (int) (getBottom() + dy));
invalidate();

break;
}
case MotionEvent.ACTION_UP: {
break;
}
case MotionEvent.ACTION_CANCEL: {
break;
}
case MotionEvent.ACTION_POINTER_UP: {
break;
}
}
return true;
}

三.bug---由于安卓可以处理多点触控,所以如果在拖拽的时候放上第二个手指,然后抬起第一个手指,此时app会把第二个点当做默认的点,并且把图片移到该点的位置。
所以需要在ACTION_POINTER_UP中重新选择操作点,所以程序改为
public class GoogleDragDemo extends LinearLayout {

private float mLastTouchX;
private float mLastTouchY;

public GoogleDragDemo(Context context) {
super(context);
}

public GoogleDragDemo(Context context, AttributeSet attrs) {
super(context, attrs);
}

// The ‘active pointer’ is the one currently moving our object.
private int INVALID_POINTER_ID = -1000;
private int mActivePointerId = INVALID_POINTER_ID;

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
int action = event.getAction() & MotionEvent.ACTION_MASK;
//为了使手指按在Button等可点击的控件上任可以滑动,需要拦截滑动实践
//并且为了使坐标准确,在此处记录按下的点
switch (action) {
case MotionEvent.ACTION_MOVE:
return true;
case MotionEvent.ACTION_DOWN:
final int pointerIndex = MotionEventCompat.getActionIndex(event);
mActivePointerId=MotionEventCompat.getPointerId(event,pointerIndex);

final float x = MotionEventCompat.getX(event, pointerIndex);
final float y = MotionEventCompat.getY(event, pointerIndex);

// Remember where we started (for dragging)
mLastTouchX = x;
mLastTouchY = y;
// Save the ID of this pointer (for dragging)
mActivePointerId = MotionEventCompat.getPointerId(event, 0);
mActivePointerId = MotionEventCompat.getPointerId(event, 0);
return false;
}
return false;
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
// Let the ScaleGestureDetector inspect all events.
// mScaleDetector.onTouchEvent(ev);

final int action = MotionEventCompat.getActionMasked(ev);

switch (action) {
case MotionEvent.ACTION_DOWN: {
break;
}

case MotionEvent.ACTION_MOVE: {
// Find the index of the active pointer and fetch its position
final int pointerIndex =
MotionEventCompat.findPointerIndex(ev, mActivePointerId);

final float x = MotionEventCompat.getX(ev, pointerIndex);
final float y = MotionEventCompat.getY(ev, pointerIndex);

// Calculate the distance moved
final float dx = x - mLastTouchX;
final float dy = y - mLastTouchY;
layout((int) (getLeft() + dx), (int) (getTop() + dy), (int) (getRight() + dx), (int) (getBottom() + dy));
invalidate();

// Remember this touch position for the next move event
//使用视图坐标系(相对做坐标系),不用刷新初始点
// mLastTouchX = x;
//mLastTouchY = y;

break;
}

case MotionEvent.ACTION_UP: {
mActivePointerId = INVALID_POINTER_ID;
break;
}

case MotionEvent.ACTION_CANCEL: {
mActivePointerId = INVALID_POINTER_ID;
break;
}

case MotionEvent.ACTION_POINTER_UP: {

final int pointerIndex = MotionEventCompat.getActionIndex(ev);
final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);

if (pointerId == mActivePointerId) {
// This was our active pointer going up. Choose a new
// active pointer and adjust accordingly.
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mLastTouchX = MotionEventCompat.getX(ev, newPointerIndex);
mLastTouchY = MotionEventCompat.getY(ev, newPointerIndex);
mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
}
break;
}
}
return true;
}
}



0 0
原创粉丝点击