android 圆环进度view

新建RoundProgressBar

复制代码
class RoundProgressBar : View {

    private val paint = Paint()

    var max = 100      //最大进度
    var progress = 0   //当前进度   取值范围 0-max
        set(value) {
            field = value
            invalidate()//调用该方法后  onDraw会执行
        }
    var roundColor = Color.GRAY //圆环颜色
    var roundProgressColor = Color.YELLOW  //圆环进度的颜色
    var roundWidth:Float = 16f    //圆环的宽度(粗细)


    constructor(context: Context) : super(context) {
        init(null, 0)
    }

    constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet) {
        init(attributeSet, 0)
    }

    constructor(context: Context, attributeSet: AttributeSet, defStyle: Int) : super(context, attributeSet, defStyle) {
        init(attributeSet, defStyle)
    }

    private fun init(attributeSet: AttributeSet?, defStyle: Int) {
        val typedArray = context.obtainStyledAttributes(attributeSet, R.styleable.RoundProgressBar, defStyle, 0)
        max = typedArray.getInt(R.styleable.RoundProgressBar_max, 100)
        progress = typedArray.getInt(R.styleable.RoundProgressBar_progress, 30)
        progress = max(0,min(progress,100))
        roundColor = typedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.GRAY)
        roundProgressColor = typedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor,Color.YELLOW)
        roundWidth = typedArray.getDimension(R.styleable.RoundProgressBar_roundWidth,16f)
        typedArray.recycle()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        // 获取圆心x坐标  (x=y)
        val c = (width / 2).toFloat()
        // 圆环的半径
        val radius = c - roundWidth / 2

        paint.color = roundColor  //设置颜色
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = roundWidth //设置宽度
        paint.isAntiAlias = true   //消除锯齿

        canvas?.drawCircle(c,c,radius,paint)

        paint.color = roundProgressColor
        paint.style = Paint.Style.STROKE
        paint.strokeWidth = roundWidth
        paint.isAntiAlias = true

        val rectF = RectF(c - radius,c - radius,c + radius,c + radius)
        canvas?.drawArc(rectF,(-90).toFloat(),(progress * 360 / max).toFloat(),false,paint)
        //invalidate()
    }


}
复制代码
context.obtainStyledAttributes 用来获取布局文件的所有属性
canvas?.drawCircle(c,c,radius,paint) 根据画笔类型画圆环或者画圆
Paint.Style.STROKE 画圆环  Paint.Style.FILL 画圆  Paint.Style.FILL_AND_STROKE 画圆 直径包括strokeWidth宽度
canvas?.drawArc(矩形对象,起始角度,画的角度,是否包含圆心,paint) 想要包含圆心的 画笔样式不能是Paint.Style.STROKE 才有效果 

 

在res.values下创建attrs.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="RoundProgressBar">
        <attr name="max" format="integer" />
        <attr name="progress" format="integer" />
        <attr name="roundColor" format="color" />
        <attr name="roundProgressColor" format="color" />
        <attr name="roundWidth" format="dimension" />
    </declare-styleable>

</resources>
复制代码

后面就可以使用了

<com.chao.myvideo.view.RoundProgressBar
        android:layout_width="50dp"
        android:layout_height="50dp"
        app:progress="0"
        app:max="100"
        android:layout_gravity="center"/>

 

posted @   荣超  阅读(444)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示