有没有办法在android xml中创建半月形的多个圆圈

vkc1a9a2  于 2023-02-27  发布在  Android
关注(0)|答案(1)|浏览(111)

在上面的图像中,我想创建三个椭圆一个在另一个里面,并在上面的一个半圆到右端的XML Android,所以如何实现这一点,我已经在网上搜索,与画布的帮助下,我们可以这样做,但我不知道如何做,请帮助我,因为这是非常困难的设计给我在Android中。
我试过通过改变它的高度和宽度使用,但没有得到接近什么是在图像。

rm5edbpk

rm5edbpk1#

这是一个简单的自定义视图,将给予你这个形状,你需要完成它,并添加更多的定制方法,例如颜色,进度,边距...等

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View

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

    private val dividerProgressPaint: Paint = Paint()
    private val currentProgressPaint: Paint = Paint()
    private val remainProgressPaint: Paint = Paint()
    private val innerProgressPaint: Paint = Paint()
    private var outerRectangle: RectF? = null
    private var innerRectangle: RectF? = null
    private var margin: Float

    private var progressDivider = .02f
    private var progress: Float = .2f
    private var remainProgress = 1 - progress - progressDivider

    init {
        dividerProgressPaint.color = Color.WHITE
        dividerProgressPaint.style = Paint.Style.STROKE
        dividerProgressPaint.strokeWidth = 5f

        remainProgressPaint.color = Color.GREEN
        remainProgressPaint.style = Paint.Style.STROKE
        remainProgressPaint.strokeWidth = 5f

        currentProgressPaint.color = Color.RED
        currentProgressPaint.style = Paint.Style.STROKE
        currentProgressPaint.strokeWidth = 5f

        innerProgressPaint.color = Color.WHITE
        innerProgressPaint.style = Paint.Style.STROKE
        innerProgressPaint.strokeWidth = 5f

        margin = 50f
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        if (outerRectangle == null) {
            outerRectangle = RectF(0f + margin, 0f + margin, width.toFloat() - margin, width.toFloat() - margin)
        }

        if (innerRectangle == null) {
            innerRectangle = RectF(100f, 100f, width.toFloat() - 100f , width.toFloat() - 100f)
        }

        val remainEndAngel = 180 + remainProgress * 180
        canvas.drawArc(outerRectangle!!, 180f, remainProgress * 180, false, remainProgressPaint)

        val dividerEndAngel = remainEndAngel + progressDivider * 180f
        canvas.drawArc(outerRectangle!!, remainEndAngel, progressDivider * 180f, false, dividerProgressPaint)

        canvas.drawArc(outerRectangle!!,dividerEndAngel, progress * 180f, false, currentProgressPaint)

        canvas.drawArc(innerRectangle!!, 180f, 180f, false, innerProgressPaint)
    }

未进行任何自定义的视图将如下所示

相关问题