Skip to content

Commit

Permalink
Implementation balloon highlight animation
Browse files Browse the repository at this point in the history
  • Loading branch information
svrlopatrik committed Jan 24, 2021
1 parent 4afe56b commit 485f805
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 0 deletions.
71 changes: 71 additions & 0 deletions balloon/src/main/java/com/skydoves/balloon/Balloon.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.ViewOutlineProvider
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.FrameLayout
import android.widget.PopupWindow
import android.widget.TextView
import androidx.annotation.AnimRes
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.annotation.DimenRes
Expand Down Expand Up @@ -457,6 +460,48 @@ class Balloon(
}
}

private fun getBalloonHighlightAnimation(): Animation? {
val animRes = if(builder.balloonHighlightAnimationStyle == NO_INT_VALUE) {
when(builder.balloonHighlightAnimation) {
BalloonHighlightAnimation.HEARTBEAT -> {
if(builder.isVisibleArrow) {
when(builder.arrowOrientation) {
ArrowOrientation.TOP -> R.anim.heartbeat_bottom_balloon_library
ArrowOrientation.BOTTOM -> R.anim.heartbeat_top_balloon_library
ArrowOrientation.LEFT -> R.anim.heartbeat_right_balloon_library
ArrowOrientation.RIGHT -> R.anim.heartbeat_left_balloon_library
}
} else {
R.anim.heartbeat_center_balloon_library
}
}
else -> return null
}
} else {
builder.balloonHighlightAnimationStyle
}

return AnimationUtils.loadAnimation(context, animRes)
}

private fun startBalloonHighlightAnimation() {
binding.balloon.post {
Handler(Looper.getMainLooper()).postDelayed({
getBalloonHighlightAnimation()?.let { animation -> binding.balloon.startAnimation(animation) }
}, builder.balloonHighlightAnimationStartDelay)
}
}

private fun stopBalloonHighlightAnimation() {
binding.balloon.apply {
animation?.apply {
cancel()
reset()
}
clearAnimation()
}
}

@MainThread
private inline fun show(anchor: View, crossinline block: () -> Unit) {
if (!isShowing && !destroyed && !context.isFinishing() &&
Expand Down Expand Up @@ -493,6 +538,7 @@ class Balloon(
showOverlayWindow(anchor)

applyBalloonAnimation()
startBalloonHighlightAnimation()
block()
}
} else if (builder.dismissWhenShowAgain) {
Expand Down Expand Up @@ -815,6 +861,7 @@ class Balloon(
/** sets a [OnBalloonDismissListener] to the popup. */
fun setOnBalloonDismissListener(onBalloonDismissListener: OnBalloonDismissListener?) {
this.bodyWindow.setOnDismissListener {
stopBalloonHighlightAnimation()
this@Balloon.dismiss()
onBalloonDismissListener?.onBalloonDismiss()
}
Expand Down Expand Up @@ -1242,6 +1289,18 @@ class Balloon(
@set:JvmSynthetic
var circularDuration: Long = 500L

@JvmField
@set:JvmSynthetic
var balloonHighlightAnimation: BalloonHighlightAnimation = BalloonHighlightAnimation.NONE

@JvmField @StyleRes
@set:JvmSynthetic
var balloonHighlightAnimationStyle: Int = NO_INT_VALUE

@JvmField
@set:JvmSynthetic
var balloonHighlightAnimationStartDelay: Long = 0L

@JvmField
@set:JvmSynthetic
var preferenceName: String? = null
Expand Down Expand Up @@ -1700,6 +1759,18 @@ class Balloon(
this.circularDuration = value
}

/** sets the balloon highlight animation using [BalloonHighlightAnimation]. */
fun setBalloonHighlightAnimation(value: BalloonHighlightAnimation, startDelay: Long = 0L): Builder = apply {
this.balloonHighlightAnimation = value
this.balloonHighlightAnimationStartDelay = startDelay
}

/** sets the balloon highlight animation using custom xml animation resource file. */
fun setBalloonHighlightAnimationResource(@AnimRes value: Int, startDelay: Long = 0L): Builder = apply {
this.balloonHighlightAnimationStyle = value
this.balloonHighlightAnimationStartDelay = startDelay
}

/** sets a [OnBalloonClickListener] to the popup. */
fun setOnBalloonClickListener(value: OnBalloonClickListener): Builder = apply {
this.onBalloonClickListener = value
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.skydoves.balloon

enum class BalloonHighlightAnimation {
NONE,
HEARTBEAT
}
14 changes: 14 additions & 0 deletions balloon/src/main/res/anim/heartbeat_bottom_balloon_library.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<scale
android:duration="800"
android:fromXScale="100%"
android:fromYScale="100%"
android:toXScale="90%"
android:toYScale="90%"
android:pivotX="50%"
android:pivotY="0%"
android:repeatMode="reverse"
android:repeatCount="infinite" />
</set>
14 changes: 14 additions & 0 deletions balloon/src/main/res/anim/heartbeat_center_balloon_library.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<scale
android:duration="800"
android:fromXScale="100%"
android:fromYScale="100%"
android:toXScale="90%"
android:toYScale="90%"
android:pivotX="50%"
android:pivotY="50%"
android:repeatMode="reverse"
android:repeatCount="infinite" />
</set>
14 changes: 14 additions & 0 deletions balloon/src/main/res/anim/heartbeat_left_balloon_library.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<scale
android:duration="800"
android:fromXScale="100%"
android:fromYScale="100%"
android:toXScale="90%"
android:toYScale="90%"
android:pivotX="100%"
android:pivotY="50%"
android:repeatMode="reverse"
android:repeatCount="infinite" />
</set>
14 changes: 14 additions & 0 deletions balloon/src/main/res/anim/heartbeat_right_balloon_library.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<scale
android:duration="800"
android:fromXScale="100%"
android:fromYScale="100%"
android:toXScale="90%"
android:toYScale="90%"
android:pivotX="0%"
android:pivotY="50%"
android:repeatMode="reverse"
android:repeatCount="infinite" />
</set>
14 changes: 14 additions & 0 deletions balloon/src/main/res/anim/heartbeat_top_balloon_library.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<scale
android:duration="800"
android:fromXScale="100%"
android:fromYScale="100%"
android:toXScale="90%"
android:toYScale="90%"
android:pivotX="50%"
android:pivotY="100%"
android:repeatMode="reverse"
android:repeatCount="infinite" />
</set>

0 comments on commit 485f805

Please sign in to comment.