From bc45b49a0b28cecc066297dea8e64db0c9100663 Mon Sep 17 00:00:00 2001 From: HearSilent Date: Wed, 17 Apr 2019 12:32:24 +0800 Subject: [PATCH] Update version to 1.0.4, Improve value label in other thumb radius --- README.md | 2 +- discreteslider/build.gradle | 4 +- .../discreteslider/DiscreteSlider.java | 71 +++++++++++-------- 3 files changed, 46 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index c4a8fe7..81a7c8f 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ allprojects { **Step 2.** Add the dependency ```gradle dependencies { - implementation 'com.github.hearsilent:DiscreteSlider:1.0.3' + implementation 'com.github.hearsilent:DiscreteSlider:1.0.4' } ``` diff --git a/discreteslider/build.gradle b/discreteslider/build.gradle index 6f29fd4..41ec2ad 100644 --- a/discreteslider/build.gradle +++ b/discreteslider/build.gradle @@ -6,8 +6,8 @@ android { defaultConfig { minSdkVersion 14 targetSdkVersion 28 - versionCode 100020 - versionName "1.0.3" + versionCode 100040 + versionName "1.0.4" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" diff --git a/discreteslider/src/main/java/hearsilent/discreteslider/DiscreteSlider.java b/discreteslider/src/main/java/hearsilent/discreteslider/DiscreteSlider.java index fd01d80..e5de808 100644 --- a/discreteslider/src/main/java/hearsilent/discreteslider/DiscreteSlider.java +++ b/discreteslider/src/main/java/hearsilent/discreteslider/DiscreteSlider.java @@ -7,6 +7,7 @@ import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; +import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.Path; import android.graphics.PointF; @@ -78,6 +79,7 @@ public class DiscreteSlider extends View { private Rect mBounds = new Rect(); private Path mValueLabelPath = new Path(); private ValueAnimator mValueLabelAnimator; + private Matrix mValueLabelMatrix = new Matrix(); private float mValueLabelAnimValue = 0f; @ValueLabelGravity private int mValueLabelGravity; @@ -883,21 +885,23 @@ protected void onDraw(Canvas canvas) { float _cx = cx; float _cy = cy; + float dp6 = Utils.convertDpToPixel(6, getContext()); + float ratio = mRadius / dp6; if (mOrientation == HORIZONTAL) { if (mValueLabelGravity == TOP) { - _cy -= mRadius + Utils.convertDpToPixel(16, getContext()) + mRadius * 3; - _cy = cy + (_cy - cy) * mValueLabelAnimValue; + _cy -= dp6 + Utils.convertDpToPixel(16, getContext()) + dp6 * 3; + _cy = cy + (_cy - cy) * mValueLabelAnimValue * ratio; } else if (mValueLabelGravity == BOTTOM) { - _cy += mRadius + Utils.convertDpToPixel(16, getContext()) + mRadius * 3; - _cy = cy + (_cy - cy) * mValueLabelAnimValue; + _cy += dp6 + Utils.convertDpToPixel(16, getContext()) + dp6 * 3; + _cy = cy + (_cy - cy) * mValueLabelAnimValue * ratio; } } else { if (mValueLabelGravity == RIGHT) { - _cx += mRadius + Utils.convertDpToPixel(16, getContext()) + mRadius * 3; - _cx = cx + (_cx - cx) * mValueLabelAnimValue; + _cx += dp6 + Utils.convertDpToPixel(16, getContext()) + dp6 * 3; + _cx = cx + (_cx - cx) * mValueLabelAnimValue * ratio; } else if (mValueLabelGravity == LEFT) { - _cx -= mRadius + Utils.convertDpToPixel(16, getContext()) + mRadius * 3; - _cx = cx + (_cx - cx) * mValueLabelAnimValue; + _cx -= dp6 + Utils.convertDpToPixel(16, getContext()) + dp6 * 3; + _cx = cx + (_cx - cx) * mValueLabelAnimValue * ratio; } } if (mPaddingPosition == mMinProgress && mPaddingPosition != -1) { @@ -987,8 +991,9 @@ public boolean onMove(MoveGestureDetector detector) { } private void generateValueLabelPath() { - float r2 = mRadius, cx2, cy2; - float r1 = mRadius * 3, cx1, cy1; + float r2 = Utils.convertDpToPixel(6, getContext()), cx2, cy2; + float r1 = r2 * 3, cx1, cy1; + float ratio = mRadius / r2; float length = mLength - mTrackWidth; if (mPaddingPosition == mMinProgress || mMaxProgress != -1 && mMode != MODE_NORMAL) { @@ -1006,35 +1011,31 @@ private void generateValueLabelPath() { cx1 = cx2; cy1 = cy2; + float dp1 = Utils.convertDpToPixel(1, getContext()), dp16 = + Utils.convertDpToPixel(16, getContext()); float ox1, oy1, ox2, oy2; if (mValueLabelGravity == TOP) { - cy1 -= r2 + Utils.convertDpToPixel(16, getContext()) + r1; - cy1 = cy2 + (cy1 - cy2) * mValueLabelAnimValue; - ox1 = -Utils.convertDpToPixel(5, getContext()) * mValueLabelAnimValue; + cy1 -= r2 + dp16 + r1; + ox1 = -Math.max((r2 - dp1), 0); ox2 = -ox1; - oy1 = oy2 = r1 + Utils.convertDpToPixel(8, getContext()) * mValueLabelAnimValue; + oy1 = oy2 = r1 + dp16 / 2; } else if (mValueLabelGravity == BOTTOM) { - cy1 += r2 + Utils.convertDpToPixel(16, getContext()) + r1; - cy1 = cy2 + (cy1 - cy2) * mValueLabelAnimValue; - ox1 = Utils.convertDpToPixel(5, getContext()) * mValueLabelAnimValue; + cy1 += r2 + dp16 + r1; + ox1 = Math.max((r2 - dp1), 0); ox2 = -ox1; - oy1 = oy2 = -r1 - Utils.convertDpToPixel(8, getContext()) * mValueLabelAnimValue; + oy1 = oy2 = -r1 - dp16 / 2; } else if (mValueLabelGravity == RIGHT) { - cx1 += r2 + Utils.convertDpToPixel(16, getContext()) + r1; - cx1 = cx2 + (cx1 - cx2) * mValueLabelAnimValue; - ox1 = ox2 = -r1 - Utils.convertDpToPixel(8, getContext()) * mValueLabelAnimValue; - oy1 = -Utils.convertDpToPixel(5, getContext()) * mValueLabelAnimValue; + cx1 += r2 + dp16 + r1; + ox1 = ox2 = -r1 - dp16 / 2; + oy1 = -Math.max((r2 - dp1), 0); oy2 = -oy1; } else { - cx1 -= r2 + Utils.convertDpToPixel(16, getContext()) + r1; - cx1 = cx2 + (cx1 - cx2) * mValueLabelAnimValue; - ox1 = ox2 = r1 + Utils.convertDpToPixel(8, getContext()) * mValueLabelAnimValue; - oy1 = Utils.convertDpToPixel(5, getContext()) * mValueLabelAnimValue; + cx1 -= r2 + dp16 + r1; + ox1 = ox2 = r1 + dp16 / 2; + oy1 = Math.max((r2 - dp1), 0); oy2 = -oy1; } - r1 *= mValueLabelAnimValue; - if (mValueLabelGravity == TOP && cy1 + r1 >= cy2 - r2) { mValueLabelPath.reset(); return; @@ -1064,6 +1065,20 @@ private void generateValueLabelPath() { .moveTo(cx1 + r1 * (float) Math.cos(Math.toRadians(135 + mValueLabelGravity)), cy1 + r1 * (float) Math.sin(Math.toRadians(135 + mValueLabelGravity))); mValueLabelPath.close(); + + if (mValueLabelAnimValue * ratio != 1) { + mValueLabelPath.computeBounds(mRectF, true); + if (mOrientation == HORIZONTAL) { + mValueLabelMatrix + .setScale(mValueLabelAnimValue * ratio, mValueLabelAnimValue * ratio, + mRectF.centerX(), cy2); + } else { + mValueLabelMatrix + .setScale(mValueLabelAnimValue * ratio, mValueLabelAnimValue * ratio, cx2, + mRectF.centerY()); + } + mValueLabelPath.transform(mValueLabelMatrix); + } } private float[] getClosestPosition(float p, float length) {