Android控件拖动的实现【源码】(2)

高度减去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();    

getLeft()方法得到的是控件坐标距离父控件原点(左上角,坐标(0,0))的x轴距离,getReght()是控件右边距离父控件原点的x轴距离,同理,getTop和getButtom是距离的y轴距离。

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();               }     

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/psgzw.html