@@ -69,10 +69,10 @@ public class BadgeDrawable extends Drawable implements TextDrawableDelegate {
6969 private final float iconOnlyRadius ;
7070 private final float badgeWithTextRadius ;
7171 private final float badgeWidePadding ;
72- private final float badgeCenterX ;
73- private final float badgeCenterY ;
7472 private final Rect tmpRect ;
7573
74+ private float badgeCenterX ;
75+ private float badgeCenterY ;
7676 private int number = ICON_ONLY_BADGE_NUMBER ;
7777 private int maxCharacterCount ;
7878 private int alpha = 255 ;
@@ -128,33 +128,34 @@ private BadgeDrawable(@NonNull View anchorView, @Nullable ViewGroup customBadgeP
128128 this .context = anchorView .getContext ();
129129 Resources res = context .getResources ();
130130 tmpRect = new Rect ();
131-
131+ badgeBounds = new Rect ();
132132 shapeDrawable = new MaterialShapeDrawable ();
133- Rect anchorRect = new Rect ();
134- // Returns the visible bounds of the anchor view.
135- anchorView .getDrawingRect (anchorRect );
136- anchorRect .top += res .getDimensionPixelSize (R .dimen .mtrl_badge_vertical_offset );
137- if (customBadgeParent != null || VERSION .SDK_INT < VERSION_CODES .JELLY_BEAN_MR2 ) {
138- // Calculates coordinates relative to the parent.
139- ViewGroup viewGroup =
140- customBadgeParent == null ? (ViewGroup ) anchorView .getParent () : customBadgeParent ;
141- viewGroup .offsetDescendantRectToMyCoords (anchorView , anchorRect );
142- }
143-
144- badgeCenterX =
145- ViewCompat .getLayoutDirection (anchorView ) == View .LAYOUT_DIRECTION_LTR
146- ? anchorRect .right
147- : anchorRect .left ;
148- badgeCenterY = anchorRect .top ;
149133
150134 iconOnlyRadius = res .getDimensionPixelSize (R .dimen .mtrl_badge_icon_only_radius );
151135 badgeWidePadding = res .getDimensionPixelSize (R .dimen .mtrl_badge_long_text_horizontal_padding );
152136 badgeWithTextRadius = res .getDimensionPixelSize (R .dimen .mtrl_badge_with_text_radius );
153137
154- badgeBounds = new Rect ();
155-
156138 textDrawableHelper = new TextDrawableHelper ();
157139 textDrawableHelper .getTextPaint ().setTextAlign (Paint .Align .CENTER );
140+
141+ calculateBadgeCenterCoordinates (anchorView , customBadgeParent );
142+ }
143+
144+ /**
145+ * Calculates and updates this badge's center coordinates based on its anchor's bounds. Internally
146+ * also updates this BadgeDrawable's bounds, because they are dependent on the center coordinates.
147+ * For pre API-18, coordinates will be calculated relative to {@code customBadgeParent} because
148+ * the BadgeDrawable will be set as the parent's foreground.
149+ *
150+ * @param anchorView This badge's anchor.
151+ * @param customBadgeParent An optional parent view that will set this BadgeDrawable as its
152+ * foreground.
153+ */
154+ public void updateBadgeCoordinates (
155+ @ NonNull View anchorView , @ Nullable ViewGroup customBadgeParent ) {
156+ calculateBadgeCenterCoordinates (anchorView , customBadgeParent );
157+ updateBounds ();
158+ invalidateSelf ();
158159 }
159160
160161 /**
@@ -390,4 +391,25 @@ private int getMaxBadgeNumber() {
390391 maxBadgeNumberDirty = false ;
391392 return maxBadgeNumber ;
392393 }
394+
395+ private void calculateBadgeCenterCoordinates (
396+ @ NonNull View anchorView , @ Nullable ViewGroup customBadgeParent ) {
397+ Resources res = context .getResources ();
398+ Rect anchorRect = new Rect ();
399+ // Returns the visible bounds of the anchor view.
400+ anchorView .getDrawingRect (anchorRect );
401+ anchorRect .top += res .getDimensionPixelSize (R .dimen .mtrl_badge_vertical_offset );
402+ if (customBadgeParent != null || VERSION .SDK_INT < VERSION_CODES .JELLY_BEAN_MR2 ) {
403+ // Calculates coordinates relative to the parent.
404+ ViewGroup viewGroup =
405+ customBadgeParent == null ? (ViewGroup ) anchorView .getParent () : customBadgeParent ;
406+ viewGroup .offsetDescendantRectToMyCoords (anchorView , anchorRect );
407+ }
408+
409+ badgeCenterX =
410+ ViewCompat .getLayoutDirection (anchorView ) == View .LAYOUT_DIRECTION_LTR
411+ ? anchorRect .right
412+ : anchorRect .left ;
413+ badgeCenterY = anchorRect .top ;
414+ }
393415}
0 commit comments