1. 程式人生 > >android view淡入淡出動畫優化

android view淡入淡出動畫優化

這個基本是看了文件之後所寫,之前一直沒有發現也不知道有沒有效果,反正記錄一點是一點,打算以後用到的話就可以用上,基本上從文件上照搬,這只是提示自己以後可以用而已。

文件解釋:

  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