From e67ee6702db4b4a68a9ea3811516465deed3e766 Mon Sep 17 00:00:00 2001 From: Fredrik Berglund Date: Mon, 15 Oct 2018 13:51:18 +0200 Subject: [PATCH] Adds animation to overlay Its duration matches that of the enter/exit animation --- .../flashbar/FlashbarContainerView.kt | 23 ++++++++++++++++++- flashbar/src/main/res/values/colors.xml | 1 + 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/flashbar/src/main/java/com/andrognito/flashbar/FlashbarContainerView.kt b/flashbar/src/main/java/com/andrognito/flashbar/FlashbarContainerView.kt index ebf6ac5..f848d07 100644 --- a/flashbar/src/main/java/com/andrognito/flashbar/FlashbarContainerView.kt +++ b/flashbar/src/main/java/com/andrognito/flashbar/FlashbarContainerView.kt @@ -1,5 +1,6 @@ package com.andrognito.flashbar +import android.animation.ArgbEvaluator import android.app.Activity import android.content.Context import android.graphics.Rect @@ -23,6 +24,9 @@ import com.andrognito.flashbar.util.afterMeasured import com.andrognito.flashbar.util.getNavigationBarPosition import com.andrognito.flashbar.util.getNavigationBarSizeInPx import com.andrognito.flashbar.util.getRootView +import android.animation.ValueAnimator +import android.support.v4.content.ContextCompat + /** * Container withView matching the height and width of the parent to hold a FlashbarView. @@ -32,6 +36,8 @@ import com.andrognito.flashbar.util.getRootView internal class FlashbarContainerView(context: Context) : RelativeLayout(context), DismissCallbacks { + private val translucent = ContextCompat.getColor(context, R.color.translucent) + internal lateinit var parentFlashbar: Flashbar private lateinit var flashbarView: FlashbarView @@ -44,6 +50,7 @@ internal class FlashbarContainerView(context: Context) private var onBarDismissListener: Flashbar.OnBarDismissListener? = null private var onTapOutsideListener: Flashbar.OnTapListener? = null private var overlayColor: Int? = null + private var overlayColorAnimator : ValueAnimator? = null private var iconAnimBuilder: FlashAnimIconBuilder? = null private var duration = DURATION_INDEFINITE @@ -101,7 +108,11 @@ internal class FlashbarContainerView(context: Context) isHapticFeedbackEnabled = true if (showOverlay) { - setBackgroundColor(overlayColor!!) + overlayColorAnimator = ValueAnimator.ofObject(ArgbEvaluator(), translucent, overlayColor).also { + // Duration will only ever be used by setting currentPlayTime to a fraction of its value; + // we never start the animator + it.duration = 1000 + } if (overlayBlockable) { isClickable = true @@ -140,6 +151,8 @@ internal class FlashbarContainerView(context: Context) // Only add the withView to the parent once if (this.parent == null) activityRootView.addView(this) + overlayColorAnimator?.addUpdateListener { animator -> this.setBackgroundColor(animator.animatedValue as Int) } + activityRootView.afterMeasured { val enterAnim = enterAnimBuilder.withView(flashbarView).build() enterAnim.start(object : FlashAnim.InternalAnimListener { @@ -150,6 +163,9 @@ internal class FlashbarContainerView(context: Context) override fun onUpdate(progress: Float) { onBarShowListener?.onShowProgress(parentFlashbar, progress) + overlayColorAnimator?.duration?.let { duration -> + overlayColorAnimator?.currentPlayTime = (duration * progress).toLong() + } } override fun onStop() { @@ -250,6 +266,9 @@ internal class FlashbarContainerView(context: Context) override fun onUpdate(progress: Float) { onBarDismissListener?.onDismissProgress(parentFlashbar, progress) + overlayColorAnimator?.duration?.let { duration -> + overlayColorAnimator?.currentPlayTime = duration - (duration * progress).toLong() + } } override fun onStop() { @@ -260,6 +279,8 @@ internal class FlashbarContainerView(context: Context) performHapticFeedback(VIRTUAL_KEY) } + overlayColorAnimator?.removeAllUpdateListeners() + onBarDismissListener?.onDismissed(parentFlashbar, event) post { (parent as? ViewGroup)?.removeView(this@FlashbarContainerView) } diff --git a/flashbar/src/main/res/values/colors.xml b/flashbar/src/main/res/values/colors.xml index 7787818..dde8877 100644 --- a/flashbar/src/main/res/values/colors.xml +++ b/flashbar/src/main/res/values/colors.xml @@ -3,6 +3,7 @@ #8e8e8e #fff #94444444 + #0000 #22000000 #80272727 #FF333333