diff --git a/lib/java/com/google/android/material/slider/BaseSlider.java b/lib/java/com/google/android/material/slider/BaseSlider.java index 6212302bca6..fa81eb41e76 100644 --- a/lib/java/com/google/android/material/slider/BaseSlider.java +++ b/lib/java/com/google/android/material/slider/BaseSlider.java @@ -279,6 +279,15 @@ abstract class BaseSlider< private static final int LABEL_ANIMATION_EXIT_EASING_ATTR = R.attr.motionEasingEmphasizedAccelerateInterpolator; + private static final float TOP_LABEL_PIVOT_X = 0.5f; + private static final float TOP_LABEL_PIVOT_Y = 1.2f; + + private static final float LEFT_LABEL_PIVOT_X = 1.2f; + private static final float LEFT_LABEL_PIVOT_Y = 0.5f; + + private static final float RIGHT_LABEL_PIVOT_X = -0.2f; + private static final float RIGHT_LABEL_PIVOT_Y = 0.5f; + @Dimension(unit = Dimension.DP) private static final int MIN_TOUCH_TARGET_DP = 48; @@ -3180,6 +3189,8 @@ private ValueAnimator createLabelAnimator(boolean enter) { } private void updateLabels() { + updateLabelPivots(); + switch (labelBehavior) { case LABEL_GONE: ensureLabelsRemoved(); @@ -3204,6 +3215,29 @@ private void updateLabels() { } } + private void updateLabelPivots() { + // Set the pivot point so that the label pops up in the direction from the thumb. + final float labelPivotX; + final float labelPivotY; + + final boolean isVertical = isVertical(); + final boolean isRtl = isRtl(); + if (isVertical && isRtl) { + labelPivotX = RIGHT_LABEL_PIVOT_X; + labelPivotY = RIGHT_LABEL_PIVOT_Y; + } else if (isVertical) { + labelPivotX = LEFT_LABEL_PIVOT_X; + labelPivotY = LEFT_LABEL_PIVOT_Y; + } else { + labelPivotX = TOP_LABEL_PIVOT_X; + labelPivotY = TOP_LABEL_PIVOT_Y; + } + + for (TooltipDrawable label : labels) { + label.setPivots(labelPivotX, labelPivotY); + } + } + private boolean isSliderVisibleOnScreen() { final Rect contentViewBounds = new Rect(); ViewUtils.getContentView(this).getHitRect(contentViewBounds); diff --git a/lib/java/com/google/android/material/tooltip/TooltipDrawable.java b/lib/java/com/google/android/material/tooltip/TooltipDrawable.java index 52afb1a65a1..cd32cc88c76 100644 --- a/lib/java/com/google/android/material/tooltip/TooltipDrawable.java +++ b/lib/java/com/google/android/material/tooltip/TooltipDrawable.java @@ -107,7 +107,7 @@ public void onLayoutChange( private float tooltipScaleX = 1F; private float tooltipScaleY = 1F; - private final float tooltipPivotX = 0.5F; + private float tooltipPivotX = 0.5F; private float tooltipPivotY = 0.5F; private float labelOpacity = 1.0F; @@ -366,15 +366,22 @@ public void setLayoutMargin(@Px int layoutMargin) { * @param fraction A value between 0.0 and 1.0 that defines how "shown" the tooltip will be. */ public void setRevealFraction(@FloatRange(from = 0.0, to = 1.0) float fraction) { - // Set the y pivot point below the bottom of the tooltip to make it look like the - // tooltip is translating slightly up while scaling in. - tooltipPivotY = 1.2F; tooltipScaleX = fraction; tooltipScaleY = fraction; labelOpacity = AnimationUtils.lerp(0F, 1F, 0.19F, 1F, fraction); invalidateSelf(); } + /** + * @hide + */ + @RestrictTo(LIBRARY_GROUP) + public void setPivots(float pivotX, float pivotY) { + this.tooltipPivotX = pivotX; + this.tooltipPivotY = pivotY; + invalidateSelf(); + } + /** * Should be called to allow this drawable to calculate its position within the current display * frame. This allows it to apply to specified window padding.