Skip to content

Instantly share code, notes, and snippets.

@iamnaran
Last active May 21, 2018 10:17
Show Gist options
  • Save iamnaran/078004e2b4c818dcb981852617f0e9e6 to your computer and use it in GitHub Desktop.
Save iamnaran/078004e2b4c818dcb981852617f0e9e6 to your computer and use it in GitHub Desktop.
Custom Circular Progress Bar Android
<ProgressBar
android:id="@+id/progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="110dp"
android:layout_height="110dp"
android:background="@drawable/circle_shape"
android:indeterminate="false"
android:max="30"
android:progress="15"
android:progressDrawable="@drawable/circular_progress_bar"
android:layout_centerVertical="true"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
/>
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:innerRadiusRatio="2.5"
android:thickness="3dp"
android:useLevel="false">
<solid android:color="#CCC" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="270"
android:toDegrees="270">
<shape
android:innerRadiusRatio="2.5"
android:shape="ring"
android:thickness="3dp"
android:useLevel="true"><!-- this line fixes the issue for lollipop api 21 -->
<gradient
android:angle="0"
android:endColor="#42A530"
android:startColor="#42A530"
android:type="linear"
android:useLevel="false" />
</shape>
</rotate>
private ProgressBarAnimation progressBarAnimation;
progressBar = (ProgressBar) findViewById(R.id.progress_bar);
progressBarAnimation = new ProgressBarAnimation(vhHeaderTwo.progressBar, 0, Integer.parseInt(studentAttendance.getTotal().getPresentDays()));
progressBar.setMax(Integer.parseInt(studentAttendance.getTotal().getOperatingDays()));
progressBarAnimation.setDuration(800);
progressBarAnimation.setInterpolator(new AnticipateInterpolator());
progressBar.startAnimation(progressBarAnimation);
progressBar.getProgressDrawable().setColorFilter(Color.parseColor(primaryColor), PorterDuff.Mode.SRC_IN);
public class ProgressBarAnimation extends Animation {
private ProgressBar progressBar;
private float from;
private float to;
public ProgressBarAnimation(ProgressBar progressBar, float from, float to) {
super();
this.progressBar = progressBar;
this.from = from;
this.to = to;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
float value = from + (to - from) * interpolatedTime;
progressBar.setProgress((int) value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment