Skip to content

Commit

Permalink
add pin show/hide according "krazykira" "oli107#164 (comment)"
Browse files Browse the repository at this point in the history
  • Loading branch information
harishsinghania committed Apr 23, 2020
1 parent 844d160 commit 6f33264
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 30 deletions.
2 changes: 1 addition & 1 deletion RangeBarSample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
app:mrb_rangeBarPaddingBottom="30dp"
app:mrb_rangeBar_rounded="true"
app:mrb_rightThumbColor="#1E88E5"
app:mrb_temporaryPins="true"
app:mrb_temporaryPins="ON_TOUCH_ONLY"
app:mrb_thumbBoundaryColor="@color/accent"
app:mrb_thumbBoundarySize="2dp"
app:mrb_thumbSize="10dp"
Expand Down
2 changes: 1 addition & 1 deletion RangeBarSample/src/main/res/layout/recyclerview_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
app:mrb_thumbBoundaryColor="@color/accent"
app:mrb_thumbBoundarySize="2dp"
app:mrb_thumbSize="10dp"
app:mrb_temporaryPins="true"
app:mrb_temporaryPins="ALWAYS"
app:mrb_tickBottomLabels="@array/ticks_labels"
app:mrb_tickDefaultLabel="label"
app:mrb_tickEnd="10"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class PinView extends View {

private float mMaxPinFont = RangeBar.DEFAULT_MAX_PIN_FONT_SP;

private boolean mPinsAreTemporary;
private int mPinsAreTemporary;

private boolean mHasBeenPressed = false;

Expand Down Expand Up @@ -127,7 +127,7 @@ 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, int pinsAreTemporary) {

mRes = ctx.getResources();
mPin = ContextCompat.getDrawable(ctx, R.drawable.rotate);
Expand Down Expand Up @@ -279,25 +279,27 @@ public void draw(Canvas canvas) {
canvas.drawCircle(mX, mY, mCircleBoundaryRadiusPx, mCircleBoundaryPaint);

//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 (mPinsAreTemporary != 2) {
if (mPinRadiusPx > 0 && (mHasBeenPressed || (mPinsAreTemporary == 0))) {
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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public class RangeBar extends View {

private boolean drawTicks = true;

private boolean mArePinsTemporary = true;
private int mArePinsTemporary = 1;

private boolean mOnlyOnDrag = false;

Expand Down Expand Up @@ -324,7 +324,7 @@ public Parcelable onSaveInstanceState() {
bundle.putFloat("BAR_PADDING_BOTTOM", mBarPaddingBottom);
bundle.putBoolean("IS_RANGE_BAR", mIsRangeBar);
bundle.putBoolean("IS_ONLY_ON_DRAG", mOnlyOnDrag);
bundle.putBoolean("ARE_PINS_TEMPORARY", mArePinsTemporary);
bundle.putInt("ARE_PINS_TEMPORARY", mArePinsTemporary);
bundle.putInt("LEFT_INDEX", mLeftIndex);
bundle.putInt("RIGHT_INDEX", mRightIndex);
bundle.putInt("MIN_INDEX_DISTANCE", mMinIndexDistance);
Expand Down Expand Up @@ -374,7 +374,7 @@ public void onRestoreInstanceState(Parcelable state) {
mBarPaddingBottom = bundle.getFloat("BAR_PADDING_BOTTOM");
mIsRangeBar = bundle.getBoolean("IS_RANGE_BAR");
mOnlyOnDrag = bundle.getBoolean("IS_ONLY_ON_DRAG");
mArePinsTemporary = bundle.getBoolean("ARE_PINS_TEMPORARY");
mArePinsTemporary = bundle.getInt("ARE_PINS_TEMPORARY");

mLeftIndex = bundle.getInt("LEFT_INDEX");
mRightIndex = bundle.getInt("RIGHT_INDEX");
Expand Down Expand Up @@ -848,7 +848,7 @@ public void setRangeBarEnabled(boolean isRangeBar) {
* stay
* drawn
*/
public void setTemporaryPins(boolean arePinsTemporary) {
public void setTemporaryPins(int arePinsTemporary) {
mArePinsTemporary = arePinsTemporary;
invalidate();
}
Expand Down Expand Up @@ -1479,7 +1479,7 @@ private void rangeBarInit(Context context, AttributeSet attrs) {
mActiveConnectingLineColors = new ArrayList<>(mConnectingLineColors);

mIsRangeBar = ta.getBoolean(R.styleable.RangeBar_mrb_rangeBar, true);
mArePinsTemporary = ta.getBoolean(R.styleable.RangeBar_mrb_temporaryPins, true);
mArePinsTemporary = ta.getInt(R.styleable.RangeBar_mrb_temporaryPins, 1);
mIsBarRounded = ta.getBoolean(R.styleable.RangeBar_mrb_rangeBar_rounded, false);

float density = mDisplayMetrices.density;
Expand Down Expand Up @@ -1812,7 +1812,7 @@ private void pressPin(final PinView thumb) {
if (mFirstSetTickCount) {
mFirstSetTickCount = false;
}
if (mArePinsTemporary) {
if (mArePinsTemporary == 2 || mArePinsTemporary == 1) {
ValueAnimator animator = ValueAnimator.ofFloat(0, mExpandedPinRadius);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

Expand Down Expand Up @@ -1842,7 +1842,7 @@ private void releasePin(final PinView thumb) {
int tickIndex = mBar.getNearestTickIndex(thumb);
thumb.setXValue(getPinValue(tickIndex));

if (mArePinsTemporary) {
if (mArePinsTemporary == 2 || mArePinsTemporary == 1) {
ValueAnimator animator = ValueAnimator.ofFloat(mExpandedPinRadius, 0);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

Expand Down
6 changes: 5 additions & 1 deletion materialrangebar/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
<attr name="mrb_pinMaxFont" format="dimension" />
<attr name="mrb_rangeBarPaddingBottom" format="dimension" />
<attr name="mrb_rangeBar" format="boolean" />
<attr name="mrb_temporaryPins" format="boolean" />
<attr name="mrb_temporaryPins" format="enum">
<enum name="ALWAYS" value="0" />
<enum name="ON_TOUCH_ONLY" value="1" />
<enum name="NEVER" value="2" />
</attr>
<attr name="mrb_rangeBar_rounded" format="boolean" />
<attr name="mrb_connectingLineWeight" format="dimension" />
<attr name="mrb_connectingLineColor" format="reference|color" />
Expand Down

0 comments on commit 6f33264

Please sign in to comment.