转载自http://blog.csdn.net/codingandroid/article/details/8495074

自定义progressbar
现在要自定义一个等待的时候转动的小圈,相信大家也都嫌系统自带的很难看吧??
如果要自定义那些系统的组件都有一个法子,那就是看系统的是怎么写的。
看下系统的progressbar的方法:
首先看android的系统的style.xml的文件,系统的样式定义都在里面 android-sdk-windows\platforms\android-8\data\res\values 目录下打开style.xml,搜索ProgressBar。
可以看到系统是这样定义progressbar的:

  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>

接下来我们关注下        <item name="android:indeterminateDrawable">@android:drawable/progress_medium_white</item> 这一行。可以看到它使用了android:drawable/progress_medium_white这样的一个资源
找到这个文件并且打开,我们可以看到:

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. - <!-- /*
  3. **
  4. ** Copyright 2009, The Android Open Source Project
  5. **
  6. ** Licensed under the Apache License, Version 2.0 (the "License");
  7. ** you may not use this file except in compliance with the License.
  8. ** You may obtain a copy of the License at
  9. **
  10. **     http://www.apache.org/licenses/LICENSE-2.0
  11. **
  12. ** Unless required by applicable law or agreed to in writing, software
  13. ** distributed under the License is distributed on an "AS IS" BASIS,
  14. ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. ** See the License for the specific language governing permissions and
  16. ** limitations under the License.
  17. */
  18. -->
  19. <animated-rotate
  20. xmlns:android="http://schemas.android.com/apk/res/android"
  21. android:drawable="@drawable/spinner_white_48" android:pivotX="50%"
  22. android:pivotY="50%"
  23. android:framesCount="12"
  24. android:frameDuration="100" />

我把前面的注释去掉,大家再看:

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <animated-rotate
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:drawable="@drawable/spinner_white_48"
  5. android:pivotX="50%"
  6. android:pivotY="50%"
  7. android:framesCount="12"
  8. android:frameDuration="100" />

就剩这么多了,然后分析下这个文件(总共没有几行代码了嘛)
xmlns:android="http://schemas.android.com/apk/res/android" 约束,不说了,也不需要我们关注
android:drawable="@drawable/spinner_white_48"这个相信接触过android的都知道这是指定了一个图标吧
android:pivotX="50%" 
android:pivotY="50%" 这两行代码是指定了一个点(point嘛)那是什么点呢,中心点,我们让那个圆圈围着这个点转动,就有了动画效果,所以它是指定的围绕哪个点转动(为了证明我的猜想,我在后来自定义的代码中将他们都改成了0,它们就围绕左上角的那个点转动了,所以证明了我的猜想是对的哦,不信的朋友可以再写完以后自己试一下)
android:framesCount="12"  这个是啥帧的count我也不太清楚了
android:frameDuration="100"这个应该是转圈持续的时间,我们可以在做完后改一改这些数字,就知道他们干嘛的啦。

看完这个文件,我们想,已经没有用到其他的文件了,只是缺少一个图标,我到360安全卫士拷贝了一个(虽然个人不太喜欢这个软件的霸道,但图片还是挺喜欢的,嘿嘿)

总结下系统是怎么定义一个progressbar的
1:定义了一个样式:style,style中使用了一个属性:progress_medium_white
2:定义一个属性progress_medium_white.xml,就这两步就完工啦。

好,那光说不练假把式,接下来就去实现下吧:
1:在我们工程的  res/values下新建一个styles.xml的文件,并拷贝progressbar的样式过来

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <style name="test_progressbar_style">
  4. <item name="android:indeterminateOnly">true</item>
  5. <item name="android:indeterminateDrawable">@android:drawable/progress_medium_white</item>
  6. <item name="android:indeterminateBehavior">repeat</item>
  7. <item name="android:indeterminateDuration">3500</item>
  8. <item name="android:minWidth">48dip</item>
  9. <item name="android:maxWidth">48dip</item>
  10. <item name="android:minHeight">48dip</item>
  11. <item name="android:maxHeight">48dip</item>
  12. </style>
  13. </resources>

我就不去修改它的属性了,大小啥的,想修改的话自己研究下不会很难哦。
没有写过style的朋友注意下,这个xml文件,申明不说,节点是 resource,style需要申明name name="test_progressbar_style"这个就是以后要引用这个style的名字啦

2:当然现在会报错,因为progress_medium_white这个还没有建嘛
在res下新建 drawable目录
在目录下新建progress_medium_white.xml文件,把系统的拷贝进来:

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <animated-rotate
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:drawable="@drawable/spinner_white_48"
  5. android:pivotX="50%"
  6. android:pivotY="50%"
  7. android:framesCount="12"
  8. android:frameDuration="100" />

再把准备好的图片拷贝到drawable-hdpi,或者任意其他目录下。
把android:drawable="@drawable/spinner_white_48" 后面的名称改成图片的名字就成功了。

3:然后可以在布局文件中定义一个试一下了哦

  1. <ProgressBar
  2. style="@style/test_progressbar_style"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content" />

没白忙活吧,这个的修改虽然很简单,但是我们掌握了这个修改方法,举一反三,就可以把系统的任意样式改成我们喜欢的了哦

最新文章

  1. 10个惊艳的Swift单行代码
  2. Spring Mvc Rest为不支持DELETE/PUT的浏览器添加DELETE/PUT支持
  3. 使用AndroidStudio报错:INSTALL_FAILED_UPDATE_INCOMPATIBLE
  4. Android开发环境部署
  5. perl 语法速查
  6. centos下安装xfce+vnc
  7. C#中父窗口和子窗口之间实现控件互操作
  8. POJ 3067 Japan 树状数组求逆序对
  9. MySQL主从同步校验与重新同步
  10. ACM HDU 1559 最大子矩阵
  11. 我的第二个网页制作:p,hn,br标签的使用
  12. SpringBoot集成redis的key,value序列化的相关问题
  13. ffmpeg 在windows 上编译
  14. socks5服务器编写经验总结
  15. /proc/diskstats
  16. BZOJ.4738.[清华集训2016]汽水(点分治 分数规划)
  17. Python全栈之路----常用模块----sys模块
  18. maxcompute笔记
  19. 手脱JDPack
  20. [GO]方法值和方法表达式

热门文章

  1. iOS多线程方案总结及使用详解
  2. 垂直对齐:vertical-align属性——使用中注意事项
  3. TZOJ 4024 游戏人生之梦幻西游(连续子段和绝对值最小)
  4. Leetcode162. Find Peak Element寻找峰值
  5. goland设置颜色和字体
  6. MySQL8.0.17 - Multi-Valued Indexes 简述
  7. LUOGU P1937 [USACO10MAR]仓配置Barn Allocation
  8. Vue. 之 npm安装 axios
  9. python 数字编码
  10. Axure之添加点击页面