From fdddeb373d3a730faaf49b117192478b1bb6bfe7 Mon Sep 17 00:00:00 2001 From: Patrik Figura <> Date: Sun, 17 Mar 2019 21:25:13 +0100 Subject: [PATCH] added support for showing/ hiding pins (dynamically) added attribute for pin updated sample application --- .../appyvet/rangebarsample/MainActivity.java | 11 ++++- .../src/main/res/layout/activity_main.xml | 23 ++++++++++ .../src/main/res/values/strings.xml | 1 + .../com/appyvet/materialrangebar/PinView.java | 44 +++++++++++-------- .../appyvet/materialrangebar/RangeBar.java | 25 +++++++++-- .../src/main/res/values/attrs.xml | 1 + 6 files changed, 80 insertions(+), 25 deletions(-) diff --git a/RangeBarSample/src/main/java/com/appyvet/rangebarsample/MainActivity.java b/RangeBarSample/src/main/java/com/appyvet/rangebarsample/MainActivity.java index eb7286fc..fc61b23a 100644 --- a/RangeBarSample/src/main/java/com/appyvet/rangebarsample/MainActivity.java +++ b/RangeBarSample/src/main/java/com/appyvet/rangebarsample/MainActivity.java @@ -84,6 +84,7 @@ protected void onCreate(Bundle savedInstanceState) { final TextView tickTopLabelsButton = (TextView) findViewById(R.id.toggleTickTopLabels); final TextView tickLabelColor = (TextView) findViewById(R.id.tickLabelColor); final TextView tickLabelSelectedColor = (TextView) findViewById(R.id.tickLabelSelectColor); + final TextView togglePinsEnabled = (TextView) findViewById(R.id.togglePinsEnabled); //Sets the buttons to bold. // barColor.setTypeface(font, Typeface.BOLD); @@ -416,9 +417,15 @@ public void onClick(View v) { } }); + togglePinsEnabled.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + rangebar.setPinsEnabled(!rangebar.arePinsEnabled()); + } + }); } - private int getValueInDP(int value) - { + + private int getValueInDP(int value) { int valueInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, getResources().getDisplayMetrics()); diff --git a/RangeBarSample/src/main/res/layout/activity_main.xml b/RangeBarSample/src/main/res/layout/activity_main.xml index 12d0c8ca..4449f0e9 100644 --- a/RangeBarSample/src/main/res/layout/activity_main.xml +++ b/RangeBarSample/src/main/res/layout/activity_main.xml @@ -277,6 +277,29 @@ + + + + + Rounded background Top tick labels Bottom tick labels + Pins enabled #33b5e5 #aa66cc diff --git a/materialrangebar/src/main/java/com/appyvet/materialrangebar/PinView.java b/materialrangebar/src/main/java/com/appyvet/materialrangebar/PinView.java index 960e3c58..7f95b3a6 100644 --- a/materialrangebar/src/main/java/com/appyvet/materialrangebar/PinView.java +++ b/materialrangebar/src/main/java/com/appyvet/materialrangebar/PinView.java @@ -95,6 +95,8 @@ class PinView extends View { private int pinColor; + private boolean mPinEnabled = false; + // Constructors //////////////////////////////////////////////////////////// public PinView(Context context) { @@ -125,7 +127,8 @@ public void setFormatter(com.appyvet.materialrangebar.IRangeBarFormatter mFormat * @param pinsAreTemporary whether to show the pin initially or just the circle */ public void init(Context ctx, float y, float pinRadiusDP, int pinColor, int textColor, - float circleRadius, int circleColor, int circleBoundaryColor, float circleBoundarySize, float minFont, float maxFont, boolean pinsAreTemporary) { + float circleRadius, int circleColor, int circleBoundaryColor, float circleBoundarySize, + float minFont, float maxFont, boolean pinsAreTemporary, boolean pinEnabled) { mRes = ctx.getResources(); mPin = ContextCompat.getDrawable(ctx, R.drawable.rotate); @@ -172,6 +175,7 @@ public void init(Context ctx, float y, float pinRadiusDP, int pinColor, int text } this.pinColor = pinColor; + mPinEnabled = pinEnabled; // Sets the minimum touchable area, but allows it to expand based on // image size @@ -274,25 +278,27 @@ public void draw(Canvas canvas) { canvas.drawCircle(mX, mY, mCircleRadiusPx, mCirclePaint); //Draw pin if pressed - if (mPinRadiusPx > 0 && (mHasBeenPressed || !mPinsAreTemporary)) { - mBounds.set((int) mX - mPinRadiusPx, - (int) mY - (mPinRadiusPx * 2) - (int) mPinPadding, - (int) mX + mPinRadiusPx, (int) mY - (int) mPinPadding); - mPin.setBounds(mBounds); - String text = mValue; - - if (this.formatter != null) { - text = formatter.format(text); + if (mPinEnabled) { + if (mPinRadiusPx > 0 && (mHasBeenPressed || !mPinsAreTemporary)) { + mBounds.set((int) mX - mPinRadiusPx, + (int) mY - (mPinRadiusPx * 2) - (int) mPinPadding, + (int) mX + mPinRadiusPx, (int) mY - (int) mPinPadding); + mPin.setBounds(mBounds); + String text = mValue; + + if (this.formatter != null) { + text = formatter.format(text); + } + + calibrateTextSize(mTextPaint, text, mBounds.width()); + mTextPaint.getTextBounds(text, 0, text.length(), mBounds); + mTextPaint.setTextAlign(Paint.Align.CENTER); + DrawableCompat.setTint(mPin, pinColor); + mPin.draw(canvas); + canvas.drawText(text, + mX, mY - mPinRadiusPx - mPinPadding + mTextYPadding, + mTextPaint); } - - calibrateTextSize(mTextPaint, text, mBounds.width()); - mTextPaint.getTextBounds(text, 0, text.length(), mBounds); - mTextPaint.setTextAlign(Paint.Align.CENTER); - DrawableCompat.setTint(mPin, pinColor); - mPin.draw(canvas); - canvas.drawText(text, - mX, mY - mPinRadiusPx - mPinPadding + mTextYPadding, - mTextPaint); } super.draw(canvas); } diff --git a/materialrangebar/src/main/java/com/appyvet/materialrangebar/RangeBar.java b/materialrangebar/src/main/java/com/appyvet/materialrangebar/RangeBar.java index a2009d64..3a5adb94 100644 --- a/materialrangebar/src/main/java/com/appyvet/materialrangebar/RangeBar.java +++ b/materialrangebar/src/main/java/com/appyvet/materialrangebar/RangeBar.java @@ -227,6 +227,8 @@ public class RangeBar extends View { private boolean mOnlyOnDrag = false; + private boolean mPinsEnabled = true; + private PinTextFormatter mPinTextFormatter = new PinTextFormatter() { @Override public String getText(String value) { @@ -405,12 +407,12 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) { mLeftThumb = new PinView(ctx); mLeftThumb.setFormatter(mFormatter); mLeftThumb.init(ctx, yPos, expandedPinRadius, mPinColor, mTextColor, mCircleSize, - mCircleColor, mCircleBoundaryColor, mCircleBoundarySize, mMinPinFont, mMaxPinFont, mArePinsTemporary); + mCircleColor, mCircleBoundaryColor, mCircleBoundarySize, mMinPinFont, mMaxPinFont, mArePinsTemporary, mPinsEnabled); } mRightThumb = new PinView(ctx); mRightThumb.setFormatter(mFormatter); mRightThumb.init(ctx, yPos, expandedPinRadius, mPinColor, mTextColor, mCircleSize, - mCircleColor, mCircleBoundaryColor, mCircleBoundarySize, mMinPinFont, mMaxPinFont, mArePinsTemporary); + mCircleColor, mCircleBoundaryColor, mCircleBoundarySize, mMinPinFont, mMaxPinFont, mArePinsTemporary, mPinsEnabled); // Create the underlying bar. final float marginLeft = Math.max(mExpandedPinRadius, mCircleSize); @@ -1157,6 +1159,20 @@ public void setPinTextFormatter(PinTextFormatter pinTextFormatter) { this.mPinTextFormatter = pinTextFormatter; } + /** + * Sets the visibility of pins by the supplied value + * + * @param enable boolean specifying whether pins are visible + */ + public void setPinsEnabled(boolean enable) { + mPinsEnabled = enable; + createPins(); + } + + public boolean arePinsEnabled() { + return mPinsEnabled; + } + // Private Methods ///////////////////////////////////////////////////////// /** @@ -1298,6 +1314,7 @@ private void rangeBarInit(Context context, AttributeSet attrs) { mIsRangeBar = ta.getBoolean(R.styleable.RangeBar_mrb_rangeBar, true); mOnlyOnDrag = ta.getBoolean(R.styleable.RangeBar_mrb_onlyOnDrag, false); + mPinsEnabled = ta.getBoolean(R.styleable.RangeBar_mrb_pinsEnabled, true); } finally { ta.recycle(); } @@ -1352,12 +1369,12 @@ private void createPins() { if (mIsRangeBar) { mLeftThumb = new PinView(ctx); mLeftThumb.init(ctx, yPos, expandedPinRadius, mPinColor, mTextColor, mCircleSize, mCircleColor, mCircleBoundaryColor, mCircleBoundarySize, - mMinPinFont, mMaxPinFont, mArePinsTemporary); + mMinPinFont, mMaxPinFont, mArePinsTemporary, mPinsEnabled); } mRightThumb = new PinView(ctx); mRightThumb .init(ctx, yPos, expandedPinRadius, mPinColor, mTextColor, mCircleSize, mCircleColor, mCircleBoundaryColor, mCircleBoundarySize - , mMinPinFont, mMaxPinFont, mArePinsTemporary); + , mMinPinFont, mMaxPinFont, mArePinsTemporary, mPinsEnabled); float marginLeft = getMarginLeft(); float barLength = getBarLength(); diff --git a/materialrangebar/src/main/res/values/attrs.xml b/materialrangebar/src/main/res/values/attrs.xml index fcaab051..ba64d0c8 100644 --- a/materialrangebar/src/main/res/values/attrs.xml +++ b/materialrangebar/src/main/res/values/attrs.xml @@ -32,6 +32,7 @@ + \ No newline at end of file