How can one use ViewPropertyAnimator to set Width to a specific value

爱⌒轻易说出口 提交于 2019-12-07 04:27:45

问题


How can I use ViewPropertyAnimator to set my view width?

I can scale or translate (see below) but I can't set to a specific width.

frame_1.animate().scaleX(5).scaleY(5).start();

but there is no

frame_1.animate().width(1024).height(768).start();

回答1:


Try this, support even from 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:


Use simple animation instead of 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; 
    } 
} 


来源:https://stackoverflow.com/questions/28600412/how-can-one-use-viewpropertyanimator-to-set-width-to-a-specific-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!