在Android中制作自定义进度栏

时间:2018-07-03 08:09:28

标签: android android-layout android-progressbar

我需要制作一个如下所示的自定义进度栏:enter image description here

我很乐意为您提供最好的方法建议。 谢谢!

2 个答案:

答案 0 :(得分:2)

https://github.com/lzyzsd/CircleProgress

已经有许多提供此类进度条的库。检查以上一项。

   allprojects {
        repositories {
            ...
            maven { url "https://jitpack.io" }
        }
    }

应用级gradle。

dependencies {
    compile 'com.github.lzyzsd:circleprogress:1.2.1'
}

在您的布局中

 <com.github.lzyzsd.circleprogress.DonutProgress
        android:layout_marginLeft="50dp"
        android:id="@+id/donut_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        custom:donut_progress="30"/>

有关所有可用的自定义,请参见https://github.com/lzyzsd/CircleProgress

答案 1 :(得分:1)

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int x = canvas.getWidth();
    int y = canvas.getHeight();

    float left      = x/2 ;
    float top       = y/2 ;
    float right     = x/2 ;
    float bottom    = y/2 ;

    mRectF.left     = left;
    mRectF.top      = top;
    mRectF.right    = right;
    mRectF.bottom   = bottom;

    mPaint.setStrokeWidth(strokeWidth);
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setStyle(Paint.Style.FILL);
    mPaint.setColor(Color.WHITE);



    mPaint.setShader(null);
    canvas.drawCircle(x/2,y/2, strokeWidth* 1.5f,mPaint);
    mPaint.setStyle(Paint.Style.STROKE);


    mPaint.setShader(createGradient());
    canvas.drawArc(mRectF,0,360,false, mPaint);


    Shader mGradient = createGradient();
    mMatrix.setRotate(-90f, x / 2 , y / 2 );
    mGradient.setLocalMatrix(mMatrix);
    mPaint.setShader(mGradient);
    canvas.drawArc(mRectF,ANGLE_START,currentAngle,false, mPaint);

}