Android随手指移动的DragView(二)——移动DragView

来源:互联网 发布:索尼网络经销商 编辑:程序博客网 时间:2024/06/05 04:18

获取偏移量offsetX和offsetY后,可以通过以下几种方式移动DragView:
(1),通过layout实现DragView的移动。

layout(getLeft() + offsetX, getTop() + offsetY, getRight() + offsetX, getBottom() + offsetY);

(2),通过ViewGroup.MarginLayoutParams实现DragView的移动。

 ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) getLayoutParams(); mlp.leftMargin = getLeft() + offsetX; mlp.topMargin = getTop() + offsetY; setLayoutParams(mlp);

(3),通过offsetLeftAndRight和offsetTopAndBottom实现DragView的移动。

//View封装好的方法,调用即可offsetLeftAndRight(offsetX);offsetTopAndBottom(offsetY);

(4),通过scrollTo实现DragView的移动。

//注意这里移动的是父布局,所以需要调用parent.getScrollX()//而且getLeft一直不变,因为子View一直没动,改变的是父布局而已     View parent = (View) getParent();  parent.scrollTo(parent.getScrollX() - offsetX, parent.getScrollY() - offsetY);

(5),通过scrollBy实现DragView的移动。

//因为移动的父View,父View像左移动,子View相对向右移动。所以为-offsetX。((View) getParent()).scrollBy(-offsetX, -offsetY);
0 0