From 653d56fad12febcc35791710a1ac5c3889208df4 Mon Sep 17 00:00:00 2001 From: Etienne Wojahn Date: Mon, 11 Dec 2017 14:31:29 +0100 Subject: [PATCH] Added CustomBubbleValueToTextConverter This adds a custom bubble text converter to create the possibility to map a specific progress value to a different value that is displayed to the user. Also you can now add something like "2h" as your bubble text. To archive this I changed the following: added DefaultConverter to config changed every bubble text to be set through converter --- .../java/com/xw/repo/BubbleConfigBuilder.java | 11 +++++++ .../main/java/com/xw/repo/BubbleSeekBar.java | 29 ++++++++++++------- .../DefaultBubbleValueToTextConverter.java | 13 +++++++++ 3 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 bubbleseekbar/src/main/java/com/xw/repo/DefaultBubbleValueToTextConverter.java diff --git a/bubbleseekbar/src/main/java/com/xw/repo/BubbleConfigBuilder.java b/bubbleseekbar/src/main/java/com/xw/repo/BubbleConfigBuilder.java index f431109..9b12faf 100644 --- a/bubbleseekbar/src/main/java/com/xw/repo/BubbleConfigBuilder.java +++ b/bubbleseekbar/src/main/java/com/xw/repo/BubbleConfigBuilder.java @@ -48,6 +48,8 @@ public class BubbleConfigBuilder { boolean hideBubble; boolean rtl; + BubbleSeekBar.BubbleValueToTextConverter bubbleValueToTextConverter; + private BubbleSeekBar mBubbleSeekBar; BubbleConfigBuilder(BubbleSeekBar bubbleSeekBar) { @@ -228,6 +230,11 @@ public BubbleConfigBuilder rtl() { return this; } + public BubbleConfigBuilder bubbleValueToTextConverter (BubbleSeekBar.BubbleValueToTextConverter converter) { + this.bubbleValueToTextConverter = converter; + return this; + } + public float getMin() { return min; } @@ -359,4 +366,8 @@ public boolean isHideBubble() { public boolean isRtl() { return rtl; } + + public BubbleSeekBar.BubbleValueToTextConverter getBubbleValueToTextConverter() { + return bubbleValueToTextConverter; + } } diff --git a/bubbleseekbar/src/main/java/com/xw/repo/BubbleSeekBar.java b/bubbleseekbar/src/main/java/com/xw/repo/BubbleSeekBar.java index 3b99bd1..9b70178 100644 --- a/bubbleseekbar/src/main/java/com/xw/repo/BubbleSeekBar.java +++ b/bubbleseekbar/src/main/java/com/xw/repo/BubbleSeekBar.java @@ -125,6 +125,8 @@ public class BubbleSeekBar extends View { private float mPreSecValue; // previous SectionValue private BubbleConfigBuilder mConfigBuilder; // config attributes + private BubbleValueToTextConverter mBubbleValueToTextConverter; + public BubbleSeekBar(Context context) { this(context, null); } @@ -205,8 +207,8 @@ public BubbleSeekBar(Context context, AttributeSet attrs, int defStyleAttr) { // init BubbleView mBubbleView = new BubbleView(context); - mBubbleView.setProgressText(isShowProgressInFloat ? - String.valueOf(getProgressFloat()) : String.valueOf(getProgress())); + mBubbleValueToTextConverter = new DefaultBubbleValueToTextConverter(); + mBubbleView.setProgressText(mBubbleValueToTextConverter.convertValueToText(this, getProgress(), getProgressFloat(), isShowProgressInFloat)); mLayoutParams = new WindowManager.LayoutParams(); mLayoutParams.gravity = Gravity.START | Gravity.TOP; @@ -721,8 +723,8 @@ public boolean onTouchEvent(MotionEvent event) { mBubbleCenterRawX = calculateCenterRawXofBubbleView(); mLayoutParams.x = (int) (mBubbleCenterRawX + 0.5f); mWindowManager.updateViewLayout(mBubbleView, mLayoutParams); - mBubbleView.setProgressText(isShowProgressInFloat ? - String.valueOf(getProgressFloat()) : String.valueOf(getProgress())); + mBubbleView.setProgressText(mBubbleValueToTextConverter.convertValueToText(this, getProgress(), getProgressFloat(), isShowProgressInFloat)); + } invalidate(); @@ -856,8 +858,8 @@ public void onAnimationStart(Animator animation) { mWindowManager.addView(mBubbleView, mLayoutParams); } }).start(); - mBubbleView.setProgressText(isShowProgressInFloat ? - String.valueOf(getProgressFloat()) : String.valueOf(getProgress())); + mBubbleView.setProgressText(mBubbleValueToTextConverter.convertValueToText(this, getProgress(), getProgressFloat(), isShowProgressInFloat)); + } /** @@ -897,8 +899,7 @@ public void onAnimationUpdate(ValueAnimator animation) { mBubbleCenterRawX = calculateCenterRawXofBubbleView(); mLayoutParams.x = (int) (mBubbleCenterRawX + 0.5f); mWindowManager.updateViewLayout(mBubbleView, mLayoutParams); - mBubbleView.setProgressText(isShowProgressInFloat ? - String.valueOf(getProgressFloat()) : String.valueOf(getProgress())); + mBubbleView.setProgressText(mBubbleValueToTextConverter.convertValueToText(BubbleSeekBar.this, getProgress(), getProgressFloat(), isShowProgressInFloat)); } invalidate(); @@ -1158,6 +1159,7 @@ void config(BubbleConfigBuilder builder) { mAlwaysShowBubbleDelay = builder.alwaysShowBubbleDelay; isHideBubble = builder.hideBubble; isRtl = builder.rtl; + mBubbleValueToTextConverter = builder.bubbleValueToTextConverter; initConfigByPriority(); calculateRadiusOfBubble(); @@ -1210,6 +1212,7 @@ public BubbleConfigBuilder getConfigBuilder() { mConfigBuilder.alwaysShowBubbleDelay = mAlwaysShowBubbleDelay; mConfigBuilder.hideBubble = isHideBubble; mConfigBuilder.rtl = isRtl; + mConfigBuilder.bubbleValueToTextConverter = mBubbleValueToTextConverter; return mConfigBuilder; } @@ -1231,8 +1234,7 @@ protected void onRestoreInstanceState(Parcelable state) { super.onRestoreInstanceState(bundle.getParcelable("save_instance")); if (mBubbleView != null) { - mBubbleView.setProgressText(isShowProgressInFloat ? - String.valueOf(getProgressFloat()) : String.valueOf(getProgress())); + mBubbleView.setProgressText(mBubbleValueToTextConverter.convertValueToText(BubbleSeekBar.this, getProgress(), getProgressFloat(), isShowProgressInFloat)); } setProgress(mProgress); @@ -1305,6 +1307,13 @@ public interface CustomSectionTextArray { SparseArray onCustomize(int sectionCount, @NonNull SparseArray array); } + /** + * Customize the value text that is displayed inside the bubble. Works fine with maximal five characters + */ + public interface BubbleValueToTextConverter { + String convertValueToText(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat, Boolean isShowProgressInFloat); + } + /*********************************************************************************************** ************************************** custom bubble view *********************************** **********************************************************************************************/ diff --git a/bubbleseekbar/src/main/java/com/xw/repo/DefaultBubbleValueToTextConverter.java b/bubbleseekbar/src/main/java/com/xw/repo/DefaultBubbleValueToTextConverter.java new file mode 100644 index 0000000..9c80f81 --- /dev/null +++ b/bubbleseekbar/src/main/java/com/xw/repo/DefaultBubbleValueToTextConverter.java @@ -0,0 +1,13 @@ +package com.xw.repo; + +/** + * Created by ewojahn on 07.12.17. + */ + +public class DefaultBubbleValueToTextConverter implements BubbleSeekBar.BubbleValueToTextConverter { + @Override + public String convertValueToText(BubbleSeekBar bubbleSeekBar, int progress, float progressFloat, Boolean isShowProgressInFloat) { + return isShowProgressInFloat ? + String.valueOf(progressFloat) : String.valueOf(progress); + } +}