自定义progressbar

 

、、、、、、、、、、、、、、、、、、、第一种、、、、、、、、、、、、、、、、、、

1.首先,我们要准备我们自己需要的转圈圈的图或者进度条的图片,并且命名为
progressbar_indeterminate1
progressbar_indeterminate2
progressbar_indeterminate3 ...等。

然后写一个xml文件,如下:

 

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <animation-list  
  3.         xmlns:android="http://schemas.android.com/apk/res/android"  
  4.         android:oneshot="false">  
  5.     <item android:drawable="@drawable/progressbar_indeterminate1" android:duration="200" />  
  6.     <item android:drawable="@drawable/progressbar_indeterminate2" android:duration="200" />  
  7.     <item android:drawable="@drawable/progressbar_indeterminate3" android:duration="200" />  
  8.     <item android:drawable="@drawable/progressbar_indeterminate4" android:duration="200" />  
  9.     <item android:drawable="@drawable/progressbar_indeterminate5" android:duration="200" />  
  10.     <item android:drawable="@drawable/progressbar_indeterminate6" android:duration="200" />  
  11.     <item android:drawable="@drawable/progressbar_indeterminate7" android:duration="200" />  
  12.     <item android:drawable="@drawable/progressbar_indeterminate8" android:duration="200" />  
  13.     <item android:drawable="@drawable/progressbar_indeterminate9" android:duration="200" />  
  14.     <item android:drawable="@drawable/progressbar_indeterminate10" android:duration="200" />  
  15.     <item android:drawable="@drawable/progressbar_indeterminate11" android:duration="200" />  
  16. </animation-list>  

 

 

2.在我们的Progressbar控件中这么配置我们刚才定义好的样式:

 

Xml代码   收藏代码
  1. <ProgressBar  
  2.  android:indeterminateDrawable="@drawable/progressbar_indeterminate"  
  3.          android:id="@+id/progressBar1"  
  4.  android:layout_width="wrap_content"  
  5.  android:layout_height="wrap_content">  
  6. </ProgressBar>  

 

 

3.OK,大功告成!!

4.最后总结一下:
  我们应该多看一下系统的那些样式,看看google的工程师们是怎么去定义这些东西的,从中我们可以学到很多东西。
  顺便说一下,虽然本文讲的Progressbar的样式,但是,我们完全可以把这个设置到一个需要设置动画的页面布局上,比如我们的程序在进入的时候需要播放一个动画,宣传公司的某个产品或者说提示用户怎么操作我们的程序以及一些快捷键什么的,完全可以这么去配置,而不再需要一个ImageView对象和AnimationDrawable对象,来通过start()来播放我们的动画,是不是很方便呐!!哈哈!!

 

、、、、、、、、、、、、、、、、、、、第二种、、、、、、、、、、、、、、、、、、

1.在drawable目录下新建一个progress_drawable_large.xml文件,如是写;

可查看源码:\frameworks\base\core\res\res\drawable\progress_large.xml

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:drawable="@drawable/bg_loading_1"    
  4.     android:pivotX="50%"    
  5.     android:pivotY="50%" />    

 

说明:bg_loading_1.png便是我们要设置的转圈圈的图片,一张便可。是不是比上面的节省资源吖~

 

2.应用到控件当中去,如是写:

Xml代码   收藏代码
  1. <ProgressBar    
  2.     android:indeterminateDrawable="@drawable/progress_drawable_large"    
  3.     android:indeterminateOnly="true"    
  4.     android:indeterminateDuration="500"    
  5.     android:indeterminateBehavior="repeat"    
  6.     android:layout_width="wrap_content"    
  7.     android:layout_height="wrap_content"></ProgressBar>    

 

说明:indeterminateDuration 设置转动时间

 

3.没有第三步,oker,大功告成了 ,哈哈,简单吧~

 

以上方法纯参照系统源码学习:

 

附:系统部分源码

 

111.系统中设置长条形进度条进度背景的progress_horizontal.xml:

可查看源码:...frameworks\base\core\res\res\drawable\progress_horizontal.xml

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8" ?>     
  2. - <!--  Copyright (C) 2008 The Android Open Source Project    
  3.     
  4.      Licensed under the Apache License, Version 2.0 (the "License");    
  5.      you may not use this file except in compliance with the License.    
  6.      You may obtain a copy of the License at    
  7.     
  8.           http://www.apache.org/licenses/LICENSE-2.0    
  9.     
  10.      Unless required by applicable law or agreed to in writing, software    
  11.      distributed under the License is distributed on an "AS IS" BASIS,    
  12.      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.    
  13.      See the License for the specific language governing permissions and    
  14.      limitations under the License.    
  15.     
  16.   -->     
  17. <layer-list xmlns:android="http://schemas.android.com/apk/res/android">    
  18. <item android:id="@android:id/background">    
  19. <shape>    
  20.   <corners android:radius="5dip" />     
  21.   <gradient android:startColor="#ff9d9e9d" android:centerColor="#ff5a5d5a" android:centerY="0.75" android:endColor="#ff747674" android:angle="270" />     
  22.   </shape>    
  23.   </item>    
  24. <item android:id="@android:id/secondaryProgress">    
  25. <clip>    
  26. <shape>    
  27.   <corners android:radius="5dip" />     
  28.   <gradient android:startColor="#80ffd300" android:centerColor="#80ffb600" android:centerY="0.75" android:endColor="#a0ffcb00" android:angle="270" />     
  29.   </shape>    
  30.   </clip>    
  31.   </item>    
  32. <item android:id="@android:id/progress">    
  33. <clip>    
  34. <shape>    
  35.   <corners android:radius="5dip" />     
  36.   <gradient android:startColor="#ffffd300" android:centerColor="#ffffb600" android:centerY="0.75" android:endColor="#ffffcb00" android:angle="270" />     
  37.   </shape>    
  38.   </clip>    
  39.   </item>    
  40.   </layer-list>    

 

应用到控件中:

Xml代码   收藏代码
  1. <ProgressBar    
  2.     android:progressDrawable="@drawable/progress_horizontal"    
  3.     android:layout_width="wrap_content"    
  4.     android:layout_height="wrap_content"></ProgressBar>    

 

222.系统中几种常见的样式:

可查看源码...\frameworks\base\core\res\res\values\styles.xml

Xml代码   收藏代码
  1. <style name="Widget.ProgressBar">    
  2.   <item name="android:indeterminateOnly">true</item>     
  3.   <item name="android:indeterminateDrawable">@android:drawable/progress_medium_white</item>     
  4.   <item name="android:indeterminateBehavior">repeat</item>     
  5.   <item name="android:indeterminateDuration">3500</item>     
  6.   <item name="android:minWidth">48dip</item>     
  7.   <item name="android:maxWidth">48dip</item>     
  8.   <item name="android:minHeight">48dip</item>     
  9.   <item name="android:maxHeight">48dip</item>     
  10.   </style>    
  11. <style name="Widget.ProgressBar.Large">    
  12.   <item name="android:indeterminateDrawable">@android:drawable/progress_large_white</item>     
  13.   <item name="android:minWidth">76dip</item>     
  14.   <item name="android:maxWidth">76dip</item>     
  15.   <item name="android:minHeight">76dip</item>     
  16.   <item name="android:maxHeight">76dip</item>     
  17.   </style>    
  18. <style name="Widget.ProgressBar.Small">    
  19.   <item name="android:indeterminateDrawable">@android:drawable/progress_small_white</item>     
  20.   <item name="android:minWidth">16dip</item>     
  21.   <item name="android:maxWidth">16dip</item>     
  22.   <item name="android:minHeight">16dip</item>     
  23.   <item name="android:maxHeight">16dip</item>     
  24.   </style>    
  25. <style name="Widget.ProgressBar.Inverse">    
  26.   <item name="android:indeterminateDrawable">@android:drawable/progress_medium</item>     
  27.   </style>    
  28. <style name="Widget.ProgressBar.Large.Inverse">    
  29.   <item name="android:indeterminateDrawable">@android:drawable/progress_large</item>     
  30.   </style>    
  31. <style name="Widget.ProgressBar.Small.Inverse">    
  32.   <item name="android:indeterminateDrawable">@android:drawable/progress_small</item>     
  33.   </style>    
  34. <style name="Widget.ProgressBar.Small.Title">    
  35.   <item name="android:indeterminateDrawable">@android:drawable/progress_small_titlebar</item>     
  36.   </style>    
  37. <style name="Widget.ProgressBar.Horizontal">    
  38.   <item name="android:indeterminateOnly">false</item>     
  39.   <item name="android:progressDrawable">@android:drawable/progress_horizontal</item>     
  40.   <item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>     
  41.   <item name="android:minHeight">20dip</item>     
  42.   <item name="android:maxHeight">20dip</item>     
  43.   </style>   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值