requestLayout()方法是否是在
Android中创建动画布局的正确工具?
我们使用Adobe / Apache Flex工作了几年,并且知道了两个方法invalidateProperties()和commitProperties().似乎Android的requestLayout()和layout()服务于类似的目的.但docs提到有一个开销.
public class Paginator extends ViewGroup { private float animatedCurrentPage = 1; private final ObjectAnimator objectAnimator; public Paginator(Context context,AttributeSet attrs) { super(context,attrs); objectAnimator = ObjectAnimator.ofFloat(this,"animatedCurrentPage",0f); } public void jumpToPage(int page) { objectAnimator.cancel(); objectAnimator.setFloatValues(animatedCurrentPage,page); objectAnimator.start(); } public void setAnimatedCurrentPage(float animatedCurrentPage) { this.animatedCurrentPage = animatedCurrentPage; requestLayout(); // <== queue an update of the layout } @Override protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec) { // measure children here } @Override protected void onLayout(boolean changed,final int l,final int t,final int r,final int b) { // layout children here } }
解决方法
我认为“invalidate()”应该通过调用而不是
link
Drawing is handled by walking the tree and rendering each view that
intersects the invalid region. Because the tree is traversed in-order,
this means that parents will draw before (i.e.,behind) their
children,with siblings drawn in the order they appear in the tree. If
you set a background drawable for a View,then the View will draw it
for you before calling back to its onDraw() method.Note that the framework will not draw views that are not in the invalid region.
To force a view to draw,call invalidate().