android view淡入淡出动画优化
程序员文章站
2022-03-18 17:08:56
...
这个基本是看了文档之后所写,之前一直没有发现也不知道有没有效果,反正记录一点是一点,打算以后用到的话就可以用上,基本上从文档上照搬,这只是提示自己以后可以用而已。
文档解释:
- 对于正在淡入的视图,请将alpha值设置为
0
和能见度VISIBLE
。(请记住,它最初被设置为GONE
)这使视图可见,但完全透明。 - 对于正在淡入的视图,将其alpha值从
0
到1
。对于正在消失的视图,将alpha值从1
到0
. - 使用
onAnimationEnd()
在Animator.AnimatorListener
,将逐渐消失的视图的可见性设置为GONE
。即使alpha值是0
,将视图的可见性设置为GONE
防止视图占用布局空间,并从布局计算中省略它,从而加快处理速度。
public class CrossfadeActivity extends Activity {
private View mContentView;
private View mLoadingView;
private int mShortAnimationDuration;
...
private void crossfade() {
// Set the content view to 0% opacity but visible, so that it is visible
// (but fully transparent) during the animation.
mContentView.setAlpha(0f);
mContentView.setVisibility(View.VISIBLE);
// Animate the content view to 100% opacity, and clear any animation
// listener set on the view.
mContentView.animate()
.alpha(1f)
.setDuration(mShortAnimationDuration)
.setListener(null);
// Animate the loading view to 0% opacity. After the animation ends,
// set its visibility to GONE as an optimization step (it won't
// participate in layout passes, etc.)
mLoadingView.animate()
.alpha(0f)
.setDuration(mShortAnimationDuration)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mLoadingView.setVisibility(View.GONE);
}
});
}
}
kotlin
class CrossfadeActivity : Activity() {
private lateinit var mContentView: View
private lateinit var mLoadingView: View
private var mShortAnimationDuration: Int = 0
...
private fun crossfade() {
mContentView.apply {
// Set the content view to 0% opacity but visible, so that it is visible
// (but fully transparent) during the animation.
alpha = 0f
visibility = View.VISIBLE
// Animate the content view to 100% opacity, and clear any animation
// listener set on the view.
animate()
.alpha(1f)
.setDuration(mShortAnimationDuration.toLong())
.setListener(null)
}
// Animate the loading view to 0% opacity. After the animation ends,
// set its visibility to GONE as an optimization step (it won't
// participate in layout passes, etc.)
mLoadingView.animate()
.alpha(0f)
.setDuration(mShortAnimationDuration.toLong())
.setListener(object : AnimatorListenerAdapter() {
override fun onAnimationEnd(animation: Animator) {
mLoadingView.visibility = View.GONE
}
})
}
}
设置gone让系统不再绘制这个控件减少占用布局,到达优化效果
附上官方文档:
https://developer.android.google.cn/training/animation/reveal-or-hide-view
上一篇: Vue源码探究-虚拟DOM的渲染
下一篇: 前后端分离搭建的简单实现
推荐阅读
-
Android自定义view利用Xfermode实现动态文字加载动画
-
Android自定义view渐变圆形动画
-
Android自定义View实现饼状图带动画效果
-
Android自定义view仿QQ的Tab按钮动画效果(示例代码)
-
Android自定义view之围棋动画效果的实现
-
Android开发触摸touch事件(补间动画和自定义view使用方法)
-
android自定义view实现进度条动画、按钮渐变及录制状态控制
-
Android自定义View实现注销图案的加载动画
-
Android开发学习笔记——View动画和属性动画
-
Android动画开发之淡入淡出、缩放、旋转、平移、组合动画效果代码实现