针对View LayoutParams做动画

mView是我们期望有动画效果的目标View

FrameLayout.LayoutParams mParams = ... // 我们的目标 layout params
if (mTransitionTime > 0) {
ValueAnimator widthAnim = ValueAnimator.ofInt(0, mParams.width); // 从0变化到期望的宽度
widthAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
// 在回调里我们踩着节拍来修改 view 的 layout params
int val = (Integer) valueAnimator.getAnimatedValue();
mParams.width = val;
if (mView != null) {
mView.setLayoutParams(mParams);
}
}
});
widthAnim.setDuration(mTransitionTime);
widthAnim.start();
ValueAnimator heightAnim = ValueAnimator.ofInt(0, mParams.height); // 从0变化到期望的高度
heightAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
// 在回调里我们踩着节拍来修改 view 的 layout params
int val = (Integer) valueAnimator.getAnimatedValue();
mParams.height = val;
if (mView != null) {
mView.setLayoutParams(mParams);
}
}
});
heightAnim.setDuration(mTransitionTime);
heightAnim.start();
}
if (mTransformDegree > 0) { // 如果需要,在由小变大的过程中添加旋转效果
ObjectAnimator.ofFloat(mView, "rotation", 0f, mTransformDegree * 1.0f)
.setDuration(mTransitionTime > 0 ? mTransitionTime : 0).start();
}