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

Simple support for to handle differents icons with backward compatibi… #100

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 @@ -91,6 +91,10 @@ public class RangeSeekBar<T extends Number> extends ImageView {
private Bitmap thumbPressedImage;
private Bitmap thumbDisabledImage;

private Bitmap thumbImageLeft, thumbImageRight;
private Bitmap thumbPressedImageLeft, thumbPressedImageRight;
private Bitmap thumbDisabledImageLeft, thumbDisabledImageRight;

private float thumbHalfWidth;
private float thumbHalfHeight;

Expand Down Expand Up @@ -212,17 +216,62 @@ private void init(Context context, AttributeSet attrs) {
alwaysActive = a.getBoolean(R.styleable.RangeSeekBar_alwaysActive, false);

Drawable normalDrawable = a.getDrawable(R.styleable.RangeSeekBar_thumbNormal);
if (normalDrawable != null) {

if (normalDrawable == null) {

Drawable normalDrawableLeft = a.getDrawable(R.styleable.RangeSeekBar_thumbNormalLeft);
Drawable normalDrawableRight = a.getDrawable(R.styleable.RangeSeekBar_thumbNormalRight);

if (normalDrawableLeft != null && normalDrawableRight != null) {
thumbImageLeft = BitmapUtil.drawableToBitmap(normalDrawableLeft);
thumbImageRight = BitmapUtil.drawableToBitmap(normalDrawableRight);
if (!BitmapUtil.haveSameSize(thumbImageLeft, thumbImageRight))
thumbImageLeft = thumbImageRight = null;
} else {
thumbImageLeft = thumbImageRight = null;
}
} else {
thumbImage = BitmapUtil.drawableToBitmap(normalDrawable);
}

Drawable disabledDrawable = a.getDrawable(R.styleable.RangeSeekBar_thumbDisabled);
if (disabledDrawable != null) {

if (disabledDrawable == null) {

Drawable disabledDrawableLeft = a.getDrawable(R.styleable.RangeSeekBar_thumbDisabledLeft);
Drawable disabledDrawableRight = a.getDrawable(R.styleable.RangeSeekBar_thumbDisabledRight);

if (disabledDrawableLeft != null && disabledDrawableRight != null) {
thumbDisabledImageLeft = BitmapUtil.drawableToBitmap(disabledDrawableLeft);
thumbDisabledImageRight = BitmapUtil.drawableToBitmap(disabledDrawableRight);
if (!BitmapUtil.haveSameSize(thumbDisabledImageLeft, thumbDisabledImageRight))
thumbDisabledImageRight = thumbDisabledImageLeft = null;
} else {
thumbDisabledImageLeft = thumbPressedImageRight = null;
}
} else {
thumbDisabledImage = BitmapUtil.drawableToBitmap(disabledDrawable);
}

Drawable pressedDrawable = a.getDrawable(R.styleable.RangeSeekBar_thumbPressed);
if (pressedDrawable != null) {

if (pressedDrawable == null) {

Drawable pressedDrawableLeft = a.getDrawable(R.styleable.RangeSeekBar_thumbPressedLeft);
Drawable pressedDrawableRight = a.getDrawable(R.styleable.RangeSeekBar_thumbPressedRight);

if (pressedDrawableLeft != null && pressedDrawableRight != null) {
thumbPressedImageLeft = BitmapUtil.drawableToBitmap(pressedDrawableLeft);
thumbPressedImageRight = BitmapUtil.drawableToBitmap(pressedDrawableRight);
if (!BitmapUtil.haveSameSize(thumbPressedImageLeft, thumbPressedImageRight))
thumbPressedImageRight = thumbPressedImageLeft = null;
} else {
thumbPressedImageLeft = thumbPressedImageRight = null;
}
} else {
thumbPressedImage = BitmapUtil.drawableToBitmap(pressedDrawable);
}

thumbShadow = a.getBoolean(R.styleable.RangeSeekBar_thumbShadow, false);
thumbShadowColor = a.getColor(R.styleable.RangeSeekBar_thumbShadowColor, defaultShadowColor);
thumbShadowXOffset = a.getDimensionPixelSize(R.styleable.RangeSeekBar_thumbShadowXOffset, defaultShadowXOffset);
Expand All @@ -235,18 +284,28 @@ private void init(Context context, AttributeSet attrs) {
}
}

if (thumbImage == null) {
thumbImage = BitmapFactory.decodeResource(getResources(), thumbNormal);
if (thumbImage != null) {
thumbImageLeft = thumbImageRight = thumbImage;
} else if (thumbImageLeft == null || thumbImageRight == null) {
thumbImageLeft = thumbImageRight = BitmapFactory.decodeResource(getResources(), thumbNormal);
}
if (thumbPressedImage == null) {
thumbPressedImage = BitmapFactory.decodeResource(getResources(), thumbPressed);

if (thumbPressedImage != null) {
thumbPressedImageLeft = thumbPressedImageRight = thumbPressedImage;
} else if (thumbPressedImageLeft == null || thumbPressedImageRight == null) {
thumbPressedImageLeft = thumbPressedImageRight = BitmapFactory.decodeResource(getResources(), thumbPressed);
}
if (thumbDisabledImage == null) {
thumbDisabledImage = BitmapFactory.decodeResource(getResources(), thumbDisabled);

if (thumbDisabledImage != null) {
thumbDisabledImageLeft = thumbDisabledImageRight = thumbDisabledImage;
} else if (thumbDisabledImageLeft == null || thumbDisabledImageRight == null) {
thumbDisabledImageLeft = thumbDisabledImageRight = BitmapFactory.decodeResource(getResources(), thumbDisabled);
}

thumbHalfWidth = 0.5f * thumbImage.getWidth();
thumbHalfHeight = 0.5f * thumbImage.getHeight();
// At this point, thumbImageLeft and thumbImageRight should be have same dimensions, use
// thumbImageLeft as reference
thumbHalfWidth = 0.5f * thumbImageLeft.getWidth();
thumbHalfHeight = 0.5f * thumbImageLeft.getHeight();

setValuePrimAndNumberType();

Expand Down Expand Up @@ -593,7 +652,7 @@ protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpe
width = MeasureSpec.getSize(widthMeasureSpec);
}

int height = thumbImage.getHeight()
int height = thumbImageLeft.getHeight()
+ (!showTextAboveThumbs ? 0 : PixelUtil.dpToPx(getContext(), HEIGHT_IN_DP))
+ (thumbShadow ? thumbShadowYOffset + thumbShadowBlur : 0);
if (MeasureSpec.UNSPECIFIED != MeasureSpec.getMode(heightMeasureSpec)) {
Expand Down Expand Up @@ -650,15 +709,15 @@ protected synchronized void onDraw(@NonNull Canvas canvas) {
drawThumbShadow(normalizedToScreen(normalizedMinValue), canvas);
}
drawThumb(normalizedToScreen(normalizedMinValue), Thumb.MIN.equals(pressedThumb), canvas,
selectedValuesAreDefault);
selectedValuesAreDefault, true);
}

// draw maximum thumb & shadow (if necessary)
if (thumbShadow) {
drawThumbShadow(normalizedToScreen(normalizedMaxValue), canvas);
}
drawThumb(normalizedToScreen(normalizedMaxValue), Thumb.MAX.equals(pressedThumb), canvas,
selectedValuesAreDefault);
selectedValuesAreDefault, false);

// draw the text if sliders have moved from default edges
if (showTextAboveThumbs && (activateOnDefaultValues || !selectedValuesAreDefault)) {
Expand Down Expand Up @@ -731,13 +790,17 @@ protected void onRestoreInstanceState(Parcelable parcel) {
* @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 isLeft Are drawing left thumb?
*/
private void drawThumb(float screenCoord, boolean pressed, Canvas canvas, boolean areSelectedValuesDefault) {
private void drawThumb(float screenCoord, boolean pressed, Canvas canvas, boolean areSelectedValuesDefault, boolean isLeft) {
Bitmap buttonToDraw;
if (!activateOnDefaultValues && areSelectedValuesDefault) {
buttonToDraw = thumbDisabledImage;
buttonToDraw = isLeft ? thumbDisabledImageLeft : thumbDisabledImageRight ;
} else {
buttonToDraw = pressed ? thumbPressedImage : thumbImage;
if (isLeft)
buttonToDraw = pressed ? thumbPressedImageLeft : thumbImageLeft;
else
buttonToDraw = pressed ? thumbPressedImageRight : thumbImageRight;
}

canvas.drawBitmap(buttonToDraw, screenCoord - thumbHalfWidth,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ public static Bitmap drawableToBitmap(Drawable drawable) {

return bitmap;
}

public static boolean haveSameSize(Bitmap b1, Bitmap b2){
return b1.getWidth() == b2.getWidth() && b1.getHeight() == b2.getHeight();
}
}
7 changes: 7 additions & 0 deletions rangeseekbar/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@
<attr name="thumbPressed" format="reference"/>
<attr name="thumbDisabled" format="reference"/>

<attr name="thumbNormalLeft" format="reference"/>
<attr name="thumbPressedLeft" format="reference"/>
<attr name="thumbDisabledLeft" format="reference"/>
<attr name="thumbNormalRight" format="reference"/>
<attr name="thumbPressedRight" format="reference"/>
<attr name="thumbDisabledRight" format="reference"/>

<!-- thumb shadow specifications -->
<attr name="thumbShadow" format="boolean"/>
<attr name="thumbShadowColor" format="color"/>
Expand Down