Skip to content

[Slider] Fix label pivots #4509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions lib/java/com/google/android/material/slider/BaseSlider.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -3180,6 +3189,8 @@ private ValueAnimator createLabelAnimator(boolean enter) {
}

private void updateLabels() {
updateLabelPivots();

switch (labelBehavior) {
case LABEL_GONE:
ensureLabelsRemoved();
Expand All @@ -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);
Expand Down
15 changes: 11 additions & 4 deletions lib/java/com/google/android/material/tooltip/TooltipDrawable.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Expand Down