Android自己定义(三)实现圆盘的百分比设置
近期一直在学习自己定义控件,昨天看到群里有人问怎样怎样实现圆盘样式的显示,学有所用,于是乎就有了这篇博客
先上图,一目了然
这里的显示颜色以及颜色块的大小你都能够自己设置
这里设置了三种颜色,相应三种颜色的三个角度
上代码:
<?xml version="1.0" encoding="utf-8"? >
<resources>
<declare-styleable name="CustomCircle">
<attr name="firstColor" format="color"/>
<attr name="secondColor" format="color"/>
<attr name="thirdColor" format="color"/>
<attr name="firstAngle" format="integer"/>
<attr name="secondAngle" format="integer"/>
<attr name="thirdAngle" format="integer"/>
</declare-styleable>
</resources>
以上都属于自己定义属性,当然自己定义了属性就要给它赋值
TypedArray mArray = context.obtainStyledAttributes(attrs,
R.styleable.CustomCircle, defStyleAttr, 0);
firstColor = mArray.getColor(R.styleable.CustomCircle_firstColor,
Color.BLUE);
secondColor = mArray.getColor(R.styleable.CustomCircle_secondColor,
Color.GREEN);
thirdColor = mArray.getColor(R.styleable.CustomCircle_thirdColor,
Color.RED);
firstAngle=mArray.getInt(R.styleable.CustomCircle_firstAngle, 90);
secondAngle=mArray.getInt(R.styleable.CustomCircle_secondAngle, 180);
thirdAngle=mArray.getInt(R.styleable.CustomCircle_thirdAngle, 120); mArray.recycle();
属性赋值结束后,当然就要開始最重要的部分了,绘图,也就是重写onDraw()方法
@Override
protected void onDraw(Canvas canvas) {
int center=getWidth()/2;
int radius=center/2;
mPaint.setColor(Color.GRAY);
mPaint.setStrokeWidth(center);
mPaint.setAntiAlias(true);
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(center, center, radius, mPaint);
mPaint.setColor(firstColor);
RectF rectF=new RectF(center-radius, center-radius, center+radius, center+radius);
canvas.drawArc(rectF, 0, firstAngle, false, mPaint);
mPaint.setColor(secondColor);
canvas.drawArc(rectF, firstAngle, secondAngle, false, mPaint);
mPaint.setColor(thirdColor);
canvas.drawArc(rectF, secondAngle, thirdAngle, false, mPaint);
}
我们继续,自己定义控件就这么定义结束了,怎样用呢?看过前面博客的人们应该知道吧!
<com.sdufe.thea.guo.view.CustomCircle
android:layout_width="300dp"
android:layout_height="300dp"
custom:firstColor="@android:color/holo_purple"
custom:secondColor="@android:color/holo_blue_bright"
custom:thirdColor="@android:color/holo_orange_light"
custom:firstAngle="60"
custom:secondAngle="180"
custom:thirdAngle="120"/>
这里须要注意的是custom,这就是你自己定义的属性了,前面要声明一下xmlns:custom="http://schemas.android.com/apk/res/你自己的包名"
差点儿相同就这样啦,就实现了你想要的功能,当你看不懂别人的代码逻辑时,你能够debug,这也是一个非常好地办法
代码下载地址:http://download.csdn.net/detail/elinavampire/8175771
Android自己定义(三)实现圆盘的百分比设置的更多相关文章
- Android测试(三)——burpsuite抓包设置
导出证书: 将证书导入模拟器中: 设置监听端口,透明代理(一定要设置这个): 进入adb shell,输入如下命令,即可抓包了: iptables -t nat -A OUTPUT -p tcp - ...
- Android UI--自定义ListView(实现下拉刷新+加载更多)
Android UI--自定义ListView(实现下拉刷新+加载更多) 关于实现ListView下拉刷新和加载更多的实现,我想网上一搜就一堆.不过我就没发现比较实用的,要不就是实现起来太复杂,要不就 ...
- 【转】android 电池(三):android电池系统
关键词:android电池系统电池系统架构 uevent power_supply驱动 平台信息: 内核:linux2.6/linux3.0系统:android/android4.0 平台:S5PV3 ...
- Android自己定义组件系列【7】——进阶实践(4)
上一篇<Android自己定义组件系列[6]--进阶实践(3)>中补充了关于Android中事件分发的过程知识.这一篇我们接着来分析任老师的<可下拉的PinnedHeaderExpa ...
- Android 自己定义View (二) 进阶
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/24300125 继续自己定义View之旅.前面已经介绍过一个自己定义View的基础 ...
- Android自己定义控件系列五:自己定义绚丽水波纹效果
尊重原创!转载请注明出处:http://blog.csdn.net/cyp331203/article/details/41114551 今天我们来利用Android自己定义控件实现一个比較有趣的效果 ...
- android Service Activity三种交互方式(付源码)(转)
android Service Activity三种交互方式(付源码) Android应用服务器OSBeanthread android Service Binder交互通信实例 最下边有源代码: ...
- Android For JNI(三)——C的指针,指针变量,指针常见错误,值传递,引用传递,返回多个值
Android For JNI(三)--C的指针,指针变量,指针常见错误,值传递,引用传递,返回多个值 C中比较难的这一块,大概就是指针了,所以大家还是多翻阅一下资料,当然,如果只是想了解一下,看本篇 ...
- Android JNI学习(三)——Java与Native相互调用
本系列文章如下: Android JNI(一)——NDK与JNI基础 Android JNI学习(二)——实战JNI之“hello world” Android JNI学习(三)——Java与Nati ...
随机推荐
- 微信支付http://www.cnblogs.com/True_to_me/p/3565039.html
公众号支付有2种支付方式: JS API 支付:是指用户打开图文消息或者扫描二维码,在微信内置浏览器打开网页进行的支付.商户网页前端通过使用微信提供的 JS API,调用微信支付模块.这种方式,适合需 ...
- inside a shard
fsync sync fsync/syncsync is a standard system call in the Unix operating system, which commits to d ...
- selenium+python自动化77-autoit文件上传【转载】
前言 关于非input文件上传,点上传按钮后,这个弹出的windows的控件了,已经跳出三界之外了,不属于selenium的管辖范围(selenium不是万能的,只能操作web上元素).autoit工 ...
- MyEclipse2015+Tomcat8.0+Maven3.3项目环境搭建
之前一直用自己的笔记本进行web项目的开发,实验室配了一台台式机,软件和环境都需要重新配置和安装.最近准备用SSM(Spring,SpringMVC,MyBatis)框架编写一个图书管理系统,主要使用 ...
- 在一个ros包下怎么使用另外一个自定义ros包里的message
假设自定义消息包my_message_package https://answers.ros.org/question/206257/catkin-use-ros-message-from-anoth ...
- 磁盘挂载MOUNT 445问题集
挂载磁盘mount -t cifs -o username="Administrator",password="123@qq" //192.168.100.4/ ...
- springBoot Feign Hystrix Dashboard
1.引入依赖 <!-- 引入关于 hystrix Dashboard的依赖 --> <dependency> <groupId>org.springframewor ...
- EF中使用Linq时First、FirstOrDefault、Single、SingleOrDefault几个方法的区别
在使用EntityFramework开发时,.NET的System.Linq.Enumerable类为我们提供了许多Linq方法. 给大家分享一下关于First.FirstOrDefault.Sing ...
- ubuntu下使用AutoKey实现ARK的自动跑/攻击/自动配种
脚本1: if not store.has_key("ark_run"): store.set_value("ark_run", 1) keyboard.pre ...
- UVA 11624 Fire!【两点BFS】
Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the m ...