Skip to content

Commit d28488d

Browse files
pubiqqleticiarossi
authored andcommitted
[Slider] Fix label pivots
Resolves #4509 GIT_ORIGIN_REV_ID=a26da16d8389df1de090c737c50c6c1e12f77df8 PiperOrigin-RevId: 713697809
1 parent 7f0a51d commit d28488d

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

lib/java/com/google/android/material/slider/BaseSlider.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,15 @@ abstract class BaseSlider<
279279
private static final int LABEL_ANIMATION_EXIT_EASING_ATTR =
280280
R.attr.motionEasingEmphasizedAccelerateInterpolator;
281281

282+
private static final float TOP_LABEL_PIVOT_X = 0.5f;
283+
private static final float TOP_LABEL_PIVOT_Y = 1.2f;
284+
285+
private static final float LEFT_LABEL_PIVOT_X = 1.2f;
286+
private static final float LEFT_LABEL_PIVOT_Y = 0.5f;
287+
288+
private static final float RIGHT_LABEL_PIVOT_X = -0.2f;
289+
private static final float RIGHT_LABEL_PIVOT_Y = 0.5f;
290+
282291
@Dimension(unit = Dimension.DP)
283292
private static final int MIN_TOUCH_TARGET_DP = 48;
284293

@@ -3276,6 +3285,8 @@ private ValueAnimator createLabelAnimator(boolean enter) {
32763285
}
32773286

32783287
private void updateLabels() {
3288+
updateLabelPivots();
3289+
32793290
switch (labelBehavior) {
32803291
case LABEL_GONE:
32813292
ensureLabelsRemoved();
@@ -3300,6 +3311,29 @@ private void updateLabels() {
33003311
}
33013312
}
33023313

3314+
private void updateLabelPivots() {
3315+
// Set the pivot point so that the label pops up in the direction from the thumb.
3316+
final float labelPivotX;
3317+
final float labelPivotY;
3318+
3319+
final boolean isVertical = isVertical();
3320+
final boolean isRtl = isRtl();
3321+
if (isVertical && isRtl) {
3322+
labelPivotX = RIGHT_LABEL_PIVOT_X;
3323+
labelPivotY = RIGHT_LABEL_PIVOT_Y;
3324+
} else if (isVertical) {
3325+
labelPivotX = LEFT_LABEL_PIVOT_X;
3326+
labelPivotY = LEFT_LABEL_PIVOT_Y;
3327+
} else {
3328+
labelPivotX = TOP_LABEL_PIVOT_X;
3329+
labelPivotY = TOP_LABEL_PIVOT_Y;
3330+
}
3331+
3332+
for (TooltipDrawable label : labels) {
3333+
label.setPivots(labelPivotX, labelPivotY);
3334+
}
3335+
}
3336+
33033337
private boolean isSliderVisibleOnScreen() {
33043338
final Rect contentViewBounds = new Rect();
33053339
ViewUtils.getContentView(this).getHitRect(contentViewBounds);

lib/java/com/google/android/material/tooltip/TooltipDrawable.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public void onLayoutChange(
107107

108108
private float tooltipScaleX = 1F;
109109
private float tooltipScaleY = 1F;
110-
private final float tooltipPivotX = 0.5F;
110+
private float tooltipPivotX = 0.5F;
111111
private float tooltipPivotY = 0.5F;
112112
private float labelOpacity = 1.0F;
113113

@@ -366,15 +366,24 @@ public void setLayoutMargin(@Px int layoutMargin) {
366366
* @param fraction A value between 0.0 and 1.0 that defines how "shown" the tooltip will be.
367367
*/
368368
public void setRevealFraction(@FloatRange(from = 0.0, to = 1.0) float fraction) {
369-
// Set the y pivot point below the bottom of the tooltip to make it look like the
370-
// tooltip is translating slightly up while scaling in.
371-
tooltipPivotY = 1.2F;
372369
tooltipScaleX = fraction;
373370
tooltipScaleY = fraction;
374371
labelOpacity = AnimationUtils.lerp(0F, 1F, 0.19F, 1F, fraction);
375372
invalidateSelf();
376373
}
377374

375+
/**
376+
* Set the pivot points for the tooltip.
377+
*
378+
* @hide
379+
*/
380+
@RestrictTo(LIBRARY_GROUP)
381+
public void setPivots(float pivotX, float pivotY) {
382+
this.tooltipPivotX = pivotX;
383+
this.tooltipPivotY = pivotY;
384+
invalidateSelf();
385+
}
386+
378387
/**
379388
* Should be called to allow this drawable to calculate its position within the current display
380389
* frame. This allows it to apply to specified window padding.
@@ -486,7 +495,7 @@ private float calculatePointerOffset() {
486495
private EdgeTreatment createMarkerEdge() {
487496
float offset = -calculatePointerOffset();
488497
// The maximum distance the arrow can be offset before extends outside the bounds.
489-
float maxArrowOffset = (float) (getBounds().width() - arrowSize * Math.sqrt(2)) / 2.0f;
498+
float maxArrowOffset = (float) ((getBounds().width() - arrowSize * Math.sqrt(2)) / 2.0f);
490499
offset = Math.max(offset, -maxArrowOffset);
491500
offset = Math.min(offset, maxArrowOffset);
492501
return new OffsetEdgeTreatment(new MarkerEdgeTreatment(arrowSize), offset);

0 commit comments

Comments
 (0)