【问题标题】:How can one use ViewPropertyAnimator to set Width to a specific value如何使用 ViewPropertyAnimator 将 Width 设置为特定值
【发布时间】:2015-02-19 07:02:52
【问题描述】:
如何使用 ViewPropertyAnimator 设置视图宽度?
我可以缩放或平移(见下文),但我无法设置为特定宽度。
frame_1.animate().scaleX(5).scaleY(5).start();
但是没有
frame_1.animate().width(1024).height(768).start();
【问题讨论】:
标签:
android
animation
android-4.4-kitkat
【解决方案1】:
试试这个,甚至从 Android 2.3 开始都支持:
ValueAnimatorCompat exitAnimator = AnimatorCompatHelper.emptyValueAnimator();
exitAnimator.setDuration(TransitCompat.ANIM_DURATION * 4);
exitAnimator.addUpdateListener(new AnimatorUpdateListenerCompat() {
private float oldWidth = view.getWidth();
private float endWidth = 0;
private float oldHeight = view.getHeight();
private float endHeight = 0;
private Interpolator interpolator2 = new BakedBezierInterpolator();//Any other will be also O.K.
@Override
public void onAnimationUpdate(ValueAnimatorCompat animation) {
float fraction = interpolator2.getInterpolation(animation.getAnimatedFraction());
float width = oldWidth + (fraction * (endWidth - oldWidth));
mTarget.get().getLayoutParams().width = (int) width;
float height = oldHeight + (fraction * (endHeight - oldHeight));
view.getLayoutParams().height = (int) height;
view.requestLayout();
}
});
exitAnimator.start();
【解决方案2】:
使用简单动画代替 ViewPropertyAnimator
public class ResizeWidthAnimation extends Animation
{
private int mWidth;
private int mStartWidth;
private View mView;
public ResizeWidthAnimation(View view, int width)
{
mView = view;
mWidth = width;
mStartWidth = view.getWidth();
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t)
{
int newWidth = mStartWidth + (int) ((mWidth - mStartWidth) * interpolatedTime);
mView.getLayoutParams().width = newWidth;
mView.requestLayout();
}
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight)
{
super.initialize(width, height, parentWidth, parentHeight);
}
@Override
public boolean willChangeBounds()
{
return true;
}
}