From 513b67ae20f2648cae3a8f1a17b55c11e69620a7 Mon Sep 17 00:00:00 2001 From: Fabian Kozynski Date: Fri, 19 Feb 2021 17:23:58 -0500 Subject: [PATCH] Animate tiles Properly animate background color of the tiles. Test: manual, light and dark mode Bug: 171319433 Change-Id: I67e3f31240f665e948a417262290cab03a1f00e6 --- .../qs/tileimpl/QSTileViewHorizontal.kt | 42 +++++++++++++++++-- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSTileViewHorizontal.kt b/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSTileViewHorizontal.kt index c98de8cb8fabd..07d48f32ff207 100644 --- a/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSTileViewHorizontal.kt +++ b/packages/SystemUI/src/com/android/systemui/qs/tileimpl/QSTileViewHorizontal.kt @@ -16,6 +16,7 @@ package com.android.systemui.qs.tileimpl +import android.animation.ValueAnimator import android.content.Context import android.content.res.ColorStateList import android.graphics.Color @@ -31,12 +32,17 @@ import com.android.systemui.plugins.qs.QSIconView import com.android.systemui.plugins.qs.QSTile import com.android.systemui.qs.tileimpl.QSTileImpl.getColorForState +// Placeholder +private const val CORNER_RADIUS = 40f + class QSTileViewHorizontal( context: Context, icon: QSIconView ) : QSTileView(context, icon, false) { private var paintDrawable: PaintDrawable? = null + private var paintColor = Color.TRANSPARENT + private var paintAnimator: ValueAnimator? = null init { orientation = HORIZONTAL @@ -70,8 +76,8 @@ class QSTileViewHorizontal( override fun newTileBackground(): Drawable? { val d = super.newTileBackground() if (paintDrawable == null) { - paintDrawable = PaintDrawable(Color.WHITE).apply { - setCornerRadius(50f) + paintDrawable = PaintDrawable(paintColor).apply { + setCornerRadius(CORNER_RADIUS) } } if (d is RippleDrawable) { @@ -94,9 +100,39 @@ class QSTileViewHorizontal( override fun handleStateChanged(state: QSTile.State) { super.handleStateChanged(state) - paintDrawable?.setTint(getCircleColor(state.state)) mSecondLine.setTextColor(mLabel.textColors) mLabelContainer.background = null + + val allowAnimations = animationsEnabled() && paintColor != Color.TRANSPARENT + val newColor = getCircleColor(state.state) + if (allowAnimations) { + animateToNewState(newColor) + } else { + if (newColor != paintColor) { + clearAnimator() + paintDrawable?.paint?.color = newColor + paintDrawable?.invalidateSelf() + } + } + paintColor = newColor + } + + private fun animateToNewState(newColor: Int) { + if (newColor != paintColor) { + clearAnimator() + paintAnimator = ValueAnimator.ofArgb(paintColor, newColor) + .setDuration(QSIconViewImpl.QS_ANIM_LENGTH).apply { + addUpdateListener { animation: ValueAnimator -> + paintDrawable?.paint?.color = animation.animatedValue as Int + paintDrawable?.invalidateSelf() + } + start() + } + } + } + + private fun clearAnimator() { + paintAnimator?.cancel()?.also { paintAnimator = null } } override fun handleExpand(dualTarget: Boolean) {}