Skip to content

Instantly share code, notes, and snippets.

@dron247
Created March 26, 2017 08:37
Show Gist options
  • Save dron247/31428ed1aa6b94c3ec22671a86b648a3 to your computer and use it in GitHub Desktop.
Save dron247/31428ed1aa6b94c3ec22671a86b648a3 to your computer and use it in GitHub Desktop.
View expand/collapse helper
package com.dgse.grukoza_body_composition_tracker.helpers;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.Transformation;
/**
* Created by Андрей on 26.09.2014.
*/
public class ViewHelper {
public static void expand(final View v, int layoutParamsWidth, int layoutParamsHeight) {
v.measure(layoutParamsWidth, layoutParamsHeight);
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
? LayoutParams.WRAP_CONTENT
: (int)(targtetHeight * interpolatedTime);
v.requestLayout();
}
@Override
public boolean willChangeBounds() {
return true;
}
};
// 1dp/ms
a.setDuration((int)(targtetHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
}
public static void collapse(final View v) {
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;
}
};
// 1dp/ms
a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density));
v.startAnimation(a);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment