【问题标题】:Using ValueAnimator to animate LayoutParams results in requestLayout() improperly called使用 ValueAnimator 为 LayoutParams 设置动画会导致 requestLayout() 调用不正确
【发布时间】:2014-01-26 18:53:49
【问题描述】:

我正在尝试为特定视图的高度变化制作动画。我已经设置了一个带有 Evaluator 的 ValueAnimator,并在动画的每一次传递中更新了 LayoutParams。反过来,这会触发 requestLayout() 调用。然而,在布局传递完成之前,动画的下一个传递会更新 LayoutParams 并触发另一个 requestLayout()。结果是 LogCat 中输出的警告以及动画效果不佳。它似乎跳过了很多帧。

    ValueAnimator contentHeightAnimator = ValueAnimator.ofObject(new HeightEvaluator(mContentView),
            mContentView.getMeasuredHeight(), (int) (getMeasuredHeight() - (actionBarHeight + destinationY)));
    contentHeightAnimator.setDuration(duration);
    contentHeightAnimator.setInterpolator(mInterpolator);
    contentHeightAnimator.start();

...

private static class HeightEvaluator extends IntEvaluator {

    private View mView;

    public HeightEvaluator(View v) {
        this.mView = v;
    }

    @Override
    public Integer evaluate(float fraction, Integer startValue, Integer endValue) {
        int num = (Integer) super.evaluate(fraction, startValue, endValue);
        ViewGroup.LayoutParams params = mView.getLayoutParams();
        params.height = num;
        mView.setLayoutParams(params);
        return num;
    }

}

动画布局更改的最佳方式是什么?

【问题讨论】:

    标签: android android-animation layoutparams


    【解决方案1】:

    这是我用来将菜单拆分的动画,以便可以插入另一个较低级别的菜单

        final int newBottomMargin = (int)(origBottomMargin + halfMargin);
        final int newTopMargin = (int)(origTopMargin + halfMargin);
    
        splitUp = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                LinearLayout.LayoutParams params =  (LinearLayout.LayoutParams) btnShopWireless.getLayoutParams();
                params.bottomMargin = (int)(newBottomMargin * interpolatedTime);
                btnShopWireless.setLayoutParams(params);
            }
        };
    
        splitDown = new Animation() {
            @Override
            protected void applyTransformation(float interpolatedTime, Transformation t) {
                LinearLayout.LayoutParams params =  (LinearLayout.LayoutParams) btnShopBundles.getLayoutParams();
                params.topMargin = (int)(newTopMargin * interpolatedTime);
                btnShopBundles.setLayoutParams(params);
            }
        };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-31
      • 1970-01-01
      • 2014-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多