欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

android view淡入淡出动画优化

程序员文章站 2022-03-18 17:08:56
...

这个基本是看了文档之后所写,之前一直没有发现也不知道有没有效果,反正记录一点是一点,打算以后用到的话就可以用上,基本上从文档上照搬,这只是提示自己以后可以用而已。

文档解释:

  1. 对于正在淡入的视图,请将alpha值设置为0和能见度VISIBLE。(请记住,它最初被设置为GONE)这使视图可见,但完全透明。
  2. 对于正在淡入的视图,将其alpha值从01。对于正在消失的视图,将alpha值从10.
  3. 使用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