Skip to content
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0-rc1'
classpath 'com.android.tools.build:gradle:2.2.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public DiscreteSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
// endregion

// region Helper Methods
private void init(Context context, AttributeSet attrs){
private void init(Context context, AttributeSet attrs) {
setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
Expand All @@ -63,53 +63,78 @@ public void onStartTrackingTouch(SeekBar seekBar) {

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if(fromUser)
fromUserCount+=1;
if (fromUser)
fromUserCount += 1;
}

@Override
public void onStopTrackingTouch(final SeekBar seekBar) {
int oldProgress = seekBar.getProgress();
final int newProgress;
if((oldProgress % stepSize) >= stepSize/2F){
newProgress = (int)(((oldProgress/(int)stepSize)+1)*stepSize);
if ((oldProgress % stepSize) >= stepSize / 2F) {
newProgress = (int) (((oldProgress / (int) stepSize) + 1) * stepSize);
} else {
newProgress = (int)(((oldProgress/(int)stepSize))*stepSize);
newProgress = (int) (((oldProgress / (int) stepSize)) * stepSize);
}

if(fromUserCount>1){ // SeekBar Dragged
if (fromUserCount > 1) { // SeekBar Dragged
ObjectAnimator animation = ObjectAnimator.ofInt(seekBar, PROGRESS_PROPERTY, oldProgress, newProgress);
animation.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime));
animation.setInterpolator(new DecelerateInterpolator());
animation.start();
} else { // SeekBar Clicked
ObjectAnimator animation = ObjectAnimator.ofInt(seekBar, PROGRESS_PROPERTY, superOldProgress, newProgress);
animation.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime));
animation.setInterpolator(new DecelerateInterpolator());
animation.start();
doAnimation(seekBar, newProgress);
}

fromUserCount = 0;
if(onDiscreteSeekBarChangeListener != null){
onDiscreteSeekBarChangeListener.onPositionChanged(newProgress/MULTIPLIER);
if (onDiscreteSeekBarChangeListener != null) {
onDiscreteSeekBarChangeListener.onPositionChanged(newProgress / MULTIPLIER);
}
}
});
}

private void doAnimation(SeekBar seekBar, int newProgress) {
ObjectAnimator animation = ObjectAnimator.ofInt(seekBar, PROGRESS_PROPERTY, superOldProgress, newProgress);
animation.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime));
animation.setInterpolator(new DecelerateInterpolator());
animation.start();
}

public void setTickMarkCount(int tickMarkCount) {
this.tickMarkCount = tickMarkCount < 2 ? 2 : tickMarkCount;
setMax((this.tickMarkCount-1) * MULTIPLIER);
this.stepSize = getMax()/(this.tickMarkCount-1);
setMax((this.tickMarkCount - 1) * MULTIPLIER);
this.stepSize = getMax() / (this.tickMarkCount - 1);
}

public void setOnDiscreteSeekBarChangeListener(OnDiscreteSeekBarChangeListener onDiscreteSeekBarChangeListener){
public void setOnDiscreteSeekBarChangeListener(OnDiscreteSeekBarChangeListener onDiscreteSeekBarChangeListener) {
this.onDiscreteSeekBarChangeListener = onDiscreteSeekBarChangeListener;
}

public void setPosition(int position){
this.setProgress(position*(int)stepSize);

public void setPosition(int position) {
int progress = position * (int) stepSize;
this.setProgress(progress);
}

public void setPositionAnimated(int position) {

superOldProgress = getProgress();

int oldProgress = position * (int) stepSize;

final int newProgress;

if ((oldProgress % stepSize) >= stepSize / 2F) {
newProgress = (int) (((oldProgress / (int) stepSize) + 1) * stepSize);
} else {
newProgress = (int) (((oldProgress / (int) stepSize)) * stepSize);
}

doAnimation(this, newProgress);
this.setProgress(newProgress);
}

// endregion


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public DiscreteSlider(Context context, AttributeSet attrs, int defStyleAttr) {
// endregion

// region Helper Methods
private void init(Context context, AttributeSet attrs){
private void init(Context context, AttributeSet attrs) {
TypedArray attributeArray = context.obtainStyledAttributes(
attrs,
R.styleable.DiscreteSlider);
Expand All @@ -71,6 +71,7 @@ private void init(Context context, AttributeSet attrs){
tickMarkCount = attributeArray.getInteger(R.styleable.DiscreteSlider_tickMarkCount, 5);
tickMarkRadius = attributeArray.getDimension(R.styleable.DiscreteSlider_tickMarkRadius, 8);
position = attributeArray.getInteger(R.styleable.DiscreteSlider_position, 0);

horizontalBarThickness = attributeArray.getDimension(R.styleable.DiscreteSlider_horizontalBarThickness, 4);
backdropFillColor = attributeArray.getColor(R.styleable.DiscreteSlider_backdropFillColor, Color.GRAY);
backdropStrokeColor = attributeArray.getColor(R.styleable.DiscreteSlider_backdropStrokeColor, Color.GRAY);
Expand All @@ -82,8 +83,8 @@ private void init(Context context, AttributeSet attrs){
}

View view = inflate(context, R.layout.discrete_slider, this);
discreteSliderBackdrop = (DiscreteSliderBackdrop)view.findViewById(R.id.discrete_slider_backdrop);
discreteSeekBar = (DiscreteSeekBar)view.findViewById(R.id.discrete_seek_bar);
discreteSliderBackdrop = (DiscreteSliderBackdrop) view.findViewById(R.id.discrete_slider_backdrop);
discreteSeekBar = (DiscreteSeekBar) view.findViewById(R.id.discrete_seek_bar);

setTickMarkCount(tickMarkCount);
setTickMarkRadius(tickMarkRadius);
Expand All @@ -95,61 +96,81 @@ private void init(Context context, AttributeSet attrs){
setThumb(thumb);
setProgressDrawable(progressDrawable);

discreteSeekBar.setPadding(discreteSeekBarLeftPadding,0,discreteSeekBarRightPadding,0);
discreteSeekBar.setPadding(discreteSeekBarLeftPadding, 0, discreteSeekBarRightPadding, 0);

discreteSeekBar.setOnDiscreteSeekBarChangeListener(new DiscreteSeekBar.OnDiscreteSeekBarChangeListener() {
@Override
public void onPositionChanged(int position) {
if(onDiscreteSliderChangeListener != null){
if (onDiscreteSliderChangeListener != null) {
onDiscreteSliderChangeListener.onPositionChanged(position);
}
}
});
}

public void setTickMarkCount(int tickMarkCount){
public void setTickMarkCount(int tickMarkCount) {
discreteSliderBackdrop.setTickMarkCount(tickMarkCount);
discreteSeekBar.setTickMarkCount(tickMarkCount);
}

public void setTickMarkRadius(float tickMarkRadius){
public void setTickMarkRadius(float tickMarkRadius) {
discreteSliderBackdrop.setTickMarkRadius(tickMarkRadius);
}

public void setPosition(int position) {
if(position<0){
private void validatePsition(int position) {
if (position < 0) {
this.position = 0;
} else if(position>tickMarkCount-1){
this.position = tickMarkCount-1;
} else if (position > tickMarkCount - 1) {
this.position = tickMarkCount - 1;
} else {
this.position = position;
}
}

public void setPosition(int position) {
validatePsition(position);
discreteSeekBar.setPosition(this.position);
if (onDiscreteSliderChangeListener != null) {
onDiscreteSliderChangeListener.onPositionChanged(position);
}
}

/**
* Sets the current position of mark and animate it
*
* @param position
*/
public void setPositionAnimated(int position) {
validatePsition(position);
discreteSeekBar.setPositionAnimated(this.position);
if (onDiscreteSliderChangeListener != null) {
onDiscreteSliderChangeListener.onPositionChanged(position);
}
}

public void setHorizontalBarThickness(float horizontalBarThickness){
public void setHorizontalBarThickness(float horizontalBarThickness) {
discreteSliderBackdrop.setHorizontalBarThickness(horizontalBarThickness);
}

public void setBackdropFillColor(int backdropFillColor){
public void setBackdropFillColor(int backdropFillColor) {
discreteSliderBackdrop.setBackdropFillColor(backdropFillColor);
}

public void setBackdropStrokeColor(int backdropStrokeColor){
public void setBackdropStrokeColor(int backdropStrokeColor) {
discreteSliderBackdrop.setBackdropStrokeColor(backdropStrokeColor);
}

public void setBackdropStrokeWidth(float backdropStrokeWidth){
public void setBackdropStrokeWidth(float backdropStrokeWidth) {
discreteSliderBackdrop.setBackdropStrokeWidth(backdropStrokeWidth);
}

public void setThumb(Drawable thumb){
if(thumb != null)
public void setThumb(Drawable thumb) {
if (thumb != null)
discreteSeekBar.setThumb(thumb);
}

public void setProgressDrawable(Drawable progressDrawable){
if(progressDrawable != null)
public void setProgressDrawable(Drawable progressDrawable) {
if (progressDrawable != null)
discreteSeekBar.setProgressDrawable(progressDrawable);
}

Expand Down
Loading