在 Android 中制作自定义进度条
Make a custom progress bar in Android
我需要制作一个如下所示的自定义进度条:
我希望获得有关执行此操作的最佳方法的一些建议。
谢谢!
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 了解所有可用的自定义设置。
@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);
}
我需要制作一个如下所示的自定义进度条:
我希望获得有关执行此操作的最佳方法的一些建议。 谢谢!
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 了解所有可用的自定义设置。
@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);
}