Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- added margin between bottom label text and slider, added font-family #197

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import androidx.annotation.Nullable;
import android.util.DisplayMetrics;
import android.util.TypedValue;

import androidx.annotation.Nullable;
import androidx.core.content.res.ResourcesCompat;

import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -66,6 +69,8 @@ public class Bar {

private int mTickDefaultColor;

private float mBottomLabelMarginTop = 0;

private List<Integer> mTickColors = new ArrayList<>();

// Constructor /////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -174,14 +179,18 @@ public Bar(Context ctx,
CharSequence[] tickTopLabels,
CharSequence[] tickBottomLabels,
String tickDefaultLabel,
float tickLabelSize) {
float tickLabelSize,
int mFontFamily) {
this(ctx, x, y, length, tickCount, tickHeight, barWeight, barColor, isBarRounded);

if (tickTopLabels != null || tickBottomLabels != null) {
// Creates the paint and sets the Paint values
mLabelPaint = new Paint();
mLabelPaint.setColor(tickLabelColor);
mLabelPaint.setAntiAlias(true);
if (mFontFamily > 0) {
mLabelPaint.setTypeface(ResourcesCompat.getFont(ctx, mFontFamily));
}
mTickLabelColor = tickLabelColor;
mTickLabelSelectedColor = tickLabelSelectedColor;
mTickTopLabels = tickTopLabels;
Expand Down Expand Up @@ -223,29 +232,32 @@ public Bar(Context ctx,
CharSequence[] tickTopLabels,
CharSequence[] tickBottomLabels,
String tickDefaultLabel,
float tickLabelSize) {
this(ctx, x, y, length, tickCount, tickHeight, barWeight, barColor, isBarRounded, tickLabelColor, tickLabelSelectedColor, tickTopLabels, tickBottomLabels, tickDefaultLabel, tickLabelSize);
float tickLabelSize,
int fontFamily) {
this(ctx, x, y, length, tickCount, tickHeight, barWeight, barColor, isBarRounded, tickLabelColor, tickLabelSelectedColor, tickTopLabels, tickBottomLabels, tickDefaultLabel, tickLabelSize, fontFamily);
mTickDefaultColor = tickDefaultColor;
mTickPaint.setColor(tickDefaultColor);
}

/**
* Bar constructor
*
* @param ctx the context
* @param x the start x co-ordinate
* @param y the y co-ordinate
* @param length the length of the bar in px
* @param tickCount the number of ticks on the bar
* @param tickHeight the height of each tick
* @param tickDefaultColor defualt tick color
* @param tickColors the colors of each tick
* @param barWeight the weight of the bar
* @param barColor the color of the bar
* @param isBarRounded if the bar has rounded edges or not
* @param tickLabelColor the color of each tick's label
* @param tickTopLabels the top label of each tick
* @param tickBottomLabels the top label of each tick
* @param ctx the context
* @param x the start x co-ordinate
* @param y the y co-ordinate
* @param length the length of the bar in px
* @param tickCount the number of ticks on the bar
* @param tickHeight the height of each tick
* @param tickDefaultColor defualt tick color
* @param tickColors the colors of each tick
* @param barWeight the weight of the bar
* @param barColor the color of the bar
* @param isBarRounded if the bar has rounded edges or not
* @param tickLabelColor the color of each tick's label
* @param tickTopLabels the top label of each tick
* @param tickBottomLabels the top label of each tick
* @param fontFamily font family
* @param bottomLabelMarginTop top margin between label and slider
*/
public Bar(Context ctx,
float x,
Expand All @@ -263,10 +275,13 @@ public Bar(Context ctx,
CharSequence[] tickTopLabels,
CharSequence[] tickBottomLabels,
String tickDefaultLabel,
float tickLabelSize) {
float tickLabelSize,
int fontFamily,
float bottomLabelMarginTop) {

this(ctx, x, y, length, tickCount, tickHeight, barWeight, barColor, isBarRounded, tickLabelColor, tickLabelSelectedColor, tickTopLabels, tickBottomLabels, tickDefaultLabel, tickLabelSize);
this(ctx, x, y, length, tickCount, tickHeight, barWeight, barColor, isBarRounded, tickLabelColor, tickLabelSelectedColor, tickTopLabels, tickBottomLabels, tickDefaultLabel, tickLabelSize, fontFamily);

mBottomLabelMarginTop = bottomLabelMarginTop;
mTickDefaultColor = tickDefaultColor;
mTickColors = tickColors;
}
Expand Down Expand Up @@ -442,12 +457,17 @@ private void drawTickLabel(Canvas canvas, final String label, float x, float pin
if (isTop) {
yPos = mY - labelBounds.height() - pinRadius;
} else {
yPos = mY + labelBounds.height() + pinRadius;
yPos = mY + labelBounds.height() + pinRadius + getPxByDp(mBottomLabelMarginTop);
}

canvas.drawText(label, xPos, yPos, mLabelPaint);
}

private int getPxByDp(float dp) {
DisplayMetrics metrics = mRes.getDisplayMetrics();
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, metrics);
}

private Paint getTick(int index) {

if (mTickColors != null && index < mTickColors.size()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ public class RangeBar extends View {

private float mTickLabelSize = DEFAULT_TICK_LABEL_FONT_SP;

private int mFontFamily = 0;

private float mBottomLabelMarginTop = 0;

private CharSequence[] mTickBottomLabels;

private CharSequence[] mTickTopLabels;
Expand Down Expand Up @@ -457,7 +461,7 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {

mBar = new Bar(ctx, marginLeft, yPos, barLength, mTickCount, mTickHeight, mTickDefaultColor, mTickColors,
mBarWeight, mBarColor, mIsBarRounded, mTickLabelColor, mTickLabelSelectedColor,
mTickTopLabels, mTickBottomLabels, mTickDefaultLabel, mTickLabelSize);
mTickTopLabels, mTickBottomLabels, mTickDefaultLabel, mTickLabelSize, mFontFamily, mBottomLabelMarginTop);

// Initialize thumbs to the desired indices
if (mIsRangeBar) {
Expand Down Expand Up @@ -1493,6 +1497,10 @@ 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);

mFontFamily = ta.getResourceId(R.styleable.RangeBar_mrb_fontFamily, 0);

mBottomLabelMarginTop = ta.getDimension(R.styleable.RangeBar_mrb_bottomLabel_marginTop, 0f);
} finally {
ta.recycle();
}
Expand All @@ -1518,7 +1526,9 @@ private void createBar() {
mTickTopLabels,
mTickBottomLabels,
mTickDefaultLabel,
mTickLabelSize);
mTickLabelSize,
mFontFamily,
mBottomLabelMarginTop);
invalidate();
}

Expand Down
2 changes: 2 additions & 0 deletions materialrangebar/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
<attr name="mrb_pinRadius" format="dimension" />
<attr name="mrb_connectingLineColors" format="reference" />
<attr name="mrb_onlyOnDrag" format="boolean" />
<attr name="mrb_fontFamily" format="reference" />
<attr name="mrb_bottomLabel_marginTop" format="dimension" />
</declare-styleable>

</resources>