private static AlphaAnimation mHideAnimation = null;
private static AlphaAnimation mShowAnimation = null;
private static final long ANIM_DURATION = 999;
private static final long CLOSE_DURATION = 1500;
/**
* View渐隐动画效果
*/
public static void setHideAnimation(View view) {
if (null == view) {
return;
}
mHideAnimation = new AlphaAnimation(1.0f, 0.0f);
mHideAnimation.setDuration(ANIM_DURATION);
mHideAnimation.setFillAfter(true);
view.startAnimation(mHideAnimation);
view.postDelayed(() -> {
mHideAnimation.cancel();
mHideAnimation = null;
}, CLOSE_DURATION);
}
/**
* View渐现动画效果
*/
public static void setShowAnimation(View view) {
if (null == view) {
return;
}
mShowAnimation = new AlphaAnimation(0.0f, 1.0f);
mShowAnimation.setDuration(ANIM_DURATION);
mShowAnimation.setFillAfter(true);
view.startAnimation(mShowAnimation);
view.postDelayed(() -> {
mShowAnimation.cancel();
mShowAnimation = null;
}, CLOSE_DURATION);
}
设置View渐隐渐显
最新推荐文章于 2022-10-13 17:52:30 发布
本文介绍了如何在Android中实现View的渐隐渐现动画效果。通过静态内部类定义了AlphaAnimation,设置动画时长,并使用startAnimation和postDelayed方法来控制动画的启动和结束。这些方法可以方便地在应用中为View添加平滑的显示和隐藏动画。
摘要由CSDN通过智能技术生成