|
47 | 47 | import androidx.annotation.StyleRes;
|
48 | 48 | import androidx.annotation.XmlRes;
|
49 | 49 | import androidx.core.view.ViewCompat;
|
| 50 | +import com.google.android.material.animation.AnimationUtils; |
50 | 51 | import com.google.android.material.internal.TextDrawableHelper;
|
51 | 52 | import com.google.android.material.internal.TextDrawableHelper.TextDrawableDelegate;
|
52 | 53 | import com.google.android.material.internal.ThemeEnforcement;
|
| 54 | +import com.google.android.material.resources.MaterialResources; |
53 | 55 | import com.google.android.material.resources.TextAppearance;
|
54 | 56 | import com.google.android.material.shape.MaterialShapeDrawable;
|
55 | 57 | import com.google.android.material.shape.ShapeAppearanceModel;
|
@@ -187,6 +189,9 @@ public class BadgeDrawable extends Drawable implements TextDrawableDelegate {
|
187 | 189 | /** A value to indicate that badge content should not be truncated. */
|
188 | 190 | public static final int BADGE_CONTENT_NOT_TRUNCATED = -2;
|
189 | 191 |
|
| 192 | + /** The font scale threshold to changing the vertical offset of the badge. **/ |
| 193 | + private static final float FONT_SCALE_THRESHOLD = .3F; |
| 194 | + |
190 | 195 | @NonNull private final WeakReference<Context> contextRef;
|
191 | 196 | @NonNull private final MaterialShapeDrawable shapeDrawable;
|
192 | 197 | @NonNull private final TextDrawableHelper textDrawableHelper;
|
@@ -1009,6 +1014,29 @@ public int getVerticalOffsetWithText() {
|
1009 | 1014 | return state.getVerticalOffsetWithText();
|
1010 | 1015 | }
|
1011 | 1016 |
|
| 1017 | + /** |
| 1018 | + * Sets how much (in pixels) to vertically move this badge away the center of its anchor when this |
| 1019 | + * badge has text and the font scale is at max size. This is in conjunction with the vertical |
| 1020 | + * offset with text. |
| 1021 | + * |
| 1022 | + * @param px how much to move the badge's vertical offset away from the center by when the font is |
| 1023 | + * large. |
| 1024 | + */ |
| 1025 | + public void setLargeFontVerticalOffsetAdjustment(@Px int px) { |
| 1026 | + state.setLargeFontVerticalOffsetAdjustment(px); |
| 1027 | + updateCenterAndBounds(); |
| 1028 | + } |
| 1029 | + |
| 1030 | + /** |
| 1031 | + * Returns how much (in pixels) this badge is being vertically moved away the center of its |
| 1032 | + * anchor when the badge has text and the font scale is at max. Note that this is not the total |
| 1033 | + * vertical offset. |
| 1034 | + */ |
| 1035 | + @Px |
| 1036 | + public int getLargeFontVerticalOffsetAdjustment() { |
| 1037 | + return state.getLargeFontVerticalOffsetAdjustment(); |
| 1038 | + } |
| 1039 | + |
1012 | 1040 | /**
|
1013 | 1041 | * Sets how much (in pixels) more (in addition to {@code savedState.verticalOffset}) to vertically
|
1014 | 1042 | * move this badge towards the center of its anchor. Currently used to adjust the placement of
|
@@ -1165,10 +1193,22 @@ private void updateCenterAndBounds() {
|
1165 | 1193 | }
|
1166 | 1194 |
|
1167 | 1195 | private int getTotalVerticalOffsetForState() {
|
1168 |
| - int vOffset = |
1169 |
| - hasBadgeContent() |
1170 |
| - ? state.getVerticalOffsetWithText() |
1171 |
| - : state.getVerticalOffsetWithoutText(); |
| 1196 | + int vOffset = state.getVerticalOffsetWithoutText(); |
| 1197 | + if (hasBadgeContent()) { |
| 1198 | + vOffset = state.getVerticalOffsetWithText(); |
| 1199 | + Context context = contextRef.get(); |
| 1200 | + if (context != null) { |
| 1201 | + float progress = |
| 1202 | + AnimationUtils.lerp(0F, 1F, |
| 1203 | + FONT_SCALE_THRESHOLD, 1F, MaterialResources.getFontScale(context) - 1F); |
| 1204 | + vOffset = |
| 1205 | + AnimationUtils.lerp( |
| 1206 | + vOffset, vOffset - state.getLargeFontVerticalOffsetAdjustment(), progress); |
| 1207 | + } |
| 1208 | + } |
| 1209 | + |
| 1210 | + |
| 1211 | + |
1172 | 1212 | // If the offset alignment mode is at the edge of the anchor, we want to move the badge
|
1173 | 1213 | // so that its origin is at the edge.
|
1174 | 1214 | if (state.offsetAlignmentMode == OFFSET_ALIGNMENT_MODE_EDGE) {
|
|
0 commit comments