Android自定义view之圆形进度条

本节介绍自定义view-圆形进度条
思路:
根据前面介绍的自定义view内容可拓展得之;
1:新建类继承自View
2:添加自定义view属性
3:重写onDraw(Canvas canvas)
4:实现功能
下面上代码

1.自定义view代码:

public class CustomView extends View {//背景圆环颜色private int circleColor;//进度条颜色&字体颜色(为了美观,所以设计字体颜色和进度条颜色值一致)private int secondCircleColor;//进度条&背景圆环宽度private float stroke_width;//进度值private float progress;//总进度值,默认为100private float totalProgress;//字体大小private float textSize;//填充模式private int style_type;public CustomView(Context context) {super(context);}public CustomView(Context context, AttributeSet attrs) {super(context, attrs);TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.CustomView);circleColor=array.getColor(R.styleable.CustomView_circleColor, Color.BLACK);secondCircleColor=array.getColor(R.styleable.CustomView_secondCircleColor, Color.RED);stroke_width=array.getDimension(R.styleable.CustomView_stroke_width, 2);progress=array.getFloat(R.styleable.CustomView_progress, 0);totalProgress=array.getFloat(R.styleable.CustomView_totalProgress, 100);textSize=array.getDimension(R.styleable.CustomView_textSize, 16);style_type=array.getInt(R.styleable.CustomView_style_Type, 0);}public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}public void setCircleColor(int color){circleColor=color;}public int getCircleColor(){return circleColor;}public void setSecondCircleColor(int color){secondCircleColor=color;}public int getSecondColor(){return secondCircleColor;}public void setStrokeWidth(float width){stroke_width=width;}public float getStrokeWidth(){return stroke_width;}public void setProgress(float progress){this.progress=progress;postInvalidate();//刷新界面}public float getProgress(){return this.progress;}public void setTotalProgress(float totalProgress){this.totalProgress=totalProgress;}public float getTotalProgress(){return this.totalProgress;}public void setTextSize(float textSize){this.textSize=textSize;}public float getTextSize(){return this.textSize;}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);//第一进度圆final Paint paint_background=new Paint();paint_background.setAntiAlias(true);paint_background.setStrokeWidth(stroke_width);paint_background.setStyle(Style.STROKE);paint_background.setColor(circleColor);//第二进度圆final Paint paint_progress=new Paint();paint_progress.setAntiAlias(true);paint_progress.setStrokeWidth(stroke_width);if(style_type==0){paint_progress.setStyle(Style.STROKE);}else if(style_type==1){paint_progress.setStyle(Style.FILL_AND_STROKE);}paint_progress.setColor(secondCircleColor);//画textfinal Paint paint_text=new Paint();paint_text.setAntiAlias(true);if(style_type==0){paint_text.setColor(secondCircleColor);}else if(style_type==1){paint_text.setColor(circleColor);}paint_text.setTextSize(textSize);paint_text.setTextAlign(Align.CENTER);if(getWidth()!=getHeight()){throw new IllegalArgumentException("高度和宽度必须相等");//控制宽度和高度}else{RectF circle_background=new RectF();circle_background.left=getLeft()+stroke_width;circle_background.right=getRight()-stroke_width;circle_background.top=getTop()+stroke_width;circle_background.bottom=getBottom()-stroke_width;canvas.drawArc(circle_background, -90, 360, false, paint_background);RectF circle_progress=new RectF();circle_progress.left=getLeft()+stroke_width;circle_progress.right=getRight()-stroke_width;circle_progress.top=getTop()+stroke_width;circle_progress.bottom=getBottom()-stroke_width;if(progress>totalProgress){throw new IllegalArgumentException("当前进度值不能大于总进度值");}else{if(style_type==0){canvas.drawArc(circle_progress, -90, progress/totalProgress*360, false, paint_progress);}else if(style_type==1){canvas.drawArc(circle_progress, -90, progress/totalProgress*360, true, paint_progress);}}canvas.drawText((int)progress+"/"+(int)totalProgress, getLeft()+getWidth()/2, getTop()+getHeight()/2+textSize/4, paint_text);}}}

2:attr属性

<?xml version="1.0" encoding="utf-8"?>
<resources><!--declare-styleable:声明样式类型;attr name=""声明属性名;format="属性的类型"  --><declare-styleable name="CustomEditText"><attr name="lineColor" format="color" /><attr name="lineHeight" format="dimension"/></declare-styleable><declare-styleable name="CustomView"><attr name="stroke_width" format="dimension"/><attr name="circleColor" format="color"/><attr name="secondCircleColor" format="color"/><attr name="progress" format="float"/><attr name="totalProgress" format="float"/><attr name="textSize" format="dimension"/><attr name="style_Type"><enum name="stroke" value="0"/><enum name="stroke_and_fill" value="1"/></attr></declare-styleable></resources>

3:xml布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="wrap_content"android:layout_height="wrap_content" ><com.anqiansong.views.CustomViewxmlns:circle="http://schemas.android.com/apk/res/com.anqiansong.androidcustomview"android:id="@+id/customview"android:layout_width="50dp"android:layout_height="50dp"android:layout_centerInParent="true"circle:circleColor="#000000"circle:secondCircleColor="#ff0000"circle:stroke_width="2dp"circle:totalProgress="100" circle:progress="10"circle:style_Type="stroke"/></RelativeLayout>

当xml文件中circle:style_Type="stroke"时



当xml文件中circle:style_Type="stroke_and_fill"时

4:activity中调用

public class MainActivity extends ActionBarActivity {CustomView customView;private float progress=0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);customView=(CustomView) findViewById(R.id.customview);handler.sendEmptyMessageDelayed(0, 1000);}Handler handler=new Handler(){public void handleMessage(android.os.Message msg) {if(msg.what==0){if(progress>100){return;}else{customView.setProgress(progress);progress+=2;handler.sendEmptyMessageDelayed(0, 100);}}};};}


当xml文件中circle:style_Type="stroke_and_fill"时

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/459809.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java二级考试备考_2017计算机二级考试《JAVA》备考测试题「带答案」

2017计算机二级考试《JAVA》备考测试题「带答案」为确保同学们将所涉及的考点全面复习到位&#xff0c;让大家充满信心的步入考场&#xff0c;以下是百分网小编搜索整理的一份计算机二级考试《JAVA》备考测试题【带答案】&#xff0c;供参考练习&#xff0c;希望对大家有所帮助…

java流类图结构_java学习之IO流(学习之旅,一)

个人在学习IO流的时候看到如下所示java 流类图结构的时候&#xff0c;我的感想是&#xff0c;这么多处于蒙的状态。Java流类图结构这么多&#xff0c;没有分类不好学&#xff0c;那我们就慢慢一口一口的吃&#xff0c;这样每天学习一点就好了&#xff0c;其实很多类并不是常用的…

php 安装xdebug扩展

php 扩展获取地址 http://pecl.php.net/package/ 编译安装的过程 wget http://pecl.php.net/get/xdebug-2.2.2.tgz tar -zxvf xdebug-2.2.2.tgz cd xdebug-2.2.2/ /data/klj/php/bin/phpize ./configure --enable-xdebug --with-php-config/data/klj/php/bin/php-config mak…

拨打电话 java_简单拨打电话程序

众所周知,对于一个手机,能拨打电话是其最重要也是最常用的一个功能.而在Android里是怎么样实现拨打电话的程序呢?我在这里写了一个简单的拨打电话的Demo,供大家参考.一共分为5个步骤.Step 1:新建一个Android工程,命名为phoneCallDemo.Step 2:设计程序的界面,打开main.xml把内容…

WPF01(xaml)

XAML&#xff1a;&#xff08;转自http://www.cnblogs.com/huangxincheng/archive/2012/06/17/2552511.html&#xff09; <Window x:Class"WpfApplication1.MainWindow"xmlns"http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x"…

java 线程 状态 图_Java提高——多线程(一)状态图

操作系统中的进程和线程的概念进程是指一个内存运行的应用程序&#xff0c;每个进程都有自己独立的一块内存空间&#xff0c;一个进程中可以启动多个线程&#xff0c;比如windows下的一个运行的应用程序.exe就是一个进程。线程是指进程中的一个执行流&#xff0c;一个进程可以运…

幽幽的灵光射不出你想要的疯狂

秋天到了&#xff0c;忧伤便无处可逃&#xff0c;秋天的忧伤的气息&#xff0c;就像一个妖艳的美女躺在你的身边&#xff0c;让你热血沸腾&#xff0c;冲动无比&#xff0c;而又悲喜交加&#xff0c;忧愁满地。如果不信&#xff0c;你可以试试。分享一首去年的诗歌&#xff0c;…

找规律

找规律填写NN方阵。如N8时, 其方阵为: 1 1 1 1 1 1 1 11 2 2 2 2 2 2 11 2 3 3 3 3 2 11 2 3 4 4 3 2 11 2 3 4 4 3 2 11 2 3 3 3 3 2 11 2 2 2 2 2 2 11 1 1 1 1 1 1 1 上代码&#xff1a; 1 #include <stdio.h&g…

arm qt5 iconv 问题

2019独角兽企业重金招聘Python工程师标准>>> 问题 3&#xff1a;./system/rootlib/helloworld -qws &#xff0c;程序运行起来&#xff0c;仍报错 QIconvCodec::convertFromUnicode: using Latin-1 for conversion, iconv_open failed …

[转]用Whois获得电信运营商的IP地址是如何分配的?

[转]用Whois获得电信运营商的IP地址是如何分配的? Linux下获得一些中国电信运营商的IP地址分配情况: APNIC是管理亚太地区IP地址分配的机构&#xff0c;它有着丰富准确的IP地址分配库&#xff0c;同时这些信息也是对外公开的&#xff0c;并提供了一个查询工具&#xff0c;下面…

庆祝教师节,李宁老师课程优惠劵疯抢中、会员卡优惠中,先到先得

李宁老师会员卡&#xff08;9-10至9-14&#xff09;大优惠&#xff1a;http://edu.51cto.com/member/id-12_1.html优惠劵只能购买李宁老师的视频课程&#xff1a;http://edu.51cto.com/member/id-12_1.html 优惠劵有效期&#xff1a;2015-9-10 至 2015-9-14 购买规则&#xf…

java mset_Java 反射机制(包括组成、结构、示例说明等内容)

第1部分 Java 反射机制介绍Java 反射机制。通俗来讲呢&#xff0c;就是在运行状态中&#xff0c;我们可以根据“类的部分已经的信息”来还原“类的全部的信息”。这里“类的部分已经的信息”&#xff0c;可以是“类名”或“类的对象”等信息。“类的全部信息”就是指“类的属性…

自己动手,实现一种类似ListT的数据结构(二)

前言&#xff1a; 首先&#xff0c;小匹夫要祝各位看官圣诞快乐&#xff0c;新年愉快&#xff5e;。上一篇文章《自己动手&#xff0c;实现一种类似List<T>的数据结构(一&#xff09;》 介绍了一下不依靠List<T>实现的各种接口&#xff0c;仿造一个轻量级数据结构的…

局域网内连接MySQL

2019独角兽企业重金招聘Python工程师标准>>> 局域网内连接MySQL 博客分类&#xff1a; MySQL MySQL局域网连接grant 我们都知道连接MySQL一般用的语句就是 jdbc:mysql://localhost:3306/database&#xff0c; 但是当你要连接到其他机器上的mysql的时候&#xff0c;…

[leetcod] Clone Graph

题目&#xff1a; Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors. OJs undirected graph serialization: Nodes are labeled uniquely. We use # as a separator for each node, and , as a separator for node label and …

【iOS7一些总结】9、与列表显示(在):列表显示UITableView

列表显示&#xff0c;顾名思义它是在一个列表视图的形式显示在屏幕上的数据的内容。于ios在列表视图UITableView达到。这个类在实际应用中频繁&#xff0c;是很easy理解。这里将UITableView的主要使用方法总结一下以备查。UITableView定义在头文件UITableView.h中&#xff0c;详…

SpringMVC(一):环境搭建

2019独角兽企业重金招聘Python工程师标准>>> //TODO 转载于:https://my.oschina.net/u/1020238/blog/505272

DockPanel 类

DockPanel 类 .NET Framework 4.5其他版本此主题尚未评级 - 评价此主题定义您可水平或垂直排列子元素的区域&#xff0c;互相。 继承层次结构 System.Object System.Windows.Threading.DispatcherObjectSystem.Windows.DependencyObjectSystem.Windows.Media.VisualSystem.Wind…

【分布式计算】MapReduce的替代者-Parameter Server

原文&#xff1a;http://blog.csdn.net/buptgshengod/article/details/46819051 首先还是要声明一下&#xff0c;这个文章是我在入职阿里云1个月以来&#xff0c;对于分布式计算的一点肤浅的认识&#xff0c;可能有些地方不够妥善&#xff0c;还请看官可以指出不足的地方&#…

iOS开发多线程篇—线程安全

iOS开发多线程篇—线程安全 一、多线程的安全隐患 资源共享 1块资源可能会被多个线程共享&#xff0c;也就是多个线程可能会访问同一块资源 比如多个线程访问同一个对象、同一个变量、同一个文件 当多个线程访问同一块资源时&#xff0c;很容易引发数据错乱和数据安全问题 示例…