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

2023-05-16

获取偏移量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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android随手指移动的DragView(二)——移动DragView 的相关文章

随机推荐