Android - 如何使用交叉淡入淡出动画更改状态栏颜色

3
我希望实现状态栏颜色的变化,并使用淡入淡出动画,如下所示。请问我该如何做呢? 状态栏颜色变化动画: statusbar color change animation 谢谢! 编辑(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()
    }
})
2个回答

1

已经有一个在Github上托管的库,展示了如何使用圆形揭示效果动画更改工具栏和状态栏颜色。看看这个。

以下是代码片段:

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);
    }
}

enter image description here

希望它有所帮助。

如果你的回答只是关于发布一个链接,请在评论中进行。 - 2Dee

0
这是如何以编程方式更改状态栏颜色的方法。现在你可以在你的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);
}

1
这只是简单地设置状态栏的颜色,但用户不会看到任何动画。我认为它应该是想要通过动画来改变状态栏的颜色。 - Mayank Bhatnagar
1
这些代码只是改变颜色而没有动画效果,不是我所要求的。 - Dhemas Eka

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接