Android property animation: how to increase view height?

放肆的年华 提交于 2019-11-27 10:06:18

问题


How to increase the view height using Property Animations in Android?

ObjectAnimator a = ObjectAnimator.ofFloat(viewToIncreaseHeight, "translationY", -100);
a.setInterpolator(new AccelerateDecelerateInterpolator());
a.setDuration(1000);
a.start();

The translationY actually moves the view not increase the height. How can I increase the height of the view?


回答1:


ValueAnimator anim = ValueAnimator.ofInt(viewToIncreaseHeight.getMeasuredHeight(), -100);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator valueAnimator) {
        int val = (Integer) valueAnimator.getAnimatedValue();
        ViewGroup.LayoutParams layoutParams = viewToIncreaseHeight.getLayoutParams();
        layoutParams.height = val;
        viewToIncreaseHeight.setLayoutParams(layoutParams);
    }
});
anim.setDuration(DURATION);
anim.start(); 



回答2:


You can use ViewPropertyAnimator, which can save you some lines of code :

yourView.animate().scaleY(-100f).setInterpolator(new AccelerateDecelerateInterpolator()).setDuration(1000);

that should be all you need, be sure to check the documentation and all available methods for ViewPropertyAnimator.




回答3:


This is not a direct answer to this question. However, it may help someone.

Sometimes, we want to increase/decrease view's height because some of its child is is being added/removed (or just becoming visible/gone).

On those cases, you really can use Android default animation. As mentioned in other answers, you can set via:

<LinearLayout
    ...
    android:animateLayoutChanges="true"
.../>

Or in Java:

linearLayout.setLayoutTransition(new LayoutTransition());

If you created a custom view:

public StickerPickerView(final Context context, final AttributeSet attrs,
        final int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    setLayoutTransition(new LayoutTransition());
}

For me, it was pretty satisfactory but I just tested on latest APIs. That will automatically add a fade in/out when your view becomes visible. It will also animate the height/width of your view when same changes (like when you change the visibility of one of the child views etc).

So, I recommend to at least give a try.




回答4:


It's pretty straight forward as you can see below.

<LinearLayout android:id="@+id/container"
android:animateLayoutChanges="true"
.../>

More info below:

https://developer.android.com/training/animation/layout



来源:https://stackoverflow.com/questions/32835397/android-property-animation-how-to-increase-view-height

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