如何在Android drawable中为环形制作圆角 [英] How to make round corners for a ring shape in Android drawable
问题描述
我有一个自定义进度条,如下所示:
I have a custom progress bar that looks like this:
这是我用来创建它的.xml代码:
Here's the .xml code I've used to create it:
background_drawable.xml
background_drawable.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="@dimen/progress_bar_radial_inner_radius"
android:thickness="@dimen/progress_bar_radial_thickness"
android:shape="ring"
android:useLevel="false" >
<solid android:color="@color/main_color_alpha"/>
</shape>
progress_drawable.xml
progress_drawable.xml
<?xml version="1.0" encoding="UTF-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="270"
android:toDegrees="270">
<shape
android:innerRadius="@dimen/progress_bar_radial_inner_radius"
android:thickness="@dimen/progress_bar_radial_thickness"
android:shape="ring" >
<solid android:color="@color/main_color"/>
</shape>
</rotate>
我想要得到的是我用来显示进度的圆环的圆角.看起来像这样:
What I want to get is a round corners for the ring that I use to show progress. Something that would look like this:
有人对如何实现这一目标有任何想法吗?
Does someone has any idea on how this can be achieved?
推荐答案
好,我发现要做的最简单的方法是在画布上绘制进度弧,而不是使用progress_drawable.xml.这是我的代码,以防有人遇到类似的问题.
Ok, the easiest way I've found to do what I want is to draw progress arc on canvas instead of using progress_drawable.xml. Here's my code in case someone has similar issue.
class RadialProgressBar : ProgressBar {
private val thickness = 28f
private val halfThickness = thickness / 2
private val startAngle = 270f
private var boundsF: RectF? = null
private lateinit var paint: Paint
constructor(context: Context?) : super(context) {
init()
}
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
init()
}
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
init()
}
private fun init() {
paint = Paint()
paint.isAntiAlias = true
paint.style = Paint.Style.STROKE
paint.strokeWidth = thickness
paint.strokeCap = Paint.Cap.ROUND
paint.color = ContextCompat.getColor(context, R.color.main_color)
progressDrawable = null
}
override fun draw(canvas: Canvas?) {
super.draw(canvas)
if (boundsF == null) {
boundsF = RectF(background.bounds)
boundsF?.inset(halfThickness, halfThickness)
}
canvas?.drawArc(boundsF, startAngle, progress * 3.60f, false, paint)
}
}
这篇关于如何在Android drawable中为环形制作圆角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!