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

Commit

Permalink
Add opportunity to disable animation (close #12)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonKozyriatskyi committed Sep 2, 2018
1 parent d25886f commit 7ead00a
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ circularProgress.getMaxProgress() // returns 10000
| Direction of the progress arc (`clockwise` or `counterclockwise`) | `app:direction` | setter: `setDirection(direction)`<br/>getter: `getDirection()` | `counterclockwise` |
| Start angle. Checkout [Start angle](#setting-start-angle) section. | `app:startAngle` | setter: `setStartAngle(startAngle)`<br/>getter: `getStartAngle()` | `270` |
| Progress cap | `app:progressCap` | setter: `setProgressStrokeCap(cap)`<br/>getter: `getProgressStrokeCap()` | `CAP_ROUND` |
| Progress animation | `app:enableProgressAnimation` | setter: `setAnimationEnabled(enableAnimation)`<br/>getter: `isAnimationEnabled()` | `true` |

---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ public class CircularProgressIndicator extends View {

private float radius;

private boolean shouldDrawDot = true;
private boolean shouldDrawDot;

private double maxProgressValue = 100.0;
private double progressValue = 0.0;

private boolean isAnimationEnabled;

@Direction
private int direction = DIRECTION_COUNTERCLOCKWISE;

Expand Down Expand Up @@ -131,7 +133,7 @@ private void init(@NonNull Context context, @Nullable AttributeSet attrs) {
textColor = a.getColor(R.styleable.CircularProgressIndicator_textColor, progressColor);
textSize = a.getDimensionPixelSize(R.styleable.CircularProgressIndicator_textSize, textSize);

shouldDrawDot = a.getBoolean(R.styleable.CircularProgressIndicator_drawDot, shouldDrawDot);
shouldDrawDot = a.getBoolean(R.styleable.CircularProgressIndicator_drawDot, true);
dotColor = a.getColor(R.styleable.CircularProgressIndicator_dotColor, progressColor);
dotWidth = a.getDimensionPixelSize(R.styleable.CircularProgressIndicator_dotWidth, progressStrokeWidth);

Expand All @@ -140,6 +142,8 @@ private void init(@NonNull Context context, @Nullable AttributeSet attrs) {
startAngle = DEFAULT_PROGRESS_START_ANGLE;
}

isAnimationEnabled = a.getBoolean(R.styleable.CircularProgressIndicator_enableProgressAnimation, true);

direction = a.getInt(R.styleable.CircularProgressIndicator_direction, DIRECTION_COUNTERCLOCKWISE);

int cap = a.getInt(R.styleable.CircularProgressIndicator_progressCap, CAP_ROUND);
Expand Down Expand Up @@ -330,8 +334,6 @@ public void setProgress(double current, double max) {
finalAngle = current / max * 360;
}

final PropertyValuesHolder angleProperty = PropertyValuesHolder.ofInt(PROPERTY_ANGLE, sweepAngle, (int) finalAngle);

double oldCurrentProgress = progressValue;

maxProgressValue = max;
Expand All @@ -344,9 +346,18 @@ public void setProgress(double current, double max) {
reformatProgressText();
calculateTextBounds();

if (progressAnimator != null) {
progressAnimator.cancel();
stopProgressAnimation();

if (isAnimationEnabled) {
startProgressAnimation(oldCurrentProgress, finalAngle);
} else {
sweepAngle = (int) finalAngle;
invalidate();
}
}

private void startProgressAnimation(double oldCurrentProgress, final double finalAngle) {
final PropertyValuesHolder angleProperty = PropertyValuesHolder.ofInt(PROPERTY_ANGLE, sweepAngle, (int) finalAngle);

progressAnimator = ValueAnimator.ofObject(new TypeEvaluator<Double>() {
@Override
Expand All @@ -368,13 +379,19 @@ public void onAnimationUpdate(ValueAnimator animation) {
@Override
public void onAnimationCancel(Animator animation) {
sweepAngle = (int) finalAngle;

invalidate();
progressAnimator = null;
}
});
progressAnimator.start();
}

private void stopProgressAnimation() {
if (progressAnimator != null) {
progressAnimator.cancel();
}
}

private void reformatProgressText() {
progressText = progressTextAdapter.formatText(progressValue);
}
Expand Down Expand Up @@ -590,6 +607,16 @@ public OnProgressChangeListener getOnProgressChangeListener() {
return onProgressChangeListener;
}

public void setAnimationEnabled(boolean enableAnimation) {
isAnimationEnabled = enableAnimation;

if (!enableAnimation) stopProgressAnimation();
}

public boolean isAnimationEnabled() {
return isAnimationEnabled;
}

@IntDef({DIRECTION_CLOCKWISE, DIRECTION_COUNTERCLOCKWISE})
public @interface Direction {
}
Expand Down
10 changes: 6 additions & 4 deletions circularprogressindicator/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@
<attr name="startAngle" format="integer|reference" />

<attr name="direction" format="enum">
<enum name="clockwise" value="0"/>
<enum name="counterclockwise" value="1"/>
<enum name="clockwise" value="0" />
<enum name="counterclockwise" value="1" />
</attr>

<attr name="progressCap" format="enum">
<enum name="round" value="0"/>
<enum name="butt" value="1"/>
<enum name="round" value="0" />
<enum name="butt" value="1" />
</attr>

<attr name="enableProgressAnimation" format="boolean" />

</declare-styleable>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.widget.CompoundButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.Switch;

import antonkozyriatskyi.circularprogressindicator.CircularProgressIndicator;

Expand Down Expand Up @@ -88,6 +89,14 @@ public void onProgressChanged(double progress, double maxProgress) {
Log.d("PROGRESS", String.format("Current: %1$.0f, max: %2$.0f", progress, maxProgress));
}
});

Switch animationSwitch = findViewById(R.id.sw_enable_animation);
animationSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
circularProgress.setAnimationEnabled(isChecked);
}
});
}

@Override
Expand Down
13 changes: 10 additions & 3 deletions example/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,20 @@
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
android:layout_marginTop="16dp"
android:text="Progress cap" />

<RadioGroup
android:layout_width="match_parent"
android:id="@+id/rg_cap"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<RadioButton
android:id="@+id/rb_cap_round"
android:checked="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="Round" />

<RadioButton
Expand All @@ -131,6 +131,13 @@

</RadioGroup>

<Switch
android:id="@+id/sw_enable_animation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:text="Enable animation" />

<Button
android:id="@+id/btn_progress_color"
android:layout_width="match_parent"
Expand Down

0 comments on commit 7ead00a

Please sign in to comment.