From 06b9aaa499660b6052a7529c814bdee31912416a Mon Sep 17 00:00:00 2001 From: skydoves Date: Fri, 23 Sep 2022 20:37:17 +0900 Subject: [PATCH] Implement getArrowForeground method and refactor setting foregrond for the arrow --- .../factory/ProfileBalloonFactory.kt | 3 +- .../main/res/drawable/background_gradient.xml | 25 +++++++++++ .../kotlin/com/skydoves/balloon/Balloon.kt | 42 +++++++------------ 3 files changed, 41 insertions(+), 29 deletions(-) create mode 100644 app/src/main/res/drawable/background_gradient.xml diff --git a/app/src/main/kotlin/com/skydoves/balloondemo/factory/ProfileBalloonFactory.kt b/app/src/main/kotlin/com/skydoves/balloondemo/factory/ProfileBalloonFactory.kt index d62c629e..e433a4d5 100644 --- a/app/src/main/kotlin/com/skydoves/balloondemo/factory/ProfileBalloonFactory.kt +++ b/app/src/main/kotlin/com/skydoves/balloondemo/factory/ProfileBalloonFactory.kt @@ -40,7 +40,8 @@ class ProfileBalloonFactory : Balloon.Factory() { setArrowPosition(0.5f) setCornerRadius(6f) setElevation(6) - setBackgroundColorResource(R.color.background800) + setBackgroundDrawableResource(R.drawable.background_gradient) + setArrowColorMatchBalloon(true) setBalloonAnimation(BalloonAnimation.CIRCULAR) setDismissWhenTouchOutside(true) setDismissWhenShowAgain(true) diff --git a/app/src/main/res/drawable/background_gradient.xml b/app/src/main/res/drawable/background_gradient.xml new file mode 100644 index 00000000..7df0f9ae --- /dev/null +++ b/app/src/main/res/drawable/background_gradient.xml @@ -0,0 +1,25 @@ + + + + + + \ No newline at end of file diff --git a/balloon/src/main/kotlin/com/skydoves/balloon/Balloon.kt b/balloon/src/main/kotlin/com/skydoves/balloon/Balloon.kt index 23caca44..0c5856df 100644 --- a/balloon/src/main/kotlin/com/skydoves/balloon/Balloon.kt +++ b/balloon/src/main/kotlin/com/skydoves/balloon/Balloon.kt @@ -268,49 +268,25 @@ public class Balloon private constructor( x = getArrowConstraintPositionX(anchor) y = binding.balloonCard.y + binding.balloonCard.height - SIZE_ARROW_BOUNDARY ViewCompat.setElevation(this, builder.arrowElevation) - if (builder.arrowColorMatchBalloon && isAPILevelHigherThan23()) { - foreground = BitmapDrawable( - resources, - adjustArrowColorByMatchingCardBackground( - this, - x, - binding.balloonCard.height.toFloat() - ) - ) - } + foreground = getArrowForeground(x, binding.balloonCard.height.toFloat()) } ArrowOrientation.TOP -> { rotation = 0f x = getArrowConstraintPositionX(anchor) y = binding.balloonCard.y - builder.arrowSize + SIZE_ARROW_BOUNDARY - if (builder.arrowColorMatchBalloon && isAPILevelHigherThan23()) { - foreground = - BitmapDrawable(resources, adjustArrowColorByMatchingCardBackground(this, x, 0f)) - } + foreground = getArrowForeground(x, 0f) } ArrowOrientation.START -> { rotation = -90f x = binding.balloonCard.x - builder.arrowSize + SIZE_ARROW_BOUNDARY y = getArrowConstraintPositionY(anchor) - if (builder.arrowColorMatchBalloon && isAPILevelHigherThan23()) { - foreground = - BitmapDrawable(resources, adjustArrowColorByMatchingCardBackground(this, 0f, y)) - } + foreground = getArrowForeground(0f, y) } ArrowOrientation.END -> { rotation = 90f x = binding.balloonCard.x + binding.balloonCard.width - SIZE_ARROW_BOUNDARY y = getArrowConstraintPositionY(anchor) - if (builder.arrowColorMatchBalloon && isAPILevelHigherThan23()) { - foreground = BitmapDrawable( - resources, - adjustArrowColorByMatchingCardBackground( - this, - binding.balloonCard.width.toFloat(), - y - ) - ) - } + foreground = getArrowForeground(binding.balloonCard.width.toFloat(), y) } } visible(builder.isVisibleArrow) @@ -318,6 +294,16 @@ public class Balloon private constructor( } } + /** Returns [BitmapDrawable] that will be used for the foreground of the arrow. */ + private fun ImageView.getArrowForeground(x: Float, y: Float): BitmapDrawable? { + return if (builder.arrowColorMatchBalloon && isAPILevelHigherThan23()) { + BitmapDrawable( + resources, + adjustArrowColorByMatchingCardBackground(this, x, y) + ) + } else null + } + /** * Calculate the color at arrow position from balloonCard. The color is then set as a foreground to the arrow. *