Reputation: 74
I am trying to make a colored progress bar using the project on the link. I want it to look like this picture
But once the animation finishes, the entire progress bar is redrawn with the same color like this picture Here's where I have trouble.
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
final BarPart barPart = mBarParts.get(mCurrentBarPartIndex);
if (mAngle < barPart.mEndPoint) {
if (mCurrentBarPartIndex > 0) {
for (int i = 0; i < mCurrentBarPartIndex; i++) {
final BarPart prevBarPart = mBarParts.get(i);
canvas.drawArc(mRect, prevBarPart.mStartPoint, mBreakPointAngle, false, prevBarPart.mPaint);
}
}
canvas.drawArc(mRect, barPart.mStartPoint, mAngle - barPart.mLimit, false, barPart.mPaint);
} else {
if (mCurrentBarPartIndex < mBarParts.size() - 1) {
++mCurrentBarPartIndex;
draw(canvas);
} else {
// last time whole progress redrawed.
canvas.drawArc(mRect, mStartAngle, 360, false, barPart.mPaint);
finishDrawing();
}
}
}
I dont understand why. If you help, I will be really appreciative.
Upvotes: 0
Views: 92
Reputation: 511
Please use this code
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_gravity="center_horizontal"
android:background="@drawable/progressbar_back"
android:elevation="8dp"
android:max="28800"
android:progress="50000"
android:progressDrawable="@drawable/progressbar_circular"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Drawable Classes progressbar_back class
<?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="9.1dp"
android:useLevel="false"
android:tint="#D7DEE3">
</shape>
progressbar_circular class
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="90"
android:toDegrees="90">
<shape
android:innerRadiusRatio="2.5"
android:shape="ring"
android:thickness="9.1dp"
android:useLevel="true">
<gradient
android:angle="180"
android:centerColor="#37a825"
android:endColor="#e803dafc"
android:startColor="#ebffcb00"
android:type="sweep"
android:useLevel="false" />
</shape>
Upvotes: 1