requestLayout()
方法是在 Android 中创建动 Canvas 局的正确工具吗?
我们在 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().
关于android - Android的requestLayout()适合布局动画吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16988644/