多彩进度条
colorful progress bar
我正在尝试使用 link. 上的项目制作彩色进度条
我希望它看起来像这样 picture
但是一旦动画结束,整个进度条就会像这样用相同的颜色重新绘制picture这就是我遇到麻烦的地方。
@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();
}
}
}
我不明白为什么。如果您能提供帮助,我将不胜感激。
请使用此代码
<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" />
可绘制类
progressbar_backclass
<?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>
我正在尝试使用 link. 上的项目制作彩色进度条 我希望它看起来像这样 picture
但是一旦动画结束,整个进度条就会像这样用相同的颜色重新绘制picture这就是我遇到麻烦的地方。
@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();
}
}
}
我不明白为什么。如果您能提供帮助,我将不胜感激。
请使用此代码
<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" />
可绘制类 progressbar_backclass
<?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>