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"/>
android 圆环进度view的更多相关文章
- Android 带进度的圆形进度条
最近项目有个需求,做带进度从下到上的圆形进度条. 网上查了一下资料,发现这篇博客写得不错http://blog.csdn.net/xiaanming/article/details/10298163 ...
- Android多种进度条使用详解
在这里,总结一下loading进度条的使用简单总结一下. 一.说起进度条,必须说说条形进度条,经常都会使用到嘛,特别是下载文件进度等等,还有像腾讯QQ安装进度条一样,有个进度总给人良好的用户体验. 先 ...
- Android loading进度条使用简单总结
在这里,总结一下loading进度条的使用简单总结一下. 一.说起进度条,必须说说条形进度条,经常都会使用到嘛,特别是下载文件进度等等,还有像腾讯QQ安装进度条一样,有个进度总给人良好的用户体验. 先 ...
- android的进度条使用
android的进度条 1.实现的效果 2.布局代码 先写一个my_browser.xml文件 存放WebView <?xml version="1.0" encoding= ...
- android多线程进度条
多线程实现更新android进度条. 实例教程,详细信息我已经注释 android多线程进度条 01package com.shougao.hello; 02 03import android ...
- Android 自己定义View学习(2)
上一篇学习了基本使用方法,今天学一下略微复杂一点的.先看一下效果图 为了完毕上面的效果还是要用到上一期开头的四步 1,属性应该要有颜色,要有速度 <?xml version="1.0& ...
- Android 判断一个 View 是否可见 getLocalVisibleRect(rect) 与 getGlobalVisibleRect(rect)
Android 判断一个 View 是否可见 getLocalVisibleRect(rect) 与 getGlobalVisibleRect(rect) [TOC] 这两个方法的区别 View.ge ...
- 1.Android 视图及View绘制分析笔记之setContentView
自从1983年第一台图形用户界面的个人电脑问世以来,几乎所有的PC操作系统都支持可视化操作,Android也不例外.对于所有Android Developer来说,我们接触最多的控件就是View.通常 ...
- 两种CSS3圆环进度条详解
晚上睡觉之前,我抽了1个多小时,研究了一下圆环进度条,结合从网上查阅的资料,我终于掌握了两种圆环的生成方法. 这次的效果就是单纯的CSS3效果,也没有写具体的JS,等以后有时间在好好整理一下吧~. 第 ...
随机推荐
- sql注入入门--基本命令
本文转载自http://blog.csdn.net/zgyulongfei/article/details/41017493 本文仅献给想学习渗透测试的sqlmap小白,大牛请绕过. > > ...
- 寒假day08
今天爬了部分与人才动态相关的数据,还刷了剑指offer的部分算法题
- pip速度慢解决办法
pip速度慢解决办法 sudo pip3 config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple 注意加不加sudo是 ...
- 视觉slam十四讲个人理解(ch7视觉里程计1)
参考博文::https://blog.csdn.net/david_han008/article/details/53560736 https://blog.csdn.net/n66040927/ar ...
- 18 11 05 继续补齐对python中的class不熟悉的地方 和 pygame 精灵
---恢复内容开始--- class game : #历史最高分----- 是定义类的属性 top_score =0 def __init__(self, player_name) : #是定义的实例 ...
- vue title悬停
title 设置name="peo" title="" v-on:mouseenter="peoAndCarHover(item.signStatus ...
- Java 语句while、do while、for循环、嵌套、for与while的区别、break continue(3)
for循环语句,双从for嵌套: /* for(初始化表达式:循环条件表达式:循环后的操作表达式) { 执行语句: } */ /*1,变量有自己的作用域.对于for来讲:如果将用于控制循环的增量定义在 ...
- sp__helptable, sp__help******* help 存储过程
from: https://sqldbaknowledgeshare.wordpress.com/2014/11/27/general-help-procedures/ GENERAL HELP P ...
- CMake变量(提供信息的变量)
目录 CMAKE_VERSION CMAKE_MAJOR_VERSION CMAKE_MINOR_VERSION CMAKE_PATCH_VERSION CMAKE_TWEAK_VERSION CMA ...
- [kuangbin 带你飞] DP专题——HDU - 1024
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...