android 自定义进度条颜色

先看图

基于产品经理各种自定义需求,经过查阅了解,下面是自己对android自定义进度条的学习过程!


这个没法了只能看源码了,还好下载了源码, sources\base\core\res\res\ 下应有尽有,修改进度条颜色只能找progress ,因为是改变样式,首先找styles.xml

找到xml后,进去找到

[html] view plain copy
  1. <spanstyle="font-size:18px"><stylename="Widget.ProgressBar">
  2. <itemname="android:indeterminateOnly">true</item>
  3. <itemname="android:indeterminateDrawable">@android:drawable/progress_medium_white</item>
  4. <itemname="android:indeterminateBehavior">repeat</item>
  5. <itemname="android:indeterminateDuration">3500</item>
  6. <itemname="android:minWidth">48dip</item>
  7. <itemname="android:maxWidth">48dip</item>
  8. <itemname="android:minHeight">48dip</item>
  9. <itemname="android:maxHeight">48dip</item>
  10. </style></span>


这是默认转圈的效果style,但今天我们修改的是水平进度条颜色!

所以找到

[html] view plain copy
  1. <spanstyle="font-size:18px"><stylename="Widget.ProgressBar.Horizontal">
  2. <itemname="android:indeterminateOnly">false</item>
  3. <itemname="android:progressDrawable">@android:drawable/progress_horizontal</item>
  4. <itemname="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
  5. <itemname="android:minHeight">20dip</item>
  6. <itemname="android:maxHeight">20dip</item>
  7. </style></span>

你看系统一步一步关联的,扩展性很性,低耦合,所以我们现在只要改变进度条是怎么样画出来的就行了 ,但是负责画进度条的是

<item name="android:progressDrawable"> 所以我们可以找到"drawable下的 progress_horizontal 文件,改变他就可以改变进度条颜色

[java] view plain copy
  1. <spanstyle="font-size:18px"><?xmlversion="1.0"encoding="utf-8"?>
  2. <!--Copyright(C)2008TheAndroidOpenSourceProject
  3. LicensedundertheApacheLicense,Version2.0(the"License");
  4. youmaynotusethisfileexceptincompliancewiththeLicense.
  5. YoumayobtainacopyoftheLicenseat
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unlessrequiredbyapplicablelaworagreedtoinwriting,software
  8. distributedundertheLicenseisdistributedonan"ASIS"BASIS,
  9. WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.
  10. SeetheLicenseforthespecificlanguagegoverningpermissionsand
  11. limitationsundertheLicense.
  12. -->
  13. <layer-listxmlns:android="http://schemas.android.com/apk/res/android">
  14. <itemandroid:id="@android:id/background">
  15. <shape>
  16. <cornersandroid:radius="5dip"/>
  17. <gradient
  18. android:startColor="#ff9d9e9d"
  19. android:centerColor="#ff5a5d5a"
  20. android:centerY="0.75"
  21. android:endColor="#ff747674"
  22. android:angle="270"
  23. />
  24. </shape>
  25. </item>
  26. <itemandroid:id="@android:id/secondaryProgress">
  27. <clip>
  28. <shape>
  29. <cornersandroid:radius="5dip"/>
  30. <gradient
  31. android:startColor="#80ffd300"
  32. android:centerColor="#80ffb600"
  33. android:centerY="0.75"
  34. android:endColor="#a0ffcb00"
  35. android:angle="270"
  36. />
  37. </shape>
  38. </clip>
  39. </item>
  40. <itemandroid:id="@android:id/progress">
  41. <clip>
  42. <shape>
  43. <cornersandroid:radius="5dip"/>
  44. <gradient
  45. android:startColor="#ffffd300"
  46. android:centerColor="#ffffb600"
  47. android:centerY="0.75"
  48. android:endColor="#ffffcb00"
  49. android:angle="270"
  50. />
  51. </shape>
  52. </clip>
  53. </item>
  54. </layer-list>
  55. </span>

看到没有,这是系统的进度条画出的布局条件

android:startColor="#80ffd300"

android:centerColor="#80ffb600"

android:endColor="#ff747674"

我们只要改变这个色值就能改变他的颜色,主要改变的是<item android:id="@android:id/progress">下的色值就行了

说了这么多,到底怎么做呢, 很简单

1 。 在我们的项目下新建一个 style.xml 文件

创建一个style 标签,集成系统默认样式,然后自定义一个新的progressDrawable 文件,随后面在layout 中的progress 中引用这个文件就行

<style name="ProgressBar_Mini"parent="@android:style/Widget.ProgressBar.Horizontal">
<item name="android:maxHeight">50dip</item>
<item name="android:minHeight">8dip</item>
<item name="android:indeterminateOnly">false</item>
<item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
<item name="android:progressDrawable">@drawable/progressbar_mini</item>
</style>

下面是我的progressbar_mini 文件,改变了下android:endColor="#F5F5F5" android:startColor="#BEBEBE" 的色值

[java] view plain copy
  1. <spanstyle="font-size:18px"><layer-listxmlns:android="http://schemas.android.com/apk/res/android">
  2. <itemandroid:id="@android:id/background">
  3. <shape>
  4. <cornersandroid:radius="5dip"/>
  5. <gradient
  6. android:angle="270"
  7. android:centerY="0.75"
  8. android:endColor="#F5F5F5"
  9. android:startColor="#BEBEBE"/>
  10. </shape>
  11. </item>
  12. <itemandroid:id="@android:id/secondaryProgress">
  13. <clip>
  14. <shape>
  15. <cornersandroid:radius="0dip"/>
  16. <gradient
  17. android:angle="270"
  18. android:centerY="0.75"
  19. android:endColor="#165CBC"
  20. android:startColor="#85B0E9"/>
  21. </shape>
  22. </clip>
  23. </item>
  24. <itemandroid:id="@android:id/progress">
  25. <clip>
  26. <shape>
  27. <cornersandroid:radius="5dip"/>
  28. <gradient
  29. android:angle="270"
  30. android:centerY="0.75"
  31. android:endColor="#165CBC"
  32. android:startColor="#85B0E9"/>
  33. </shape>
  34. </clip>
  35. </item>
  36. </layer-list></span>


最后在中引用就可以了

<ProgressBar
android:id="@+id/progress"
style="@style/ProgressBar_Mini"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:progress="50" />

转载申明初入:http://blog.csdn.net/liao3841054/article/details/7556551

更多相关文章

  1. android多apk共享私有文件
  2. android之使用signapk打包成系统应用,获取系统权限
  3. Android开发11――手机横屏和竖屏与android:configChanges
  4. android下修改hosts文件不起作用(无效)的解决办法
  5. Android中使用手势识别功能
  6. android 模拟器手机如何添加文件到sd卡
  7. Android源码内部编译过程总结(Make)(转)
  8. 2011.07.14——— android res与assets的区别
  9. NPM 和webpack 的基础使用

随机推荐

  1. (转)关于android的4.2的0文件夹的详解(目
  2. android 利用shape做控件背景(小圆点,空心
  3. Android(安卓)7.0+抓包https突破ssl-pinn
  4. Android中使用POI加载与显示word文档
  5. 做一个有上下滚动效果的TextView
  6. Android(安卓)Monkey入门及使用
  7. Android(安卓)Service服务
  8. 无usb数据线在真机上调试android程序
  9. Android内存泄露自动检测神器LeakCanary
  10. Android中为什么主线程不会因为Looper.lo