【问题标题】:Android - How to change status bar color with crossfade animation [duplicate]Android - 如何使用交叉淡入淡出动画更改状态栏颜色[重复]
【发布时间】:2018-12-11 12:03:21
【问题描述】:

我想用交叉淡入淡出动画实现变色状态栏,如下所示。我能知道怎么做吗?

状态栏变色动画:

谢谢!

已编辑(2018 年 7 月 7 日) - 伙计们,这不是重复的

不管怎样,我终于找到了使用 ValueAnimator 和 ArgbEvaluator 的解决方案。

val mColorAnimation = ValueAnimator.ofObject(ArgbEvaluator(), firstColor, secondColor, thirdColor, fourthColor) 

mColorAnimation.duration = (pageCount - 1) * 10000000000L

mColorAnimation.addUpdateListener { animator ->
    val color = animator.animatedValue as Int

    // change status, navigation, and action bar color
    window.statusBarColor = color
    window.navigationBarColor = color
    supportActionBar?.setBackgroundDrawable(ColorDrawable(color))
}

main_viewpager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener{
    override fun onPageScrollStateChanged(state: Int) { /* unused */ }
    override fun onPageSelected(position: Int) { /* unused */ }

    override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
        mColorAnimation.currentPlayTime = ((positionOffset + position) * 10000000000).toLong()
    }
})

【问题讨论】:

    标签: android user-interface animation statusbar


    【解决方案1】:

    github 上已经有一个库来展示如何使用圆形显示效果更改工具栏和状态栏颜色的动画。 Check this out.

    这里是代码sn-p

    public class MainActivity extends Activity {
    
        private View mRevealView;
        private View mRevealBackgroundView;
        private Toolbar mToolbar;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mToolbar = (Toolbar) findViewById(R.id.appbar);
            mToolbar.setTitle(getString(R.string.app_name));
    
            mRevealView = findViewById(R.id.reveal);
            mRevealBackgroundView = findViewById(R.id.revealBackground);
    
            ToggleButton toggleButton = (ToggleButton) findViewById(R.id.toggleButton);
            toggleButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean on = ((ToggleButton) v).isChecked();
    
                    if (on) {
                        animateAppAndStatusBar(R.color.primary, R.color.accent);
                    } else {
                        animateAppAndStatusBar(R.color.accent, R.color.primary);
                    }
                }
            });
    
            setActionBar(mToolbar);
        }
    
        private void animateAppAndStatusBar(int fromColor, final int toColor) {
            Animator animator = ViewAnimationUtils.createCircularReveal(
                    mRevealView,
                    mToolbar.getWidth() / 2,
                    mToolbar.getHeight() / 2, 0,
                    mToolbar.getWidth() / 2);
    
            animator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationStart(Animator animation) {
                    mRevealView.setBackgroundColor(getResources().getColor(toColor));
                }
            });
    
            mRevealBackgroundView.setBackgroundColor(getResources().getColor(fromColor));
            animator.setStartDelay(200);
            animator.setDuration(125);
            animator.start();
            mRevealView.setVisibility(View.VISIBLE);
        }
    }
    

    希望对你有帮助。

    【讨论】:

    • 如果您的答案只是发布链接,请在 cmets 中进行。
    【解决方案2】:

    您可以通过这种方式以编程方式更改状态栏颜色。现在您可以在 ViewPager 监听器中更改状态栏颜色。

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        Window window = getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(Color.RED);
    }
    

    【讨论】:

    • 这只会设置状态栏颜色,但不会向用户显示任何动画。我认为它旨在通过动画更改状态栏颜色。
    • 那些代码只是改变颜色没有动画,不是我要求的
    猜你喜欢
    • 2016-05-12
    • 1970-01-01
    • 1970-01-01
    • 2021-07-06
    • 2021-01-28
    • 2013-07-28
    • 2014-09-16
    • 1970-01-01
    • 2011-09-06
    相关资源
    最近更新 更多