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,等以后有时间在好好整理一下吧~. 第 ...
随机推荐
- transform—切割轮播图
效果演示: 1.结构分析 第一步:在一个div里面有显示图片的ul标签(1个)和左右切换的a标签(2个): 第二步:ul标签中有5个li标签,li标签浮动,每个li标签的宽度占ul宽度的五分之一,高度 ...
- winform 集成 log4net
1.引入库log4net.dll 2.展开项目文件下的Properties文件夹,打开AssemblyInfo.cs并在AssemblyInfo.cs中添加一行:在AssemblyInfo.cs中添加 ...
- Java 知识点(一)
博主对 Java知识点的整理基于 c语言,整理内容为 Java的重点及与 c语言的差异点或编程通要知识点.水平有限,欢迎指正.(参考书籍<Java 核心技术 卷Ⅰ>) Java 的类名:名 ...
- Windows环境下spyder调用Arcpy
用python写代码还是喜欢spyder,所以在网上找了通过spyder调用arcpy的方法. 这篇帖子总结的方法非常详细,且通过本人实践,切实可行https://blog.csdn.net/qq_2 ...
- Python说文解字_main
1. main函数: 我们知道很多的编程语言都要写一个main函数,比如在C# 中Main函数还需要大写.很多人疑惑为什么要写这么一个Main函数.其实这就是好比我们在建了一排房子,你从哪个门都可以进 ...
- MyBatis学习——动态SQL
开发人员在使用JDBC框架或者其他类似的框架进行数据库开发时,通常都要根据需求去手动拼接SQL,这样非常麻烦,而myBatis提供了对SQL语句动态组装的功能,恰好解决了这一问题. 一,动态SQL中的 ...
- 67)vector的begin() end() 和 front() back()的区别 rbegin() rend()
1) ·············· 2)`````````v1.begin() 和v1.end() 是作为迭代器v1的 第一个位置 和 最后一个元素的下一个位置. `````````````v1. ...
- Constant expression required
switch 语句编译报错Constant expression required 问题:case语句后面必须是常量,使用final修饰 public static final int NotifyT ...
- UML-如何进行面向对象设计?
1.开发者如何设计对象? 1).直接编码 2).uml图,然后编码 3).uml图,不编码 绘图要轻量的 2.并行创建若干模型 如:5分钟画交互图,5分钟画类图.反复交替 3.选择什么样的UML CA ...
- UML-如何创建领域模型?
以当前迭代中所要设计的需求为界: 1.寻找概念类 2.将其绘制为类图 3.添加关联和属性