Created
September 6, 2017 20:39
-
-
Save Ekalips/610a2b0a73825dfae712bf2709939a29 to your computer and use it in GitHub Desktop.
Handy util class that will animate view vertical collapse/expand
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.wldev.expandablecardviewlist.extra; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.view.animation.Animation; | |
import android.view.animation.Transformation; | |
public class ViewAnimationUtils { | |
public static void expand(final View v, final AnimationEndCallback callback, boolean fast) { | |
v.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); | |
final int targtetHeight = v.getMeasuredHeight(); | |
v.getLayoutParams().height = 0; | |
v.setVisibility(View.VISIBLE); | |
Animation a = new Animation() { | |
@Override | |
protected void applyTransformation(float interpolatedTime, Transformation t) { | |
v.getLayoutParams().height = interpolatedTime == 1 | |
? ViewGroup.LayoutParams.WRAP_CONTENT | |
: (int) (targtetHeight * interpolatedTime); | |
v.requestLayout(); | |
} | |
@Override | |
public boolean willChangeBounds() { | |
return true; | |
} | |
}; | |
int duration = 0; | |
if (!fast) { | |
duration = (int) (targtetHeight / v.getContext().getResources().getDisplayMetrics().density); | |
} | |
a.setDuration(duration); | |
a.setAnimationListener(new Animation.AnimationListener() { | |
@Override | |
public void onAnimationStart(Animation animation) { | |
} | |
@Override | |
public void onAnimationEnd(Animation animation) { | |
if (callback != null) | |
callback.onAnimationEnded(); | |
} | |
@Override | |
public void onAnimationRepeat(Animation animation) { | |
} | |
}); | |
v.startAnimation(a); | |
} | |
public static void collapse(final View v, final AnimationEndCallback callback, boolean fast) { | |
final int initialHeight = v.getMeasuredHeight(); | |
Animation a = new Animation() { | |
@Override | |
protected void applyTransformation(float interpolatedTime, Transformation t) { | |
if (interpolatedTime == 1) { | |
v.setVisibility(View.GONE); | |
} else { | |
v.getLayoutParams().height = initialHeight - (int) (initialHeight * interpolatedTime); | |
v.requestLayout(); | |
} | |
} | |
@Override | |
public boolean willChangeBounds() { | |
return true; | |
} | |
}; | |
int duration = 0; | |
if (!fast) { | |
duration = (int) (initialHeight / v.getContext().getResources().getDisplayMetrics().density); | |
} | |
a.setDuration(duration); | |
a.setAnimationListener(new Animation.AnimationListener() { | |
@Override | |
public void onAnimationStart(Animation animation) { | |
} | |
@Override | |
public void onAnimationEnd(Animation animation) { | |
if (callback != null) | |
callback.onAnimationEnded(); | |
} | |
@Override | |
public void onAnimationRepeat(Animation animation) { | |
} | |
}); | |
v.startAnimation(a); | |
} | |
interface AnimationEndCallback { | |
void onAnimationEnded(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment