高度减去50是减去状态栏和标题栏的高度。
case MotionEvent.ACTION_DOWN: lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); break;然后获取控件一开始的位置,然后在ACTION_MOVIE中:
int dx =(int)event.getRawX() - lastX; int dy =(int)event.getRawY() - lastY; int left = v.getLeft() + dx; int top = v.getTop() + dy; int right = v.getRight() + dx; int bottom = v.getBottom() + dy; if(left < 0){ left = 0; right = left + v.getWidth(); } if(right > screenWidth){ right = screenWidth; left = right - v.getWidth(); } if(top < 0){ top = 0; bottom = top + v.getHeight(); } if(bottom > screenHeight){ bottom = screenHeight; top = bottom - v.getHeight(); } v.layout(left, top, right, bottom); Log.i("@@@@@@", "position??" + left +", " + top + ", " + right + ", " + bottom); lastX = (int) event.getRawX(); lastY = (int) event.getRawY();