ObjectAnimator animate LinearLayout width

后端 未结 5 1264
日久生厌
日久生厌 2020-11-30 20:31

Ok, so i checked out http://android-developers.blogspot.com/2011/02/animation-in-honeycomb.html

He says you can animate the property of an object in a given time. A

5条回答
  •  时光取名叫无心
    2020-11-30 21:03

    In situations where there isn't a simple property getter/setter you should use ValueAnimator and perform the animation manually.

    Assuming:

    • v is the view you're animating
    • END_WIDTH is the target width of the view in pixels.
    • DURATION is the desired length of the animation in milliseconds.

    Your code should look something like this:

        ValueAnimator anim = ValueAnimator.ofInt(v.getMeasuredWidth(), END_WIDTH);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int val = (Integer) valueAnimator.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
                layoutParams.width = val;
                v.setLayoutParams(layoutParams);
            }
        });
        anim.setDuration(DURATION);
        anim.start(); 
    

提交回复
热议问题