android - Android的requestLayout()适合布局动画吗?

标签 android android-layout

requestLayout() 方法是在 Android 中创建动 Canvas 局的正确工具吗?

我们在 Adob​​e/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/

相关文章:

java - 如何确保onBind在bindService之后被调用

java - 我的 Android 应用程序中出现 IOException

android - 使用旧版本然后 Android 4.3 检测信标

android - 如何在 Android 上正确放置 EditText 和 ImageButton

android - 如何正确配置 Horizo​​ntalScrollView 和 Scrollview?

android - 为什么我的 PagingSource 没有给我任何数据?

android - 无法在android中使用edittext.setText ("")清除edittext

android-layout - 硬编码字符串 "Test",应使用@string 资源

android - 如何使工具栏保持不变 Android

android - 使用 View 组将 ImageView 置于相机预览模式