How to change height layout as animation (programmatically)

How to change the height layout programmatically as an animation?

first:

enter image description here

after:

enter image description here

+6
source share
1 answer

Jamie amigo

After testing my code, I saw a small problem. Since I used "scaleY" , it just "stretches" the view. This means that if there is any text or something like that, it will simply stretch it and will not look beautiful. Try instead of ValueAnimator , its work will be smoother

 public void onClick(View v) { if(!isBig){ ValueAnimator va = ValueAnimator.ofInt(100, 200); va.setDuration(400); va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { public void onAnimationUpdate(ValueAnimator animation) { Integer value = (Integer) animation.getAnimatedValue(); v.getLayoutParams().height = value.intValue(); v.requestLayout(); } }); va.start(); isBig = true; } else{ ValueAnimator va = ValueAnimator.ofInt(200, 100); va.setDuration(400); va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { public void onAnimationUpdate(ValueAnimator animation) { Integer value = (Integer) animation.getAnimatedValue(); v.getLayoutParams().height = value.intValue(); v.requestLayout(); } }); va.start(); isBig = false; } } 

XML:

 <RelativeLayout android:layout_width="150dp" android:layout_height="100dp" android:layout_centerHorizontal="true" android:background="@android:color/holo_red_dark" android:onClick="onButtonClick" android:clickable="true"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="My Layout"/> </RelativeLayout> 

Old answer You can use ObjectAnimator, just remember to set pivotY(0) so that it moves only from below. Play with him yourself to suit your needs :)

 private boolean isBig = false; ... public void onClick(View v) { v.setPivotY(0f); if(!isBig){ ObjectAnimator scaleY = ObjectAnimator.ofFloat(v, "scaleY", 2f); scaleY.setInterpolator(new DecelerateInterpolator()); scaleY.start(); isBig = true; } else{ ObjectAnimator scaleY = ObjectAnimator.ofFloat(v, "scaleY", 1f); scaleY.setInterpolator(new DecelerateInterpolator()); scaleY.start(); isBig = false; } } 
+18
source

Source: https://habr.com/ru/post/972842/


All Articles