当前位置: 首页 > news >正文

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

源码

自定义属性

<?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)源码

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布局

  <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更新进度

progress ++
runOnUiThread({
    progressView.updateProgress(progress)
})

效果图

在这里插入图片描述

相关文章:

  • 循环嵌套语句的实际应用(2)
  • C#中的实体属性详解与示例
  • 基于Python的北京天气数据可视化分析
  • Kafka之Consumer原理
  • 软光敏的程序实现
  • 顶顶通呼叫中心中间件(mod_cti基于FreeSWITCH)-同振与顺振的用法
  • [office] 如何才能用EXCEL打开dat文件- #微信#学习方法
  • 植物大战僵尸杂交版2.0.88最新版安装包
  • 【C语言】学生管理系统:完整模拟与实现
  • C++ CMake FetchContent用法介绍
  • BGP基础实验
  • 三维可视化技术在厂区人员、车辆定位中的设计与应用效益
  • 正则表达式三
  • sqlilabs靶场安装
  • python + selenium 实现浏览器的基本操作
  • 上传图片并显示#Vue3#后端接口数据
  • Datetime,一个 Python 的时间掌控者
  • 乙二醇水溶液物性参数
  • 将SU模型导入ARCGIS,并获取高度信息,多面体转SHP文件(ARCMAP)
  • Spring boot 集成mybatis-plus
  • 金价大反攻,国内金饰价格涨回千元,能否重返巅峰?
  • 马上评|从一个细节看今年五一档电影
  • 多省份晒出“五一”旅游“成绩单”:北京游客接待量、旅游消费创历史新高
  • 科普|肩周炎的自限性,意味着不治也能自己好?
  • 市场监管总局通报民用“三表”专项检查结果
  • 恒瑞医药通过港交所上市聆讯,最快或5月挂牌上市