@@ -2589,9 +2589,7 @@ protected void onDraw(@NonNull Canvas canvas) {
2589
2589
int yCenter = calculateTrackCenter ();
2590
2590
2591
2591
drawInactiveTracks (canvas , trackWidth , yCenter );
2592
- if (!isCentered ()) {
2593
- drawActiveTracks (canvas , trackWidth , yCenter );
2594
- }
2592
+ drawActiveTracks (canvas , trackWidth , yCenter );
2595
2593
2596
2594
if (isRtl () || isVertical ()) {
2597
2595
drawTrackIcons (canvas , activeTrackRect , inactiveTrackLeftRect );
@@ -2621,14 +2619,16 @@ private float[] getActiveRange() {
2621
2619
float left = normalizeValue (values .size () == 1 ? valueFrom : min );
2622
2620
float right = normalizeValue (max );
2623
2621
2624
- // When centered, there is no active range, left == right in order to draw the inactive track on
2625
- // both sides of the thumb leaving space for it, covering the entirety of the track.
2622
+ // When centered, the active range is bound by the center.
2626
2623
if (isCentered ()) {
2627
- left = right ;
2624
+ left = min (.5f , right );
2625
+ right = max (.5f , right );
2628
2626
}
2629
2627
2630
- // In RTL we draw things in reverse, so swap the left and right range values
2631
- return isRtl () || isVertical () ? new float [] {right , left } : new float [] {left , right };
2628
+ // In RTL we draw things in reverse, so swap the left and right range values.
2629
+ return !isCentered () && (isRtl () || isVertical ())
2630
+ ? new float [] {right , left }
2631
+ : new float [] {left , right };
2632
2632
}
2633
2633
2634
2634
private void drawInactiveTracks (@ NonNull Canvas canvas , int width , int yCenter ) {
@@ -2692,7 +2692,7 @@ private void drawActiveTracks(@NonNull Canvas canvas, int width, int yCenter) {
2692
2692
}
2693
2693
2694
2694
FullCornerDirection direction = FullCornerDirection .NONE ;
2695
- if (values .size () == 1 ) { // Only 1 thumb
2695
+ if (values .size () == 1 && ! isCentered () ) { // Only 1 thumb
2696
2696
direction = isRtl () || isVertical () ? FullCornerDirection .RIGHT : FullCornerDirection .LEFT ;
2697
2697
}
2698
2698
@@ -2712,8 +2712,14 @@ private void drawActiveTracks(@NonNull Canvas canvas, int width, int yCenter) {
2712
2712
int trackCornerSize = getTrackCornerSize ();
2713
2713
switch (direction ) {
2714
2714
case NONE :
2715
- left += thumbTrackGapSize ;
2716
- right -= thumbTrackGapSize ;
2715
+ if (!isCentered ()) {
2716
+ left += thumbTrackGapSize ;
2717
+ right -= thumbTrackGapSize ;
2718
+ } else if (activeRange [1 ] == .5f ) { // centered, active track ends at the center
2719
+ left += thumbTrackGapSize ;
2720
+ } else if (activeRange [0 ] == .5f ) { // centered, active track starts at the center
2721
+ right -= thumbTrackGapSize ;
2722
+ }
2717
2723
break ;
2718
2724
case LEFT :
2719
2725
left -= trackCornerSize ;
@@ -2955,11 +2961,7 @@ private void maybeDrawTicks(@NonNull Canvas canvas) {
2955
2961
2956
2962
// Draw ticks on the active track (if any).
2957
2963
if (leftActiveTickIndex <= rightActiveTickIndex ) {
2958
- drawTicks (
2959
- leftActiveTickIndex * 2 ,
2960
- (rightActiveTickIndex + 1 ) * 2 ,
2961
- canvas ,
2962
- isCentered () ? inactiveTicksPaint : activeTicksPaint ); // centered uses inactive color.
2964
+ drawTicks (leftActiveTickIndex * 2 , (rightActiveTickIndex + 1 ) * 2 , canvas , activeTicksPaint );
2963
2965
}
2964
2966
2965
2967
// Draw ticks on the right inactive track (if any).
@@ -2971,7 +2973,9 @@ private void maybeDrawTicks(@NonNull Canvas canvas) {
2971
2973
2972
2974
private void drawTicks (int from , int to , Canvas canvas , Paint paint ) {
2973
2975
for (int i = from ; i < to ; i += 2 ) {
2974
- if (isOverlappingThumb (ticksCoordinates [i ])) {
2976
+ float coordinateToCheck = isVertical () ? ticksCoordinates [i + 1 ] : ticksCoordinates [i ];
2977
+ if (isOverlappingThumb (coordinateToCheck )
2978
+ || (isCentered () && isOverlappingCenterGap (coordinateToCheck ))) {
2975
2979
continue ;
2976
2980
}
2977
2981
canvas .drawPoint (ticksCoordinates [i ], ticksCoordinates [i + 1 ], paint );
@@ -2987,6 +2991,12 @@ private boolean isOverlappingThumb(float tickCoordinate) {
2987
2991
return false ;
2988
2992
}
2989
2993
2994
+ private boolean isOverlappingCenterGap (float tickCoordinate ) {
2995
+ float threshold = thumbTrackGapSize + thumbWidth / 2f ;
2996
+ float trackCenter = (trackWidth + trackSidePadding * 2 ) / 2f ;
2997
+ return tickCoordinate >= trackCenter - threshold && tickCoordinate <= trackCenter + threshold ;
2998
+ }
2999
+
2990
3000
private void maybeDrawStopIndicator (@ NonNull Canvas canvas , int yCenter ) {
2991
3001
if (trackStopIndicatorSize <= 0 || values .isEmpty ()) {
2992
3002
return ;
@@ -3000,10 +3010,6 @@ private void maybeDrawStopIndicator(@NonNull Canvas canvas, int yCenter) {
3000
3010
if (isCentered () || (values .size () > 1 && values .get (0 ) > valueFrom )) {
3001
3011
drawStopIndicator (canvas , valueToX (valueFrom ), yCenter );
3002
3012
}
3003
- // Centered, draw indicator in the middle of the track.
3004
- if (isCentered ()) {
3005
- drawStopIndicator (canvas , (valueToX (valueTo ) + valueToX (valueFrom )) / 2f , yCenter );
3006
- }
3007
3013
}
3008
3014
3009
3015
private void drawStopIndicator (@ NonNull Canvas canvas , float x , float y ) {
0 commit comments