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

Support for different height on active bar #87

Open
wants to merge 2 commits 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 @@ -40,6 +40,8 @@ public void onCreate(Bundle savedInstanceState) {
rangeSeekBar.setSelectedMinValue(20);
rangeSeekBar.setSelectedMaxValue(88);

rangeSeekBar.setActiveBarHeight(5);

// Add to layout
LinearLayout layout = (LinearLayout) findViewById(R.id.seekbar_placeholder);
layout.addView(rangeSeekBar);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ public class RangeSeekBar<T extends Number> extends ImageView {
private static final int DEFAULT_TEXT_DISTANCE_TO_TOP_IN_DP = 8;

private static final int LINE_HEIGHT_IN_DP = 1;
private static final int ACTIVE_LINE_HEIGHT_IN_DP = 1;
private final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private final Paint shadowPaint = new Paint();

Expand Down Expand Up @@ -135,7 +136,8 @@ public class RangeSeekBar<T extends Number> extends ImageView {
private Matrix thumbShadowMatrix = new Matrix();

private boolean activateOnDefaultValues;

private float activeBarHeight;
private float barHeight;

public RangeSeekBar(Context context) {
super(context);
Expand Down Expand Up @@ -168,7 +170,6 @@ private T extractNumericValueFromAttributes(TypedArray a, int attribute, int def
}

private void init(Context context, AttributeSet attrs) {
float barHeight;
int thumbNormal = R.drawable.seek_thumb_normal;
int thumbPressed = R.drawable.seek_thumb_pressed;
int thumbDisabled = R.drawable.seek_thumb_disabled;
Expand All @@ -182,6 +183,7 @@ private void init(Context context, AttributeSet attrs) {
setRangeToDefaultValues();
internalPad = PixelUtil.dpToPx(context, INITIAL_PADDING_IN_DP);
barHeight = PixelUtil.dpToPx(context, LINE_HEIGHT_IN_DP);
activeBarHeight = PixelUtil.dpToPx(context, ACTIVE_LINE_HEIGHT_IN_DP);
activeColor = ACTIVE_COLOR;
defaultColor = Color.GRAY;
alwaysActive = false;
Expand All @@ -205,6 +207,7 @@ private void init(Context context, AttributeSet attrs) {
showLabels = a.getBoolean(R.styleable.RangeSeekBar_showLabels, true);
internalPad = a.getDimensionPixelSize(R.styleable.RangeSeekBar_internalPadding, INITIAL_PADDING_IN_DP);
barHeight = a.getDimensionPixelSize(R.styleable.RangeSeekBar_barHeight, LINE_HEIGHT_IN_DP);
activeBarHeight = a.getDimensionPixelSize(R.styleable.RangeSeekBar_activeBarHeight, ACTIVE_LINE_HEIGHT_IN_DP);
activeColor = a.getColor(R.styleable.RangeSeekBar_activeColor, ACTIVE_COLOR);
defaultColor = a.getColor(R.styleable.RangeSeekBar_defaultColor, Color.GRAY);
alwaysActive = a.getBoolean(R.styleable.RangeSeekBar_alwaysActive, false);
Expand Down Expand Up @@ -291,6 +294,26 @@ public void setTextAboveThumbsColorResource(@ColorRes int resId) {
setTextAboveThumbsColor(getResources().getColor(resId));
}

/**
* Sets the height of the active bar
*
* @param activeBarHeight the height in DP uints
*/

public void setActiveBarHeight(int activeBarHeight) {
this.activeBarHeight = PixelUtil.dpToPx(getContext(), activeBarHeight);
}

/**
* Sets the height of the bar
*
* @param barHeight the height in DP uints
*/

public void setBarHeight(int barHeight) {
this.barHeight = PixelUtil.dpToPx(getContext(), barHeight);
}

@SuppressWarnings("unchecked")
// only used to set default values when initialised from XML without any values specified
private void setRangeToDefaultValues() {
Expand Down Expand Up @@ -609,6 +632,8 @@ protected synchronized void onDraw(@NonNull Canvas canvas) {
// draw seek bar background line
rect.left = padding;
rect.right = getWidth() - padding;
rect.top = textOffset + thumbHalfHeight - barHeight / 2;
rect.bottom = textOffset + thumbHalfHeight + barHeight / 2;
canvas.drawRect(rect, paint);

boolean selectedValuesAreDefault = (normalizedMinValue <= minDeltaForDefault && normalizedMaxValue >= 1 - minDeltaForDefault);
Expand All @@ -618,11 +643,15 @@ protected synchronized void onDraw(@NonNull Canvas canvas) {
activeColor; // non default, filter is active

// draw seek bar active range line
rect.left = normalizedToScreen(normalizedMinValue);
rect.right = normalizedToScreen(normalizedMaxValue);

paint.setColor(colorToUseForButtonsAndHighlightedLine);
canvas.drawRect(rect, paint);
if (activateOnDefaultValues || !selectedValuesAreDefault) {
rect.left = normalizedToScreen(normalizedMinValue);
rect.right = normalizedToScreen(normalizedMaxValue);
rect.top = textOffset + thumbHalfHeight - activeBarHeight / 2;
rect.bottom = textOffset + thumbHalfHeight + activeBarHeight / 2;

paint.setColor(colorToUseForButtonsAndHighlightedLine);
canvas.drawRect(rect, paint);
}

// draw minimum thumb (& shadow if requested) if not a single thumb control
if (!singleThumb) {
Expand Down Expand Up @@ -708,9 +737,11 @@ protected void onRestoreInstanceState(Parcelable parcel) {
/**
* Draws the "normal" resp. "pressed" thumb image on specified x-coordinate.
*
* @param screenCoord The x-coordinate in screen space where to draw the image.
* @param pressed Is the thumb currently in "pressed" state?
* @param canvas The canvas to draw upon.
*
* @param screenCoord The x-coordinate in screen space where to draw the image.
* @param pressed Is the thumb currently in "pressed" state?
* @param canvas The canvas to draw upon.
* @param areSelectedValuesDefault Should show active state button if selected values are selected
*/
private void drawThumb(float screenCoord, boolean pressed, Canvas canvas, boolean areSelectedValuesDefault) {
Bitmap buttonToDraw;
Expand Down
3 changes: 3 additions & 0 deletions rangeseekbar/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
<!-- the height of the bar -->
<attr name="barHeight" format="dimension"/>

<!-- the height of the active bar -->
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good, but there should also be a way to set this in code

<attr name="activeBarHeight" format="dimension"/>

<!-- the color of the bar that is not selected -->
<attr name="defaultColor" format="color"/>

Expand Down