This Post helps to animate the android views expand and collapse like Expandable list view.
Find below the code snippet.
ViewAnimationUtils.java
Example
Find below the code snippet.
ViewAnimationUtils.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; import android.view.animation.Transformation; public class ViewAnimationUtils { int animDuration = 600; public static void expand(final View view) { view.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); final int targtetHeight = view.getMeasuredHeight(); view.getLayoutParams().height = 0; view.setVisibility(View.VISIBLE); Animation animation = new Animation() { @Override protected void applyTransformation(float interpolatedTime, Transformation t) { view.getLayoutParams().height = interpolatedTime == 1 ? ViewGroup.LayoutParams.WRAP_CONTENT : (int) (targtetHeight * interpolatedTime); view.requestLayout(); } @Override public boolean willChangeBounds() { return true; } }; animation.setDuration(animDuration); view.startAnimation(animation); } public static void collapse(final View view) { final int initialHeight = view.getMeasuredHeight(); Animation animation = new Animation() { @Override protected void applyTransformation(float interpolatedTime, Transformation t) { if (interpolatedTime == 1) { view.setVisibility(View.GONE); } else { view.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime); view.requestLayout(); } } @Override public boolean willChangeBounds() { return true; } }; animation.setDuration(animDuration); view.startAnimation(animation); } } |
Example
TextView description; Button expand, collapse; expand.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ViewAnimationUtils.expand(description) } }); collapse.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ViewAnimationUtils.expand(description) } });
1 comment:
Very Informative..!
Post a Comment