【发布时间】: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