我正在编写一个简单的应用程序来实现符合 Material 设计的抽屉导航,如这篇 Stack 帖子所述:https://stackoverflow.com/a/27153313/1621380
ScrimInsetsFrameLayout
工作得很好,一切都很好,直到我尝试以编程方式更改状态栏的颜色。我的应用使用 Palette API 来动态更改工具栏和状态栏的颜色。
我正在使用 property animation api为工具栏制作动画,效果很好!但我尝试在状态栏上做同样的动画,但它似乎不想做动画。这是一个例子
这是我的动画师的代码:
public static void fadeStatusBar(final DrawerLayout layout, Integer from, Integer to) {
if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), from, to);
colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animator) {
layout.setStatusBarBackgroundColor((Integer) animator.getAnimatedValue());
}
});
colorAnimation.start();
}
}
注意:相同的代码正在淡出工具栏,因此它被证明是有效的。
我的问题是;有谁知道在使用 DrawerLayout.setStatusBarBackgorundColour()
时实现平滑过渡的方法吗?
注意:我使用了 Window 方法 window.setStatusBarColor()
方法,它的动画效果很好,但在拉入 NavDrawer 时会破坏“透明状态栏”。
最佳答案
根据@adneal 对我最初问题的评论,答案是在动画期间使 View 无效。
DrawerLayout calculates the top inset and draws the status bar background itself. So, you just need to make a call to View.invalidate to force it to redraw while the animation is being updated. @adneal
我已经更新了我的代码
ValueAnimator colorAnimation = ValueAnimator.ofArgb(from, to);
colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animator) {
layout.invalidate();
layout.setStatusBarBackgroundColor((Integer) animator.getAnimatedValue());
}
});
colorAnimation.start();
对于任何想知道为什么这有效的人,我发现这个堆栈帖子很有启发性:When it's necessary to execute invalidate() on a View?
关于java - DrawerLayout 上的动画状态栏颜色 - Material Design,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27913314/