import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
public class AnimationUtils {
public enum AnimationState{
STATE_SHOW,
STATE_HIDDEN,
STATE_SHOW_DOWN,
STATE_SHOW_UP,
STATE_SHOW_LEFT,
STATE_SHOW_RIGHT,
STATE_HIDDEN_DOWN,
STATE_HIDDEN_UP,
STATE_HIDDEN_LEFT,
STATE_HIDDEN_RIGHT
}
public static void showAndHiddenAnimation(final View view, AnimationState state, long duration){
view.post(new Runnable() {
@Override
public void run() {
int viewWidth=view.getMeasuredWidth()+50;
int viewHeight=view.getMeasuredHeight()+50;
float start = 0f;
float end = 0f;
if(state == AnimationState.STATE_SHOW || state == AnimationState.STATE_SHOW_DOWN || state == AnimationState.STATE_SHOW_UP
|| state == AnimationState.STATE_SHOW_LEFT || state == AnimationState.STATE_SHOW_RIGHT){
end = 1f;
view.setVisibility(View.VISIBLE);
} else
if(state == AnimationState.STATE_HIDDEN || state == AnimationState.STATE_HIDDEN_DOWN || state == AnimationState.STATE_HIDDEN_UP
|| state == AnimationState.STATE_HIDDEN_LEFT || state == AnimationState.STATE_HIDDEN_RIGHT){
start = 1f;
view.setVisibility(View.GONE);
}
AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation animation = new AlphaAnimation(start, end);
animation.setDuration(duration);
animation.setFillAfter(true);
if(state == AnimationState.STATE_SHOW_DOWN ){
TranslateAnimation translateAnimation = new TranslateAnimation(0,0,-viewHeight,0);
translateAnimation.setDuration(duration);
translateAnimation.setFillEnabled(true);
translateAnimation.setFillAfter(true);
animationSet.addAnimation(translateAnimation);
}else if(state == AnimationState.STATE_HIDDEN_DOWN){
TranslateAnimation translateAnimation = new TranslateAnimation(0,0,0,-viewHeight);
translateAnimation.setDuration(duration);
translateAnimation.setFillEnabled(true);
translateAnimation.setFillAfter(true);
animationSet.addAnimation(translateAnimation);
}else if(state == AnimationState.STATE_SHOW_UP ){
TranslateAnimation translateAnimation = new TranslateAnimation(0,0,viewHeight,0);
translateAnimation.setDuration(duration);
translateAnimation.setFillEnabled(true);
translateAnimation.setFillAfter(true);
animationSet.addAnimation(translateAnimation);
}else if(state == AnimationState.STATE_HIDDEN_UP){
TranslateAnimation translateAnimation = new TranslateAnimation(0,0,0,viewHeight);
translateAnimation.setDuration(duration);
translateAnimation.setFillEnabled(true);
translateAnimation.setFillAfter(true);
animationSet.addAnimation(translateAnimation);
}else if(state == AnimationState.STATE_SHOW_LEFT ){
TranslateAnimation translateAnimation = new TranslateAnimation(-viewWidth,0,0,0);
translateAnimation.setDuration(duration);
translateAnimation.setFillEnabled(true);
translateAnimation.setFillAfter(true);
animationSet.addAnimation(translateAnimation);
}else if(state == AnimationState.STATE_HIDDEN_LEFT){
TranslateAnimation translateAnimation = new TranslateAnimation(0,-viewWidth,0,0);
translateAnimation.setDuration(duration);
translateAnimation.setFillEnabled(true);
translateAnimation.setFillAfter(true);
animationSet.addAnimation(translateAnimation);
}else if(state == AnimationState.STATE_SHOW_RIGHT ){
TranslateAnimation translateAnimation = new TranslateAnimation(viewWidth,0,0,0);
translateAnimation.setDuration(duration);
translateAnimation.setFillEnabled(true);
translateAnimation.setFillAfter(true);
animationSet.addAnimation(translateAnimation);
}else if(state == AnimationState.STATE_HIDDEN_RIGHT){
TranslateAnimation translateAnimation = new TranslateAnimation(0,viewWidth,0,0);
translateAnimation.setDuration(duration);
translateAnimation.setFillEnabled(true);
translateAnimation.setFillAfter(true);
animationSet.addAnimation(translateAnimation);
}
animationSet.addAnimation(animation);
animationSet.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
view.clearAnimation();
}
});
view.setAnimation(animationSet);
animationSet.start();
}
});
}
}
例子 弹出显示
AnimationUtils.showAndHiddenAnimation(view,AnimationUtils.AnimationState.STATE_HIDDEN_UP,300);
收起隐藏
AnimationUtils.showAndHiddenAnimation(view, AnimationUtils.AnimationState.STATE_SHOW_UP, 300);
|