角不适用于android drawable中的环形

Posted

技术标签:

【中文标题】角不适用于android drawable中的环形【英文标题】:Corners does not work with ring shape in android drawable 【发布时间】:2019-06-23 15:57:20 【问题描述】:

我正在尝试实现一个简单的圆形进度条。它工作正常,但我似乎无法使进度圈的角变圆。

我正在尝试实现这样的目标:

我的代码:

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape
            android:thickness="3dp"
            android:shape="ring"
            android:type="sweep"
            android:useLevel="false">

            <solid android:color="@color/light_grey"/>
        </shape>
    </item>

    <item android:id="@android:id/progress">
        <rotate
            android:fromDegrees="270"
            android:toDegrees="270">

            <shape
                android:thickness="3dp"
                android:shape="ring"
                android:type="sweep"
                android:useLevel="true">

                <solid android:color="@color/green"/>
                <corners android:radius="8dp"/> // --> this has no effect whatsoever
            </shape>
        </rotate>
    </item>
</layer-list>

【问题讨论】:

你能分享一下你的预期输出吗 @NileshRathod 我更新了我的帖子,举了一个我想要实现的例子。谢谢 ***.com/questions/36639660/… ***.com/questions/14688117/… 不幸的是,这一切都没有帮助。那里的解决方案要么引用外部 3 方库,要么在 Kotlin 中提供代码,要么根本不起作用。 【参考方案1】:

有点晚了,但您可以像这样创建自定义循环进度视图

class CircularProgressView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) 

    private lateinit var progressPaint: Paint
    private lateinit var backgroundPaint: Paint
    var max = MAX_PROGRESS
    var progress = START_PROGRESS
        set(value) 
            angle = calculateAngle(value)
            invalidate()
        

    private val rect = RectF()
    private var diameter = 0F
    private var angle = 0F

    init 
        attrs?.let 
            val typedArray = context.obtainStyledAttributes(it, R.styleable.CircularProgressView, 0, 0)
            val stroke = typedArray.getDimension(R.styleable.CircularProgressView_stroke, context.resources.getDimension(R.dimen._1dp))
            val backgroundColor = typedArray.getColor(R.styleable.CircularProgressView_backgroundColor, ContextCompat.getColor(context, R.color.gray))
            val progressColor = typedArray.getColor(R.styleable.CircularProgressView_progressColor, ContextCompat.getColor(context, R.color.black))
            progressPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply 
                style = Paint.Style.STROKE
                strokeWidth = stroke
                color = progressColor
                strokeCap = Paint.Cap.ROUND
            
            backgroundPaint = Paint(Paint.ANTI_ALIAS_FLAG).apply 
                style = Paint.Style.STROKE
                strokeWidth = stroke
                color = backgroundColor
            
            max = typedArray.getFloat(R.styleable.CircularProgressView_max, MAX_PROGRESS)
            progress = typedArray.getFloat(R.styleable.CircularProgressView_progress, START_PROGRESS)
            typedArray.recycle()
        
    

    override fun onDraw(canvas: Canvas) 
        drawCircle(MAX_ANGLE, canvas, backgroundPaint)
        drawCircle(angle, canvas, progressPaint)
    

    override fun onSizeChanged(width: Int, height: Int, oldWidth: Int, oldHeight: Int) 
        diameter = width.coerceAtMost(height).toFloat()
        updateRect()
    

    fun setProgressColor(@ColorRes color: Int) 
        progressPaint.color = ContextCompat.getColor(context, color)
        invalidate()
    

    fun setProgressBackgroundColor(@ColorRes color: Int) 
        backgroundPaint.color = ContextCompat.getColor(context, color)
        invalidate()
    

    private fun updateRect() 
        val strokeWidth = backgroundPaint.strokeWidth
        rect.set(strokeWidth, strokeWidth, diameter - strokeWidth, diameter - strokeWidth)
    

    private fun drawCircle(angle: Float, canvas: Canvas, paint: Paint) 
        canvas.drawArc(rect, START_ANGLE, angle, false, paint)
    

    private fun calculateAngle(progress: Float) = MAX_ANGLE / max * progress

    companion object 
        private const val START_ANGLE = -90F
        private const val MAX_ANGLE = 360F
        private const val MAX_PROGRESS = 100F
        private const val START_PROGRESS = 0F
    

属性

<declare-styleable name="CircularProgressView">
        <attr name="progress" format="float" />
        <attr name="max" format="float" />
        <attr name="stroke" format="dimension" />
        <attr name="backgroundColor" format="color" />
        <attr name="progressColor" format="color" />
</declare-styleable>

【讨论】:

以上是关于角不适用于android drawable中的环形的主要内容,如果未能解决你的问题,请参考以下文章

如何在Android drawable中为环形制作圆角

将角半径应用于 Storyboard 内的特定 UIView 角不适用于所有角

ANDROID布局实现圆角边框

Android操作系统中默认的loading动画怎么调用

无法解析属性“src”中的可绘制对象

为啥 Room 实体不适用于 Android 中的不可变属性