material progressbar颜色设置

来源:互联网 发布:皇室战争淘宝慢充原理 编辑:程序博客网 时间:2024/05/29 04:05



一直觉得Android L的ProgressBar,也就是那个加载小圆圈动画非常潮,研究后发现谷歌是使用矢量图形VectorDrawable(SVG)画出图形和一些动画混合制作而成的,流畅而且生动。这里写点使用心得。

首先看下sdk里的xml源码。

[html] view plain copy
 print?
  1. <style name="Widget.Material.ProgressBar" parent="Widget.ProgressBar">  
  2.     <item name="indeterminateDrawable">@drawable/progress_medium_material</item>  
  3. </style>  

可以看到谷歌做了一个Material风格的drawable,再看看drawable怎么写的,

[html] view plain copy
 print?
  1. <animated-vector xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:drawable="@drawable/vector_drawable_progress_bar_medium" >  
  3.   
  4.     <target  
  5.         android:name="progressBar"  
  6.         android:animation="@anim/progress_indeterminate_material" />  
  7.   
  8.     <target  
  9.         android:name="root"  
  10.         android:animation="@anim/progress_indeterminate_rotation_material" />  
  11.   
  12. </animated-vector>  
这是一个animated-vector,字面上理解就是动画加矢量图形。上面drawable就是图形,下面是两个属性动画,再来看看vector_drawable_progress_bar_medium,
[html] view plain copy
 print?
  1. <vector xmlns:android="http://schemas.android.com/apk/res/android"  
  2.         android:height="48dp"  
  3.         android:width="48dp"  
  4.         android:viewportHeight="48"  
  5.         android:viewportWidth="48"  
  6.         android:tint="?attr/colorControlActivated">  
  7.   
  8.     <group  
  9.         android:name="root"  
  10.         android:translateX="24.0"  
  11.         android:translateY="24.0" >  
  12.         <path  
  13.             android:name="progressBar"  
  14.             android:fillColor="#00000000"  
  15.             android:pathData="M0, 0 m 0, -19 a 19,19 0 1,1 0,38 a 19,19 0 1,1 0,-38"  
  16.             android:strokeColor="@color/white"  
  17.             android:strokeLineCap="square"  
  18.             android:strokeLineJoin="miter"  
  19.             android:strokeWidth="4"  
  20.             android:trimPathEnd="0"  
  21.             android:trimPathOffset="0"  
  22.             android:trimPathStart="0" />  
  23.     </group>  
  24.   
  25. </vector>  
这里可以看到一些配置属性,大小什么的,pathData就这个绘图(SVG)的核心参数,再看下两个动画文件,
[html] view plain copy
 print?
  1. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  2.   
  3.     <objectAnimator  
  4.         android:duration="1333"  
  5.         android:interpolator="@interpolator/trim_start_interpolator"  
  6.         android:propertyName="trimPathStart"  
  7.         android:repeatCount="-1"  
  8.         android:valueFrom="0"  
  9.         android:valueTo="0.75"  
  10.         android:valueType="floatType" />  
  11.     <objectAnimator  
  12.         android:duration="1333"  
  13.         android:interpolator="@interpolator/trim_end_interpolator"  
  14.         android:propertyName="trimPathEnd"  
  15.         android:repeatCount="-1"  
  16.         android:valueFrom="0"  
  17.         android:valueTo="0.75"  
  18.         android:valueType="floatType" />  
  19.     <objectAnimator  
  20.         android:duration="1333"  
  21.         android:interpolator="@android:anim/linear_interpolator"  
  22.         android:propertyName="trimPathOffset"  
  23.         android:repeatCount="-1"  
  24.         android:valueFrom="0"  
  25.         android:valueTo="0.25"  
  26.         android:valueType="floatType" />  
  27.   
  28. </set>  
[html] view plain copy
 print?
  1. <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:duration="6665"  
  3.     android:interpolator="@android:anim/linear_interpolator"  
  4.     android:propertyName="rotation"  
  5.     android:repeatCount="-1"  
  6.     android:valueFrom="0"  
  7.     android:valueTo="720"  
  8.     android:valueType="floatType" />  
动画文件上理解可能是一个圆圈线条6.665秒内进行720度旋转,然后进行伸缩变换,复合成了动态的一个动画效果。


再来说说如何在项目中应用这个ProgressBar。

跟其他MaterialDesign控件和主题一样,你需要一个SupportV7兼容包或者高版本Sdk,兼容包在你的Sdk\extras\support\v7\appcompat目录下,最好保持最新。

1.在你的项目中挂载SupportV7兼容包,也就是项目的project.properties中写上android.library.reference.1=../导入的v7工程名

2.在你的style.xml里新建个ProgressBar的style,这个style继承于v7中的ProgressBar风格,他是自动根据sdk进行适配,如果高于L则会显现出MaterialDesign风格。

[html] view plain copy
 print?
  1. <style name="MyProgressBar" parent="Base.Widget.AppCompat.ProgressBar">  
  2.      <item name="android:indeterminateTint">你自定义的颜色</item>  
  3.      <item name="android:indeterminateTintMode">src_atop</item>  
  4.  </style>  
3.刚开始一直苦于不知道如何自定义这个ProgressBar颜色,后来看了源码xml中的attrs,才试出来是indeterminateTint和indeterminateTintMode两个属性共同作用的结果,TintMode是混合模式。源码如下,

[html] view plain copy
 print?
  1. <!-- Tint to apply to the indeterminate progress indicator. -->  
  2.         <attr name="indeterminateTint" format="color" />  
  3.         <!-- Blending mode used to apply the indeterminate progress indicator tint. -->  
  4.         <attr name="indeterminateTintMode">  
  5.             <!-- The tint is drawn on top of the drawable.  
  6.                  [Sa + (1 - Sa)*Da, Rc = Sc + (1 - Sa)*Dc] -->  
  7.             <enum name="src_over" value="3" />  
  8.             <!-- The tint is masked by the alpha channel of the drawable. The drawable’s  
  9.                  color channels are thrown out. [Sa * Da, Sc * Da] -->  
  10.             <enum name="src_in" value="5" />  
  11.             <!-- The tint is drawn above the drawable, but with the drawable’s alpha  
  12.                  channel masking the result. [Da, Sc * Da + (1 - Sa) * Dc] -->  
  13.             <enum name="src_atop" value="9" />  
  14.             <!-- Multiplies the color and alpha channels of the drawable with those of  
  15.                  the tint. [Sa * Da, Sc * Dc] -->  
  16.             <enum name="multiply" value="14" />  
  17.             <!-- [Sa + Da - Sa * Da, Sc + Dc - Sc * Dc] -->  
  18.             <enum name="screen" value="15" />  
  19.             <!-- Combines the tint and drawable color and alpha channels, clamping the  
  20.                  result to valid color values. Saturate(S + D) -->  
  21.             <enum name="add" value="16" />  
  22.         </attr>  
还有一些更多的自定义属性,不再赘述。

4.最后再再你的layout中加入这个控件。

[html] view plain copy
 print?
  1. <ProgressBar  
  2.        style="@style/MyProgressBar"  
  3.        android:layout_width="wrap_content"  
  4.        android:layout_height="wrap_content"  
  5.        android:layout_centerInParent="true"  
  6.        android:visibility="visible" />  

简单的控制隐藏和显示就能达到转圈加载中和加载完成的效果,十分简单。



原文地址 http://blog.csdn.net/csdn393417081/article/details/47749165

1 0
原创粉丝点击