淡入淡出

来源:互联网 发布:java每5个数输出一行 编辑:程序博客网 时间:2024/04/30 13:37
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;


/**
 * 淡入淡出
 * 
 * @author l
 * 
 */
public class FadeUitl {
// 淡出
// ①谁在执行淡出动画:当前正在展示的界面
// ②淡出的时间
// ③在淡出执行完成后,删除淡出的界面
// 淡入
// ①谁在执行淡入动画:目标界面
// ②淡入的时间
// ③淡入的等待时间:淡出界面的执行动画时间

private static Handler handler=new Handler(){
public void handleMessage(Message msg) {
View currentView=(View) msg.obj;
ViewGroup middle = (ViewGroup) currentView.getParent();
middle.removeView(currentView);
}
};


/**
* 淡出

* @param currentView
* @param duration
*/
public static void fadeOut(final View currentView, long duration) {
// where 1.0 means fully opaque and 0.0 means fully transparent.
AlphaAnimation animation = new AlphaAnimation(1, 0);
animation.setDuration(duration);
// 动画执行完成后,停留在执行的终点上
// animation.setFillAfter(true);
// 动画执行完成后,删除currentView
animation.setAnimationListener(new AnimationListener() {

@Override
public void onAnimationStart(Animation animation) {
}

@Override
public void onAnimationRepeat(Animation animation) {
}

@Override
public void onAnimationEnd(Animation animation) {
// 删除currentView
// middle.removeView(child1);
// ViewGroup middle = (ViewGroup) currentView.getParent();
// middle.removeView(currentView);

Message msg=Message.obtain();
msg.obj=currentView;
// handler.sendMessageDelayed(msg, 1);
handler.sendMessage(msg);

}
});
currentView.startAnimation(animation);
}


/**
* 淡入

* @param currentView
* @param duration
*/
public static void fadeIn(View targetView, long duration, long delay) {
// where 1.0 means fully opaque and 0.0 means fully transparent.
AlphaAnimation animation = new AlphaAnimation(0, 1);
animation.setDuration(duration);
// 延迟播放动画
animation.setStartOffset(delay);
targetView.startAnimation(animation);
}
}
0 0
原创粉丝点击