From b66d34199395f691c861ed280d0d31fefa7f5d78 Mon Sep 17 00:00:00 2001 From: skydoves Date: Sun, 16 Feb 2020 20:43:14 +0900 Subject: [PATCH] Implement circularUnRevealed animation when dismiss --- .../factory/ProfileBalloonFactory.kt | 2 +- .../main/java/com/skydoves/balloon/Balloon.kt | 10 +++- .../com/skydoves/balloon/ViewExtension.kt | 52 +++++++++++++++---- 3 files changed, 53 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/skydoves/balloondemo/factory/ProfileBalloonFactory.kt b/app/src/main/java/com/skydoves/balloondemo/factory/ProfileBalloonFactory.kt index 8266c31d..45d53389 100644 --- a/app/src/main/java/com/skydoves/balloondemo/factory/ProfileBalloonFactory.kt +++ b/app/src/main/java/com/skydoves/balloondemo/factory/ProfileBalloonFactory.kt @@ -36,7 +36,7 @@ class ProfileBalloonFactory : Balloon.Factory() { setHeight(250) setCornerRadius(4f) setBackgroundColorResource(R.color.white) - setBalloonAnimation(BalloonAnimation.OVERSHOOT) + setBalloonAnimation(BalloonAnimation.CIRCULAR) setDismissWhenShowAgain(true) setLifecycleOwner(lifecycle) } diff --git a/balloon/src/main/java/com/skydoves/balloon/Balloon.kt b/balloon/src/main/java/com/skydoves/balloon/Balloon.kt index bb201869..9b04ff08 100644 --- a/balloon/src/main/java/com/skydoves/balloon/Balloon.kt +++ b/balloon/src/main/java/com/skydoves/balloon/Balloon.kt @@ -475,7 +475,15 @@ class Balloon( fun dismiss() { if (this.isShowing) { this.isShowing = false - this.bodyWindow.dismiss() + + val dismissWindow: () -> Unit = { this.bodyWindow.dismiss() } + if (this.builder.balloonAnimation == BalloonAnimation.CIRCULAR) { + this.bodyWindow.contentView.circularUnRevealed() { + dismissWindow() + } + } else { + dismissWindow() + } } } diff --git a/balloon/src/main/java/com/skydoves/balloon/ViewExtension.kt b/balloon/src/main/java/com/skydoves/balloon/ViewExtension.kt index 5d613ce8..3930e443 100644 --- a/balloon/src/main/java/com/skydoves/balloon/ViewExtension.kt +++ b/balloon/src/main/java/com/skydoves/balloon/ViewExtension.kt @@ -16,12 +16,16 @@ package com.skydoves.balloon +import android.animation.Animator +import android.animation.AnimatorListenerAdapter import android.os.Build import android.view.View import android.view.ViewAnimationUtils +import androidx.annotation.MainThread import kotlin.math.max /** makes visible or invisible a View align the value parameter. */ +@MainThread internal fun View.visible(value: Boolean) { if (value) { this.visibility = View.VISIBLE @@ -30,8 +34,46 @@ internal fun View.visible(value: Boolean) { } } +@MainThread /** shows circular revealed animation to a view. */ internal fun View.circularRevealed() { + doOnLayoutChanged { + val view = this + ViewAnimationUtils.createCircularReveal( + view, + (view.left + view.right) / 2, + (view.top + view.bottom) / 2, + 0f, + max(view.width, view.height).toFloat()).apply { + duration = 500 + start() + } + } +} + +@MainThread +internal fun View.circularUnRevealed(doAfterFinish: () -> Unit) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + val view = this + ViewAnimationUtils.createCircularReveal( + view, + (view.left + view.right) / 2, + (view.top + view.bottom) / 2, + max(view.width, view.height).toFloat(), + 0f).apply { + duration = 500 + start() + }.addListener(object : AnimatorListenerAdapter() { + override fun onAnimationEnd(animation: Animator?) { + super.onAnimationEnd(animation) + doAfterFinish() + } + }) + } +} + +@MainThread +internal fun View.doOnLayoutChanged(block: () -> Unit) { this.addOnLayoutChangeListener( object : View.OnLayoutChangeListener { override fun onLayoutChange( @@ -47,15 +89,7 @@ internal fun View.circularRevealed() { ) { view.removeOnLayoutChangeListener(this) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - ViewAnimationUtils.createCircularReveal( - view, - (view.left + view.right) / 2, - (view.top + view.bottom) / 2, - 0f, - max(view.width, view.height).toFloat()).run { - duration = 500 - start() - } + block() } } })