From 78ebd9650a450068f5f76cc1a3a3a59b2e066ba3 Mon Sep 17 00:00:00 2001 From: gangzhu Date: Sat, 3 Dec 2022 15:19:52 +0800 Subject: [PATCH] anchor position use offset after passing view. --- .../main/java/com/takusemba/spotlight/SpotlightView.kt | 10 ++++++---- .../src/main/java/com/takusemba/spotlight/Target.kt | 4 ++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/spotlight/src/main/java/com/takusemba/spotlight/SpotlightView.kt b/spotlight/src/main/java/com/takusemba/spotlight/SpotlightView.kt index 02cf0ba..b889d75 100644 --- a/spotlight/src/main/java/com/takusemba/spotlight/SpotlightView.kt +++ b/spotlight/src/main/java/com/takusemba/spotlight/SpotlightView.kt @@ -115,10 +115,12 @@ internal class SpotlightView @JvmOverloads constructor( addView(target.overlay, MATCH_PARENT, MATCH_PARENT) this.target = target.apply { // adjust anchor in case where custom container is set. - val location = IntArray(2) - getLocationInWindow(location) - val offset = PointF(location[0].toFloat(), location[1].toFloat()) - anchor.offset(-offset.x, -offset.y) + if (isNeedOffset) { + val location = IntArray(2) + getLocationInWindow(location) + val offset = PointF(location[0].toFloat(), location[1].toFloat()) + anchor.offset(-offset.x, -offset.y) + } } this.shapeAnimator?.removeAllListeners() this.shapeAnimator?.removeAllUpdateListeners() diff --git a/spotlight/src/main/java/com/takusemba/spotlight/Target.kt b/spotlight/src/main/java/com/takusemba/spotlight/Target.kt index 4a16fc2..28e46bf 100644 --- a/spotlight/src/main/java/com/takusemba/spotlight/Target.kt +++ b/spotlight/src/main/java/com/takusemba/spotlight/Target.kt @@ -11,6 +11,7 @@ import com.takusemba.spotlight.shape.Shape * Target represents the spot that Spotlight will cast. */ class Target( + var isNeedOffset: Boolean = false, val anchor: PointF, val shape: Shape, val effect: Effect, @@ -24,6 +25,7 @@ class Target( */ class Builder { + private var isNeedOffset: Boolean = false private var anchor: PointF = DEFAULT_ANCHOR private var shape: Shape = DEFAULT_SHAPE private var effect: Effect = DEFAULT_EFFECT @@ -38,6 +40,7 @@ class Target( view.getLocationInWindow(location) val x = location[0] + view.width / 2f val y = location[1] + view.height / 2f + isNeedOffset = true setAnchor(x, y) } @@ -84,6 +87,7 @@ class Target( } fun build() = Target( + isNeedOffset = isNeedOffset, anchor = anchor, shape = shape, effect = effect,