Skip to content
This repository has been archived by the owner on Jul 17, 2022. It is now read-only.

Commit

Permalink
Animate state change of a SeekBar when setEnabled(boolean) is called
Browse files Browse the repository at this point in the history
  • Loading branch information
mardous committed Sep 2, 2020
1 parent e9c5dbc commit a79b4de
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 32 deletions.
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ext {
siteUrl = 'https://github.com/mardous/DiscreteSeekBar'
gitUrl = 'https://github.com/mardous/DiscreteSeekBar.git'

libraryVersion = '1.0.0'
libraryVersion = '1.1.0'
libraryVersionCode = buildVersionCode(libraryVersion)

developerId = 'mardous'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ public int transform(int value) {
private boolean mMirrorForRtl = false;
private boolean mAllowTrackClick = true;
private boolean mIndicatorPopupEnabled = true;
private boolean mAnimateEnabledState = true;
//We use our own Formatter to avoid creating new instances on every progress change
Formatter mFormatter;
private Formatter mFormatter;
private String mIndicatorFormatter;
private NumericTransformer mNumericTransformer;
private StringBuilder mFormatBuilder;
Expand Down Expand Up @@ -186,6 +187,8 @@ public DiscreteSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
int max = 100;
int min = 0;
int value = 0;

mAnimateEnabledState = a.getBoolean(R.styleable.DiscreteSeekBar_dsb_animateEnabledState, mAnimateEnabledState);
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 Down Expand Up @@ -270,6 +273,16 @@ public DiscreteSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
if (!editMode) {
mIndicator = new PopupIndicator(context, attrs, defStyleAttr, convertValueToMessage(mMax),
thumbSize, thumbSize + mAddedTouchBounds + separation);
MarkerDrawable.MarkerAnimationListener mFloaterListener = new MarkerDrawable.MarkerAnimationListener() {
@Override
public void onClosingComplete() {
mThumb.animateToNormal();
}

@Override
public void onOpeningComplete() {
}
};
mIndicator.setListener(mFloaterListener);
}
a.recycle();
Expand All @@ -281,7 +294,7 @@ public DiscreteSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
/**
* Sets the current Indicator formatter string
*
* @param formatter
* @param formatter The new formatter to use.
* @see String#format(String, Object...)
* @see #setNumericTransformer(DiscreteSeekBar.NumericTransformer)
*/
Expand All @@ -293,7 +306,7 @@ public void setIndicatorFormatter(@Nullable String formatter) {
/**
* Sets the current {@link DiscreteSeekBar.NumericTransformer}
*
* @param transformer
* @param transformer The new transformer to use.
* @see #getNumericTransformer()
*/
public void setNumericTransformer(@Nullable NumericTransformer transformer) {
Expand Down Expand Up @@ -508,6 +521,16 @@ public void setIndicatorPopupEnabled(boolean enabled) {
this.mIndicatorPopupEnabled = enabled;
}

/**
* Sets if the {@link #setEnabled(boolean)} method of this {@link DiscreteSeekBar}
* must change the view's state using a smooth animation.
*
* @param mAnimateEnabledState The new value.
*/
public void setAnimateEnabledState(boolean mAnimateEnabledState) {
this.mAnimateEnabledState = mAnimateEnabledState;
}

private void updateIndicatorSizes() {
if (!isInEditMode()) {
if (mNumericTransformer.useStringTransform()) {
Expand Down Expand Up @@ -833,11 +856,11 @@ private void animateSetProgress(int progress) {
mPositionAnimator = ValueAnimator.ofFloat(curProgress, progress);
mPositionAnimator.setDuration(PROGRESS_ANIMATION_DURATION);
mPositionAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
setAnimationPosition((float) valueAnimator.getAnimatedValue());
}
});
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
setAnimationPosition((float) valueAnimator.getAnimatedValue());
}
});

mPositionAnimator.start();
}
Expand Down Expand Up @@ -987,16 +1010,19 @@ private void hideFloater() {
}
}

private MarkerDrawable.MarkerAnimationListener mFloaterListener = new MarkerDrawable.MarkerAnimationListener() {
@Override
public void onClosingComplete() {
mThumb.animateToNormal();
}

@Override
public void onOpeningComplete() {
@Override
public void setEnabled(final boolean enabled) {
if (mAnimateEnabledState) {
animate().alpha(enabled ? 1.0f : 0.5f).setDuration(500).withEndAction(new Runnable() {
@Override
public void run() {
DiscreteSeekBar.super.setEnabled(enabled);
}
}).start();
} else {
super.setEnabled(enabled);
}
};
}

@Override
protected void onDetachedFromWindow() {
Expand Down Expand Up @@ -1040,14 +1066,14 @@ static class CustomState extends BaseSavedState {
private int max;
private int min;

public CustomState(Parcel source) {
CustomState(Parcel source) {
super(source);
progress = source.readInt();
max = source.readInt();
min = source.readInt();
}

public CustomState(Parcelable superState) {
CustomState(Parcelable superState) {
super(superState);
}

Expand All @@ -1059,18 +1085,16 @@ public void writeToParcel(Parcel outcoming, int flags) {
outcoming.writeInt(min);
}

public static final Creator<CustomState> CREATOR =
new Creator<CustomState>() {

@Override
public CustomState[] newArray(int size) {
return new CustomState[size];
}
public static final Creator<CustomState> CREATOR = new Creator<CustomState>() {
@Override
public CustomState[] newArray(int size) {
return new CustomState[size];
}

@Override
public CustomState createFromParcel(Parcel incoming) {
return new CustomState(incoming);
}
};
@Override
public CustomState createFromParcel(Parcel incoming) {
return new CustomState(incoming);
}
};
}
}
1 change: 1 addition & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@
<attr name="dsb_scrubberHeight" format="integer|dimension"/>
<attr name="dsb_thumbSize" format="integer|dimension"/>
<attr name="dsb_indicatorSeparation" format="integer|dimension"/>
<attr name="dsb_animateEnabledState" format="boolean"/>
</declare-styleable>
</resources>

0 comments on commit a79b4de

Please sign in to comment.