From 24a89b4816ac4d4738f292ebb5d961118558306b Mon Sep 17 00:00:00 2001 From: Sergey Date: Fri, 21 Jun 2019 13:21:34 +0400 Subject: [PATCH] Stable seek bar positioning Touching the thumb on the Seek bar prevents the thumb from immediately jumping to a new position and changing the progress of the Seek bar. Test: Place your finger on the seek bar thumb so that your finger touches the thumb. Take your finger away. Seek bar should not change progress and thumb's position. Place your finger on the seek bar thumb so that your finger touches the thumb. Seek bar should not change progress and thumb's position. Then, without lifting your finger from the screen, move your finger towards the maximum or minimum value. Seek bar thumb should follow your finger while maintaining its position relative to your finger. Change-Id: Ia72d247662ad5b8dd66d0b0578098b9a2b6060cd --- core/java/android/widget/AbsSeekBar.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/core/java/android/widget/AbsSeekBar.java b/core/java/android/widget/AbsSeekBar.java index a85c5854d05ab..b4a3661ca32a4 100644 --- a/core/java/android/widget/AbsSeekBar.java +++ b/core/java/android/widget/AbsSeekBar.java @@ -88,6 +88,7 @@ public abstract class AbsSeekBar extends ProgressBar { private float mTouchDownX; @UnsupportedAppUsage private boolean mIsDragging; + private float mTouchThumbOffset = 0.0f; public AbsSeekBar(Context context) { super(context); @@ -775,6 +776,14 @@ public abstract class AbsSeekBar extends ProgressBar { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: + if (mThumb != null) { + final int availableWidth = getWidth() - mPaddingLeft - mPaddingRight; + mTouchThumbOffset = (getProgress() - getMin()) / (float) (getMax() + - getMin()) - (event.getX() - mPaddingLeft) / availableWidth; + if (Math.abs(mTouchThumbOffset * availableWidth) > getThumbOffset()) { + mTouchThumbOffset = 0; + } + } if (isInScrollingContainer()) { mTouchDownX = event.getX(); } else { @@ -857,7 +866,8 @@ public abstract class AbsSeekBar extends ProgressBar { } else if (x < mPaddingLeft) { scale = 1.0f; } else { - scale = (availableWidth - x + mPaddingLeft) / (float) availableWidth; + scale = (availableWidth - x + mPaddingLeft) / (float) availableWidth + + mTouchThumbOffset; progress = mTouchProgressOffset; } } else { @@ -866,7 +876,7 @@ public abstract class AbsSeekBar extends ProgressBar { } else if (x > width - mPaddingRight) { scale = 1.0f; } else { - scale = (x - mPaddingLeft) / (float) availableWidth; + scale = (x - mPaddingLeft) / (float) availableWidth + mTouchThumbOffset; progress = mTouchProgressOffset; } }