Created
April 16, 2015 10:40
-
-
Save jiparis/222d6d8e868528ee179b to your computer and use it in GitHub Desktop.
Android Collapse Animation (from http://stackoverflow.com/a/13984525/3344594)
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.myapp.anim; | |
import android.view.View; | |
import android.view.ViewGroup.LayoutParams; | |
import android.view.animation.Animation; | |
import android.view.animation.Transformation; | |
import android.widget.LinearLayout; | |
public class CollapseAnimation extends Animation { | |
public final static int COLLAPSE = 1; | |
public final static int EXPAND = 0; | |
private View mView; | |
private int mEndHeight; | |
private int mType; | |
private LinearLayout.LayoutParams mLayoutParams; | |
public CollapseAnimation(View view, int duration, int type) { | |
setDuration(duration); | |
mView = view; | |
mEndHeight = mView.getHeight(); | |
mLayoutParams = ((LinearLayout.LayoutParams) view.getLayoutParams()); | |
mType = type; | |
if(mType == EXPAND) { | |
mLayoutParams.height = 0; | |
} else { | |
mLayoutParams.height = LayoutParams.WRAP_CONTENT; | |
} | |
view.setVisibility(View.VISIBLE); | |
} | |
public int getHeight(){ | |
return mView.getHeight(); | |
} | |
public void setHeight(int height){ | |
mEndHeight = height; | |
} | |
@Override | |
protected void applyTransformation(float interpolatedTime, Transformation t) { | |
super.applyTransformation(interpolatedTime, t); | |
if (interpolatedTime < 1.0f) { | |
if(mType == EXPAND) { | |
mLayoutParams.height = (int)(mEndHeight * interpolatedTime); | |
} else { | |
mLayoutParams.height = (int) (mEndHeight * (1 - interpolatedTime)); | |
} | |
mView.requestLayout(); | |
} else { | |
if(mType == EXPAND) { | |
mLayoutParams.height = LayoutParams.WRAP_CONTENT; | |
mView.requestLayout(); | |
}else{ | |
mView.setVisibility(View.GONE); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment