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 1 commit
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 @@ -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 @@ -116,6 +117,7 @@ public class RangeSeekBar<T extends Number> extends ImageView {
private int textSize;
private int distanceToTop;
private RectF rect;
private RectF rectActive;
Copy link
Owner

Choose a reason for hiding this comment

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

I'm thinking that this doesn't need another rect on top of it, could be done with the one we have that's already drawing the line, just need to adapt its top and bottom when we draw it if it's active


private boolean singleThumb;
private boolean alwaysActive;
Expand Down Expand Up @@ -169,6 +171,7 @@ private T extractNumericValueFromAttributes(TypedArray a, int attribute, int def

private void init(Context context, AttributeSet attrs) {
float barHeight;
float activeBarHeight;
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 +185,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 +209,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 @@ -258,6 +263,11 @@ private void init(Context context, AttributeSet attrs) {
getWidth() - padding,
textOffset + thumbHalfHeight + barHeight / 2);

rectActive = new RectF(padding,
textOffset + thumbHalfHeight - activeBarHeight / 2,
getWidth() - padding,
textOffset + thumbHalfHeight + activeBarHeight / 2);

// make RangeSeekBar focusable. This solves focus handling issues in case EditText widgets are being used along with the RangeSeekBar within ScrollViews.
setFocusable(true);
setFocusableInTouchMode(true);
Expand Down Expand Up @@ -640,6 +650,13 @@ protected synchronized void onDraw(@NonNull Canvas canvas) {
drawThumb(normalizedToScreen(normalizedMaxValue), Thumb.MAX.equals(pressedThumb), canvas,
selectedValuesAreDefault);

// draw activeBar if sliders have moved from default edges or is always active
if (alwaysActive || (activateOnDefaultValues || !selectedValuesAreDefault)) {
rectActive.left = normalizedToScreen(normalizedMinValue);
rectActive.right = normalizedToScreen(normalizedMaxValue);
canvas.drawRect(rectActive, paint);
}

// draw the text if sliders have moved from default edges
if (showTextAboveThumbs && (activateOnDefaultValues || !selectedValuesAreDefault)) {
paint.setTextSize(textSize);
Expand Down Expand Up @@ -707,14 +724,18 @@ protected void onRestoreInstanceState(Parcelable parcel) {

/**
* Draws the "normal" resp. "pressed" thumb image on specified x-coordinate.
* alwaysActive variable overwrites activateOnDefaultValues
*
* @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;
if (!activateOnDefaultValues && areSelectedValuesDefault) {
if (alwaysActive) {
Copy link
Owner

Choose a reason for hiding this comment

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

Why did you need this? As far as I can tell, if the alwaysActive flag is on, the buttons never show as disabled

buttonToDraw = pressed ? thumbPressedImage : thumbImage;
} else if (!activateOnDefaultValues && areSelectedValuesDefault){
buttonToDraw = thumbDisabledImage;
} else {
buttonToDraw = pressed ? thumbPressedImage : thumbImage;
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