多彩的进度栏

时间:2018-01-10 11:09:13

标签: android android-progressbar

我正在尝试使用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();

        }
    }
}

我不明白为什么。如果你有帮助,我会非常感激。

1 个答案:

答案 0 :(得分:1)

请使用此代码

<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类

<?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>