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

Add dividers plus other tweaks to thumb/indicator #115

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 @@ -32,6 +32,7 @@
import android.support.v4.view.MotionEventCompat;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.MotionEvent;
Expand All @@ -40,10 +41,12 @@
import android.view.ViewParent;

import org.adw.library.widgets.discreteseekbar.internal.PopupIndicator;
import org.adw.library.widgets.discreteseekbar.internal.ThumbStyle;
import org.adw.library.widgets.discreteseekbar.internal.compat.AnimatorCompat;
import org.adw.library.widgets.discreteseekbar.internal.compat.SeekBarCompat;
import org.adw.library.widgets.discreteseekbar.internal.drawable.MarkerDrawable;
import org.adw.library.widgets.discreteseekbar.internal.drawable.ThumbDrawable;
import org.adw.library.widgets.discreteseekbar.internal.drawable.TrackRectDividerDrawable;
import org.adw.library.widgets.discreteseekbar.internal.drawable.TrackRectDrawable;

import java.util.Formatter;
Expand Down Expand Up @@ -134,15 +137,20 @@ public int transform(int value) {
private ThumbDrawable mThumb;
private TrackRectDrawable mTrack;
private TrackRectDrawable mScrubber;
private TrackRectDividerDrawable mTrackDivider;
private Drawable mRipple;

private ThumbStyle thumbStyle = ThumbStyle.DEFAULT;

private int mTrackHeight;
private int mScrubberHeight;
private int mAddedTouchBounds;

private int mMax;
private int mMin;
private int mValue;
private int mSections = 0;
private boolean mIndicatorTextEnabled = true;
private int mKeyProgressIncrement = 1;
private boolean mMirrorForRtl = false;
private boolean mAllowTrackClick = true;
Expand Down Expand Up @@ -178,6 +186,8 @@ public DiscreteSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
setFocusable(true);
setWillNotDraw(false);



mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
float density = context.getResources().getDisplayMetrics().density;

Expand All @@ -187,6 +197,11 @@ public DiscreteSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
int max = 100;
int min = 0;
int value = 0;
int sections = 0;
int dividersWidth = 0;
int dividersHeight = 0;

thumbStyle = ThumbStyle.getThumbStyle(a.getInteger(R.styleable.DiscreteSeekBar_dsb_thumbStyle, thumbStyle.getValue()));
mMirrorForRtl = a.getBoolean(R.styleable.DiscreteSeekBar_dsb_mirrorForRtl, mMirrorForRtl);
mAllowTrackClick = a.getBoolean(R.styleable.DiscreteSeekBar_dsb_allowTrackClickToDrag, mAllowTrackClick);
mIndicatorPopupEnabled = a.getBoolean(R.styleable.DiscreteSeekBar_dsb_indicatorPopupEnabled, mIndicatorPopupEnabled);
Expand All @@ -203,6 +218,10 @@ public DiscreteSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
int indexMax = R.styleable.DiscreteSeekBar_dsb_max;
int indexMin = R.styleable.DiscreteSeekBar_dsb_min;
int indexValue = R.styleable.DiscreteSeekBar_dsb_value;
int sectionsValue = R.styleable.DiscreteSeekBar_dsb_sections;
int dividersWidthValue = R.styleable.DiscreteSeekBar_dsb_dividerWidth;
int dividersHeightValue = R.styleable.DiscreteSeekBar_dsb_dividerHeight;
int indicatorTextEnabledValue = R.styleable.DiscreteSeekBar_dsb_indicatorTextEnabled;
final TypedValue out = new TypedValue();
//Not sure why, but we wanted to be able to use dimensions here...
if (a.getValue(indexMax, out)) {
Expand All @@ -226,15 +245,31 @@ public DiscreteSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
value = a.getInteger(indexValue, value);
}
}
if(a.getValue(sectionsValue, out)){
sections = a.getInteger(sectionsValue, sections);
}
if(a.getValue(dividersWidthValue, out)){
dividersWidth = a.getInteger(dividersWidthValue, dividersWidth);
}
if(a.getValue(dividersHeightValue, out)){
dividersHeight = a.getInteger(dividersHeightValue, dividersHeight);
}
if(a.getValue(indicatorTextEnabledValue, out)){
mIndicatorTextEnabled = a.getBoolean(indicatorTextEnabledValue, mIndicatorTextEnabled);
}


mMin = min;
mMax = Math.max(min + 1, max);
mValue = Math.max(min, Math.min(max, value));
mSections = sections;
updateKeyboardRange();


mIndicatorFormatter = a.getString(R.styleable.DiscreteSeekBar_dsb_indicatorFormatter);

ColorStateList trackColor = a.getColorStateList(R.styleable.DiscreteSeekBar_dsb_trackColor);
ColorStateList trackDividerColor = a.getColorStateList(R.styleable.DiscreteSeekBar_dsb_trackDividerColor);
ColorStateList progressColor = a.getColorStateList(R.styleable.DiscreteSeekBar_dsb_progressColor);
ColorStateList rippleColor = a.getColorStateList(R.styleable.DiscreteSeekBar_dsb_rippleColor);
boolean editMode = isInEditMode();
Expand All @@ -244,10 +279,14 @@ public DiscreteSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
if (editMode || trackColor == null) {
trackColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{Color.GRAY});
}
if (editMode || trackDividerColor == null) {
trackDividerColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{Color.DKGRAY});
}
if (editMode || progressColor == null) {
progressColor = new ColorStateList(new int[][]{new int[]{}}, new int[]{DEFAULT_THUMB_COLOR});
}


mRipple = SeekBarCompat.getRipple(rippleColor);
if (isLollipopOrGreater) {
SeekBarCompat.setBackground(this, mRipple);
Expand All @@ -263,13 +302,19 @@ public DiscreteSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
mScrubber = shapeDrawable;
mScrubber.setCallback(this);

mTrackDivider = new TrackRectDividerDrawable(trackDividerColor, mSections, dividersWidth,dividersHeight);
mTrackDivider.setCallback(this);

mThumb = new ThumbDrawable(progressColor, thumbSize);
mThumb.setCallback(this);
mThumb.setBounds(0, 0, mThumb.getIntrinsicWidth(), mThumb.getIntrinsicHeight());


if (!editMode) {
mIndicator = new PopupIndicator(context, attrs, defStyleAttr, convertValueToMessage(mMax),

String message = mIndicatorTextEnabled ? convertValueToMessage(mMax) : "";

mIndicator = new PopupIndicator(context, attrs, defStyleAttr,message,
thumbSize, thumbSize + mAddedTouchBounds + separation);
mIndicator.setListener(mFloaterListener);
}
Expand Down Expand Up @@ -501,6 +546,24 @@ public void setTrackColor(@NonNull ColorStateList colorStateList) {
mTrack.setColorStateList(colorStateList);
}

/**
* Sets the color of the seekbar dividers
*
* @param color The color the track will be changed to
*/
public void setDividerTrackColor(int color) {
mTrackDivider.setColorStateList(ColorStateList.valueOf(color));
}

/**
* Sets the color of the seekbar dividers
*
* @param colorStateList The ColorStateList the track will be changed to
*/
public void setDividerTrackColor(@NonNull ColorStateList colorStateList) {
mTrackDivider.setColorStateList(colorStateList);
}

/**
* If {@code enabled} is false the indicator won't appear. By default popup indicator is
* enabled.
Expand All @@ -510,7 +573,7 @@ public void setIndicatorPopupEnabled(boolean enabled) {
}

private void updateIndicatorSizes() {
if (!isInEditMode()) {
if (!isInEditMode() && mIndicatorTextEnabled) {
if (mNumericTransformer.useStringTransform()) {
mIndicator.updateSizes(mNumericTransformer.transformToString(mMax));
} else {
Expand Down Expand Up @@ -618,6 +681,9 @@ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
mScrubber.setBounds(paddingLeft + halfThumb, bottom - halfThumb - scrubberHeight,
paddingLeft + halfThumb, bottom - halfThumb + scrubberHeight);

mTrackDivider.setBounds(paddingLeft + halfThumb, bottom - halfThumb - scrubberHeight,
getWidth() - halfThumb - paddingRight - addedThumb, bottom - halfThumb + scrubberHeight);

//Update the thumb position after size changed
updateThumbPosFromCurrentProgress();
}
Expand All @@ -630,7 +696,11 @@ protected synchronized void onDraw(Canvas canvas) {
super.onDraw(canvas);
mTrack.draw(canvas);
mScrubber.draw(canvas);
if(mSections > 0)
mTrackDivider.draw(canvas);
mThumb.draw(canvas);


}

@Override
Expand All @@ -650,22 +720,46 @@ private void updateFromDrawableState() {
pressed = true;
}
}
if (isEnabled() && (focused || pressed) && mIndicatorPopupEnabled) {
//We want to add a small delay here to avoid
//poping in/out on simple taps
removeCallbacks(mShowIndicatorRunnable);
postDelayed(mShowIndicatorRunnable, INDICATOR_DELAY_FOR_TAPS);
} else {
hideFloater();

if(thumbStyle.equals(ThumbStyle.DEFAULT)){
if (isEnabled() && (focused || pressed) && mIndicatorPopupEnabled) {
//We want to add a small delay here to avoid
//poping in/out on simple taps
removeCallbacks(mShowIndicatorRunnable);

postDelayed(mShowIndicatorRunnable, INDICATOR_DELAY_FOR_TAPS);
} else {
hideFloater();
}
}else if(thumbStyle.equals(ThumbStyle.ONLYMARKER)) {
if (isEnabled() && mIndicatorPopupEnabled) {
//We want to add a small delay here to avoid
//poping in/out on simple taps
removeCallbacks(mShowIndicatorRunnable);

postDelayed(mShowIndicatorRunnable, INDICATOR_DELAY_FOR_TAPS);

}
}else if(thumbStyle.equals(ThumbStyle.ONLYTHUMB)) {
// if (isEnabled() && (focused || pressed) && mIndicatorPopupEnabled) {
// //We want to add a small delay here to avoid
// //poping in/out on simple taps
// removeCallbacks(mShowIndicatorRunnable);
//
// postDelayed(mShowIndicatorRunnable, INDICATOR_DELAY_FOR_TAPS);
//
// }
}

mThumb.setState(state);
mTrack.setState(state);
mScrubber.setState(state);
mTrackDivider.setState(state);
mRipple.setState(state);
}

private void updateProgressMessage(int value) {
if (!isInEditMode()) {
if (!isInEditMode() && mIndicatorTextEnabled) {
if (mNumericTransformer.useStringTransform()) {
mIndicator.setValue(mNumericTransformer.transformToString(value));
} else {
Expand Down Expand Up @@ -700,7 +794,7 @@ private String convertValueToMessage(int value) {

@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
if (!isEnabled() || !isClickable()) {
return false;
}
int actionMasked = MotionEventCompat.getActionMasked(event);
Expand Down Expand Up @@ -809,7 +903,7 @@ boolean isAnimationRunning() {
return mPositionAnimator != null && mPositionAnimator.isRunning();
}

void animateSetProgress(int progress) {
public void animateSetProgress(int progress) {
final float curProgress = isAnimationRunning() ? getAnimationPosition() : getProgress();

if (progress < mMin) {
Expand Down Expand Up @@ -948,7 +1042,7 @@ private void setHotspot(float x, float y) {

@Override
protected boolean verifyDrawable(Drawable who) {
return who == mThumb || who == mTrack || who == mScrubber || who == mRipple || super.verifyDrawable(who);
return who == mThumb || who == mTrack || who == mScrubber || who == mTrackDivider || who == mRipple || super.verifyDrawable(who);
}

private void attemptClaimDrag() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ public Marker(Context context, AttributeSet attrs, int defStyleAttr, String maxV
public void resetSizes(String maxValue) {
DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
//Account for negative numbers... is there any proper way of getting the biggest string between our range????
mNumber.setText("-" + maxValue);
if(maxValue.length() == 0)
mNumber.setText("");
else
mNumber.setText("-" + maxValue);
//Do a first forced measure call for the TextView (with the biggest text content),
//to calculate the max width and use always the same.
//this avoids the TextView from shrinking and growing when the text content changes
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.adw.library.widgets.discreteseekbar.internal;

/**
* Created by USER on 8/12/2017.
*/

public enum ThumbStyle {

DEFAULT(0),
ONLYTHUMB(1),
ONLYMARKER(2);

private final int value;
private ThumbStyle(int value) {
this.value = value;
}

public int getValue() {
return value;
}

public static ThumbStyle getThumbStyle(int value) {
for (ThumbStyle thumbStyle : ThumbStyle.values()) {
if (thumbStyle.value == value)
return thumbStyle;
}
throw new IllegalArgumentException("No ThumbStyle with that value");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.adw.library.widgets.discreteseekbar.internal.drawable;

import android.content.res.ColorStateList;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.util.Log;

/**
* Created by USER on 8/12/2017.
*/

public class TrackRectDividerDrawable extends TrackRectDrawable {

public int sections;
private int dividersWidth;
private int dividersHeight;

public TrackRectDividerDrawable(@NonNull ColorStateList tintStateList, int sections, int dividersWidth, int dividersHeight) {
super(tintStateList);
this.sections = sections;
this.dividersWidth = dividersWidth;
this.dividersHeight = dividersHeight;
}

@Override
void doDraw(Canvas canvas, Paint paint) {

if(sections > 0){
float subSection = Math.abs(getBounds().left - getBounds().right) / sections;
float sectionsOffset = getBounds().left;

paint.setStrokeWidth(dividersWidth);

float midHeight = dividersHeight > -1 ? dividersHeight /2 : 0;

for(int i = 0; i <= sections; i++){
float xPos = (subSection * i) + sectionsOffset;
canvas.drawLine(xPos,getBounds().top - midHeight, xPos, getBounds().bottom +midHeight, paint );
}
}
}
}
Loading