【自定义View】Android圆饼进度条

源码

自定义属性

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="ArcProgressView">
        <attr name="android:textSize" />
        <attr name="bgBorderWidth" format="dimension" />
        <attr name="defaultContentColor" format="color" />
        <attr name="bgBorderColor" format="color" />
        <attr name="progressColor" format="color" />
        <attr name="android:textColor" />
    </declare-styleable>
</resources>

View(kotlin)源码

kotlin 复制代码
package com.example.test

import android.content.Context
import android.content.res.Resources
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import kotlin.math.roundToInt

class ArcProgressView : View {
    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        val obtainStyledAttributes =
            context?.obtainStyledAttributes(attrs, R.styleable.ArcProgressView)

        obtainStyledAttributes?.let {
            textSize = it.getDimensionPixelSize(
                R.styleable.ArcProgressView_android_textSize,
                textSize
            )

            strokeWidth = it.getDimensionPixelSize(
                R.styleable.ArcProgressView_bgBorderWidth,
                strokeWidth.roundToInt()
            ).toFloat()
            defaultContentColor = it.getColor(
                R.styleable.ArcProgressView_defaultContentColor,
                defaultContentColor
            )
            bgBorderColor =
                it.getColor(R.styleable.ArcProgressView_bgBorderColor, bgBorderColor)
            progressColor =
                it.getColor(R.styleable.ArcProgressView_progressColor, progressColor)
            textColor =
                it.getColor(R.styleable.ArcProgressView_android_textColor, textColor)

        }

        obtainStyledAttributes?.recycle()
    }

    val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    var strokeWidth = dpToPx(0.5f).toFloat();
    var textSize = spToPx(10f).roundToInt()
    var defaultContentColor = Color.parseColor("#FFF7ED")
    var bgBorderColor = Color.parseColor("#FAD29D")
    var progressColor = Color.parseColor("#FAD29D")
    var textColor = Color.parseColor("#FA940F")

    private var progress: Int = 0
        set(value) {
            field = value
            invalidate()
        }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        drawBackground(canvas)
        drawProgress(canvas)
    }

    private fun dpToPx(dp: Float): Int {
        return (Resources.getSystem().displayMetrics.density * dp + 0.5f).roundToInt()
    }

    private fun spToPx(sp: Float): Float {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_SP,
            sp,
            Resources.getSystem().displayMetrics
        )
    }

    private fun drawBackground(canvas: Canvas) {

        paint.strokeWidth = strokeWidth
        paint.style = Paint.Style.FILL
        paint.color = defaultContentColor
        canvas.drawOval(
            0f + strokeWidth / 2,
            0f + strokeWidth / 2,
            width.toFloat() - strokeWidth / 2,
            height.toFloat() - strokeWidth / 2,
            paint
        )
        paint.style = Paint.Style.STROKE
        paint.color = bgBorderColor
        canvas.drawOval(
            0f + strokeWidth / 2,
            0f + strokeWidth / 2,
            width.toFloat() - strokeWidth / 2,
            height.toFloat() - strokeWidth / 2,
            paint
        )

    }

    val textBound = Rect()

    private fun drawProgress(canvas: Canvas) {
        paint.style = Paint.Style.FILL
        paint.color = progressColor

        paint.textSize = textSize.toFloat()

        val progressStr = "$progress%"
        paint.getTextBounds(progressStr, 0, progressStr.length, textBound)
        val halfTextWidth = textBound.width() / 2f
        val halfTextHeight = textBound.height() / 2f
        val progressAngle = progress * 3.6f

        val halfWidth = width.toFloat() / 2f
        val halfHeight = height.toFloat() / 2f

        canvas.drawArc(
            0f,
            0f,
            width.toFloat(),
            height.toFloat(),
            270f,
            progressAngle,
            true,
            paint
        )

        paint.isFakeBoldText = true
        paint.color = textColor
        paint.textAlign = Paint.Align.LEFT
        canvas.drawText(
            progressStr,
            halfWidth - halfTextWidth,
            halfHeight - halfTextHeight + textBound.height(),
            paint
        )
    }

    fun updateProgress(progress: Int) {
        this.progress = progress
    }


}

Activity布局

xml 复制代码
  <com.example.test.ArcProgressView
        android:layout_margin="12dp"
        android:id="@+id/progress"
        android:textSize="24sp"
        android:textColor="@color/black"
        android:layout_width="100dp"
        android:layout_height="100dp"/>

Activity更新进度

kotlin 复制代码
progress ++
runOnUiThread({
    progressView.updateProgress(progress)
})

效果图

相关推荐
网络研究院5 分钟前
Android 安卓内存安全漏洞数量大幅下降的原因
android·安全·编程·安卓·内存·漏洞·技术
凉亭下12 分钟前
android navigation 用法详细使用
android
小比卡丘3 小时前
C语言进阶版第17课—自定义类型:联合和枚举
android·java·c语言
前行的小黑炭3 小时前
一篇搞定Android 实现扫码支付:如何对接海外的第三方支付;项目中的真实经验分享;如何高效对接,高效开发
android
落落落sss5 小时前
MybatisPlus
android·java·开发语言·spring·tomcat·rabbitmq·mybatis
代码敲上天.5 小时前
数据库语句优化
android·数据库·adb
GEEKVIP8 小时前
手机使用技巧:8 个 Android 锁屏移除工具 [解锁 Android]
android·macos·ios·智能手机·电脑·手机·iphone
model200510 小时前
android + tflite 分类APP开发-2
android·分类·tflite
彭于晏68910 小时前
Android广播
android·java·开发语言
与衫11 小时前
掌握嵌套子查询:复杂 SQL 中 * 列的准确表列关系
android·javascript·sql