你好,游客 登录 注册 搜索
背景:
阅读新闻

Android自定义实现圆形播放进度条

[日期:2012-06-23] 来源:Linux社区  作者:geniuseoe [字体:大 中 小]

canvas的drawArc(RectF oval, float startAngle, float sweepAngle, boolean useCenter, Paint paint) 方法是关键
相关参数说明大家详看SDK文档

控件大小决定oval
画笔属性设置paint
useCenter表示是否填充
startAngle是绘制圆弧的起点,我们使用-90度,即12点钟方向
sweepAngle是从起点顺时针绘制覆盖的角度,意味着进度值为30%的话,就是 360 * 30%

设置进度的代码片段:

  1. public synchronized void setMainProgress (int progress)  
  2.  {  
  3.         mMainCurProgress = progress;  
  4.         if (mMainCurProgress < 0)  
  5.         {  
  6.             mMainCurProgress = 0;  
  7.         }  
  8.           
  9.         if (mMainCurProgress > mMaxProgress)  
  10.         {  
  11.             mMainCurProgress = mMaxProgress;  
  12.         }  
  13.           
  14.         invalidate();  
  15. }  

设置进度值之后触发重绘,计算sweepAngle的值,最后完成绘制效果,怎么样,是不是就对上了
 
该控件的自定义属性如下:

  1. <?xml version="1.0" encoding="utf-8"?>    
  2. lt;resources>   
  3.   
  4.    <declare-styleable name="CircleProgressBar">    
  5.        <attr name="max" format="integer"/>                <!-- 进度条最大值 -->  
  6.        <attr name="fill" format="boolean"/>           <!-- 是否填充圆形区域 -->  
  7.        <attr name="Paint_Width" format="integer"/>        <!-- 画笔宽度,填充模式下无效,会被重置为0 -->  
  8.        <attr name="Paint_Color" format="integer"/>        <!-- 画笔颜色 -->  
  9.        <attr name="Inside_Interval" format="integer"/> <!-- 圆形区域向里缩进的距离 -->   
  10.    </declare-styleable>    
  11.      
  12. lt;/resources>   

再贴上本例的布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:roundProgress="http://schemas.android.com/apk/res/com.genius.progress"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical"   
  7.     android:background="#ffffff">  
  8.   
  9.       
  10.     <LinearLayout   
  11.     android:layout_height="wrap_content"   
  12.     android:layout_width="match_parent"   
  13.     android:id="@+id/linearLayout1"  
  14.     android:orientation="horizontal"  
  15.     android:gravity = "center_horizontal">  
  16.       
  17.         <Button   
  18.         android:text="增加主进度条"   
  19.         android:id="@+id/buttonAddMainPro"   
  20.         android:layout_width="wrap_content"   
  21.         android:layout_height="wrap_content"  
  22.         android:layout_weight="1">  
  23.         </Button>  
  24.           
  25.           
  26.         <Button   
  27.         android:text="增加子进度条"   
  28.         android:id="@+id/buttonAddSubPro"   
  29.         android:layout_width="wrap_content"   
  30.         android:layout_height="wrap_content"  
  31.         android:layout_weight="1">  
  32.         </Button>   
  33.           
  34.         <ImageButton  
  35.         android:id="@+id/buttonImage"   
  36.         android:layout_width="wrap_content"  
  37.         android:layout_height="wrap_content"  
  38.         android:src="@drawable/background2"  
  39.         android:layout_weight="1" />   
  40.           
  41.     </LinearLayout>  
  42.       
  43.           
  44.     <LinearLayout   
  45.         android:layout_height="wrap_content"   
  46.         android:layout_width="match_parent"   
  47.         android:id="@+id/linearLayout2"  
  48.         android:orientation="horizontal"  
  49.         android:background="#ff0000"  
  50.         android:gravity = "center_horizontal">  
  51.        
  52.        
  53.         <com.genius.circle.CircleProgress  
  54.         android:id="@+id/roundBar1"  
  55.         android:layout_width="wrap_content"    
  56.         android:layout_height="wrap_content"  
  57.         android:background="@drawable/background1"     
  58.         roundProgress:max="100"  
  59.         roundProgress:fill = "false"      
  60.         roundProgress:Inside_Interval="5"  
  61.         roundProgress:Paint_Width = "4"  
  62.         roundProgress:Paint_Color = "0xff0000ff"   
  63.         />  
  64.       
  65.           
  66.         <com.genius.circle.CircleProgress   
  67.          android:id="@+id/roundBar2"  
  68.          android:layout_width="wrap_content"    
  69.          android:layout_height="wrap_content"  
  70.          android:background="@drawable/background3"        
  71.          roundProgress:max="100"  
  72.          roundProgress:Inside_Interval="8"  
  73.          roundProgress:fill = "true"      
  74.          roundProgress:Paint_Width = "4"                   
  75.          roundProgress:Paint_Color = "0xffaa5500"   
  76.         />  
  77.        
  78.           
  79.         <com.genius.circle.CircleProgress   
  80.          android:id="@+id/roundBar3"  
  81.          android:layout_width="96dp"    
  82.          android:layout_height="96dp"     
  83.          roundProgress:max="100"  
  84.          roundProgress:fill="false"  
  85.           roundProgress:Paint_Width="40"        
  86.          roundProgress:Inside_Interval="20"  
  87.         />  
  88.           
  89.     </LinearLayout>  
  90.   
  91.        
  92.       
  93.        
  94.     <LinearLayout   
  95.     android:layout_height="wrap_content"   
  96.     android:layout_width="match_parent"   
  97.     android:id="@+id/linearLayout3"  
  98.     android:orientation="horizontal"  
  99.     android:gravity = "center_horizontal|center_vertical"  
  100.     android:background="#00ff00">    
  101.       
  102.       
  103.             <Button   
  104.             android:text="启动动画"   
  105.             android:id="@+id/buttonStart"   
  106.             android:layout_width="100dip"   
  107.             android:layout_height="wrap_content">  
  108.             </Button>  
  109.       
  110.       
  111.             <com.genius.circle.CircleProgress   
  112.              android:id="@+id/roundBar4"  
  113.              android:layout_width="wrap_content"    
  114.              android:layout_height="wrap_content"  
  115.              android:background="@drawable/background1"        
  116.              roundProgress:Inside_Interval="6"  
  117.              roundProgress:Paint_Color = "0xff0000ff"   
  118.             />  
  119.           
  120.             <Button   
  121.             android:text="停止动画"   
  122.             android:id="@+id/buttonStop"   
  123.             android:layout_width="100dip"   
  124.             android:layout_height="wrap_content">  
  125.             </Button>  
  126.       
  127.       
  128.     </LinearLayout>  
  129.       
  130.       
  131.   
  132. </LinearLayout>  
linux
相关资讯       Android开发入门 
本文评论   查看全部评论 (1)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 1 楼
* 匿名 发表于 2013/5/13 16:43:53
很好的东西,good 感谢楼主