Android系统中ProgressBar的深入理解

一、系统Theme和Style##

1. 属性的定义

[ /frameworks/base/core/res/res/values/attrs.xml]
<resources>
    <!-- These are the standard attributes that make up a complete theme. -->
    <declare-styleable name="Theme">
        <!-- ============== -->
        <!-- Generic styles -->
        ...
        <!--定义属性名, format="reference"表示可随主题配置的style-->
        <attr name="progressBarStyle" format="reference" />
        <attr name="progressBarStyleHorizontal" format="reference" />
        <attr name="progressBarStyleSmall" format="reference" />
        <attr name="progressBarStyleSmallTitle" format="reference" />
        <attr name="progressBarStyleLarge" format="reference" />
        <attr name="progressBarStyleInverse" format="reference" />
        <attr name="progressBarStyleSmallInverse" format="reference" />
        <attr name="progressBarStyleLargeInverse" format="reference" />
    </declare-styleable>
    ...
    <!-- 名称为ProgressBar的styleable所包含的属性名集合。-->
    <declare-styleable name="ProgressBar">
        <!-- Defines the maximum value the progress can take. -->
        <attr name="max" format="integer" />
        <!-- Defines the default progress value, between 0 and max. -->
        <attr name="progress" format="integer" />
        <!-- Defines the secondary progress value, between 0 and max. This progress is drawn between
             the primary progress and the background.  It can be ideal for media scenarios such as
             showing the buffering progress while the default progress shows the play progress. -->
        <attr name="secondaryProgress" format="integer" />
        <!-- Allows to enable the indeterminate mode. In this mode the progress
         bar plays an infinite looping animation. -->
        <attr name="indeterminate" format="boolean" />
        <!-- Restricts to ONLY indeterminate mode (state-keeping progress mode will not work). -->
        <attr name="indeterminateOnly" format="boolean" />
        <!-- Drawable used for the indeterminate mode. -->
        <attr name="indeterminateDrawable" format="reference" />
        <!-- Drawable used for the progress mode. -->
        <attr name="progressDrawable" format="reference" />
        <!-- Duration of the indeterminate animation. -->
        <attr name="indeterminateDuration" format="integer" min="1" />
        <!-- Defines how the indeterminate mode should behave when the progress
        reaches max. -->
        <attr name="indeterminateBehavior">
            <!-- Progress starts over from 0. -->
            <enum name="repeat" value="1" />
            <!-- Progress keeps the current value and goes back to 0. -->
            <enum name="cycle" value="2" />
        </attr>
        <attr name="minWidth" format="dimension" />
        <attr name="maxWidth" />
        <attr name="minHeight" format="dimension" />
        <attr name="maxHeight" />
        <attr name="interpolator" format="reference" />
        <!-- Timeout between frames of animation in milliseconds
             {@deprecated Not used by the framework.} -->
        <attr name="animationResolution" format="integer" />
        <!-- Defines if the associated drawables need to be mirrored when in RTL mode.
             Default is false -->
        <attr name="mirrorForRtl" format="boolean" />
        <!-- Tint to apply to the progress indicator. -->
        <attr name="progressTint" format="color" />
        <!-- Blending mode used to apply the progress indicator tint. -->
        <attr name="progressTintMode">
            <!-- The tint is drawn on top of the drawable.
                 [Sa + (1 - Sa)*Da, Rc = Sc + (1 - Sa)*Dc] -->
            <enum name="src_over" value="3" />
            <!-- The tint is masked by the alpha channel of the drawable. The drawable’s
                 color channels are thrown out. [Sa * Da, Sc * Da] -->
            <enum name="src_in" value="5" />
            <!-- The tint is drawn above the drawable, but with the drawable’s alpha
                 channel masking the result. [Da, Sc * Da + (1 - Sa) * Dc] -->
            <enum name="src_atop" value="9" />
            <!-- Multiplies the color and alpha channels of the drawable with those of
                 the tint. [Sa * Da, Sc * Dc] -->
            <enum name="multiply" value="14" />
            <!-- [Sa + Da - Sa * Da, Sc + Dc - Sc * Dc] -->
            <enum name="screen" value="15" />
            <!-- Combines the tint and drawable color and alpha channels, clamping the
                 result to valid color values. Saturate(S + D) -->
            <enum name="add" value="16" />
        </attr>
        <!-- Tint to apply to the progress indicator background. -->
        <attr name="progressBackgroundTint" format="color" />
        <!-- Blending mode used to apply the progress indicator background tint. -->
        <attr name="progressBackgroundTintMode">
            <!-- The tint is drawn on top of the drawable.
                 [Sa + (1 - Sa)*Da, Rc = Sc + (1 - Sa)*Dc] -->
            <enum name="src_over" value="3" />
            <!-- The tint is masked by the alpha channel of the drawable. The drawable’s
                 color channels are thrown out. [Sa * Da, Sc * Da] -->
            <enum name="src_in" value="5" />
            <!-- The tint is drawn above the drawable, but with the drawable’s alpha
                 channel masking the result. [Da, Sc * Da + (1 - Sa) * Dc] -->
            <enum name="src_atop" value="9" />
            <!-- Multiplies the color and alpha channels of the drawable with those of
                 the tint. [Sa * Da, Sc * Dc] -->
            <enum name="multiply" value="14" />
            <!-- [Sa + Da - Sa * Da, Sc + Dc - Sc * Dc] -->
            <enum name="screen" value="15" />
            <!-- Combines the tint and drawable color and alpha channels, clamping the
                 result to valid color values. Saturate(S + D) -->
            <enum name="add" value="16" />
        </attr>
        <!-- Tint to apply to the secondary progress indicator. -->
        <attr name="secondaryProgressTint" format="color" />
        <!-- Blending mode used to apply the secondary progress indicator tint. -->
        <attr name="secondaryProgressTintMode">
            <!-- The tint is drawn on top of the drawable.
                 [Sa + (1 - Sa)*Da, Rc = Sc + (1 - Sa)*Dc] -->
            <enum name="src_over" value="3" />
            <!-- The tint is masked by the alpha channel of the drawable. The drawable’s
                 color channels are thrown out. [Sa * Da, Sc * Da] -->
            <enum name="src_in" value="5" />
            <!-- The tint is drawn above the drawable, but with the drawable’s alpha
                 channel masking the result. [Da, Sc * Da + (1 - Sa) * Dc] -->
            <enum name="src_atop" value="9" />
            <!-- Multiplies the color and alpha channels of the drawable with those of
                 the tint. [Sa * Da, Sc * Dc] -->
            <enum name="multiply" value="14" />
            <!-- [Sa + Da - Sa * Da, Sc + Dc - Sc * Dc] -->
            <enum name="screen" value="15" />
            <!-- Combines the tint and drawable color and alpha channels, clamping the
                 result to valid color values. Saturate(S + D) -->
            <enum name="add" value="16" />
        </attr>
        <!-- Tint to apply to the indeterminate progress indicator. -->
        <attr name="indeterminateTint" format="color" />
        <!-- Blending mode used to apply the indeterminate progress indicator tint. -->
        <attr name="indeterminateTintMode">
            <!-- The tint is drawn on top of the drawable.
                 [Sa + (1 - Sa)*Da, Rc = Sc + (1 - Sa)*Dc] -->
            <enum name="src_over" value="3" />
            <!-- The tint is masked by the alpha channel of the drawable. The drawable’s
                 color channels are thrown out. [Sa * Da, Sc * Da] -->
            <enum name="src_in" value="5" />
            <!-- The tint is drawn above the drawable, but with the drawable’s alpha
                 channel masking the result. [Da, Sc * Da + (1 - Sa) * Dc] -->
            <enum name="src_atop" value="9" />
            <!-- Multiplies the color and alpha channels of the drawable with those of
                 the tint. [Sa * Da, Sc * Dc] -->
            <enum name="multiply" value="14" />
            <!-- [Sa + Da - Sa * Da, Sc + Dc - Sc * Dc] -->
            <enum name="screen" value="15" />
            <!-- Combines the tint and drawable color and alpha channels, clamping the
                 result to valid color values. Saturate(S + D) -->
            <enum name="add" value="16" />
        </attr>
        <!-- Tint to apply to the background. -->
        <attr name="backgroundTint" />
        <!-- Blending mode used to apply the background tint. -->
        <attr name="backgroundTintMode" />
    </declare-styleable>
</resources>

2. 不同主题下的样式

2.1 Theme主题
[ /frameworks/base/core/res/res/values/themes.xml]
<resources>
    <style name="Theme">
        <!-- Widget styles -->
        <item name="progressBarStyle">@style/Widget.ProgressBar</item>
        <item name="progressBarStyleHorizontal">@style/Widget.ProgressBar.Horizontal</item>
        <item name="progressBarStyleSmall">@style/Widget.ProgressBar.Small</item>
        <item name="progressBarStyleSmallTitle">@style/Widget.ProgressBar.Small.Title</item>
        <item name="progressBarStyleLarge">@style/Widget.ProgressBar.Large</item>
        <item name="progressBarStyleInverse">@style/Widget.ProgressBar.Inverse</item>
        <item name="progressBarStyleSmallInverse">@style/Widget.ProgressBar.Small.Inverse</item>
        <item name="progressBarStyleLargeInverse">@style/Widget.ProgressBar.Large.Inverse</item>
        ...
    </style>

    <style name="Theme.Light">
        <item name="progressBarStyle">@style/Widget.ProgressBar.Inverse</item>
        <item name="progressBarStyleSmall">@style/Widget.ProgressBar.Small.Inverse</item>
        <item name="progressBarStyleLarge">@style/Widget.ProgressBar.Large.Inverse</item>
        <item name="progressBarStyleInverse">@style/Widget.ProgressBar</item>
        <item name="progressBarStyleSmallInverse">@style/Widget.ProgressBar.Small</item>
        <item name="progressBarStyleLargeInverse">@style/Widget.ProgressBar.Large</item>
     ...
    </style>
</resources>
2.1Theme.Holo主题
[ /frameworks/base/core/res/res/values/themes_holo.xml]
<resources>
    <style name="Theme.Holo">
        <item name="progressBarStyle">@style/Widget.Holo.ProgressBar</item>
        <item name="progressBarStyleHorizontal">@style/Widget.Holo.ProgressBar.Horizontal</item>
        <item name="progressBarStyleSmall">@style/Widget.Holo.ProgressBar.Small</item>
        <item name="progressBarStyleSmallTitle">@style/Widget.Holo.ProgressBar.Small.Title</item>
        <item name="progressBarStyleLarge">@style/Widget.Holo.ProgressBar.Large</item>
        <item name="progressBarStyleInverse">@style/Widget.Holo.ProgressBar.Inverse</item>
        <item name="progressBarStyleSmallInverse">@style/Widget.Holo.ProgressBar.Small.Inverse</item>
        <item name="progressBarStyleLargeInverse">@style/Widget.Holo.ProgressBar.Large.Inverse</item>
    </style>

    <style name="Theme.Holo.Light" parent="Theme.Light">
        <item name="progressBarStyle">@style/Widget.Holo.Light.ProgressBar</item>
        <item name="progressBarStyleHorizontal">@style/Widget.Holo.Light.ProgressBar.Horizontal</item>
        <item name="progressBarStyleSmall">@style/Widget.Holo.Light.ProgressBar.Small</item>
        <item name="progressBarStyleSmallTitle">@style/Widget.Holo.Light.ProgressBar.Small.Title</item>
        <item name="progressBarStyleLarge">@style/Widget.Holo.Light.ProgressBar.Large</item>
        <item name="progressBarStyleInverse">@style/Widget.Holo.Light.ProgressBar.Inverse</item>
        <item name="progressBarStyleSmallInverse">@style/Widget.Holo.Light.ProgressBar.Small.Inverse</item>
        <item name="progressBarStyleLargeInverse">@style/Widget.Holo.Light.ProgressBar.Large.Inverse</item>
    </style>
</resources>

3. 引用的style列表

[ /frameworks/base/core/res/res/values/styles.xml]
<resources>
     <style name="Widget.ProgressBar">
        <item name="indeterminateOnly">true</item>
        <item name="indeterminateDrawable">@drawable/progress_medium_white</item>
        <item name="indeterminateBehavior">repeat</item>
        <item name="indeterminateDuration">3500</item>
        <item name="minWidth">48dip</item>
        <item name="maxWidth">48dip</item>
        <item name="minHeight">48dip</item>
        <item name="maxHeight">48dip</item>
        <item name="mirrorForRtl">false</item>
    </style>

    <style name="Widget.ProgressBar.Large">
        <item name="indeterminateDrawable">@drawable/progress_large_white</item>
        <item name="minWidth">76dip</item>
        <item name="maxWidth">76dip</item>
        <item name="minHeight">76dip</item>
        <item name="maxHeight">76dip</item>
    </style>

    <style name="Widget.ProgressBar.Small">
        <item name="indeterminateDrawable">@drawable/progress_small_white</item>
        <item name="minWidth">16dip</item>
        <item name="maxWidth">16dip</item>
        <item name="minHeight">16dip</item>
        <item name="maxHeight">16dip</item>
    </style>

    <style name="Widget.ProgressBar.Inverse">
        <item name="indeterminateDrawable">@drawable/progress_medium</item>
    </style>

    <style name="Widget.ProgressBar.Large.Inverse">
        <item name="indeterminateDrawable">@drawable/progress_large</item>
    </style>

    <style name="Widget.ProgressBar.Small.Inverse">
        <item name="indeterminateDrawable">@drawable/progress_small</item>
    </style>

    <style name="Widget.ProgressBar.Small.Title">
        <item name="indeterminateDrawable">@drawable/progress_small_titlebar</item>
    </style>

    <style name="Widget.ProgressBar.Horizontal">
        <item name="indeterminateOnly">false</item>
        <item name="progressDrawable">@drawable/progress_horizontal</item>
        <item name="indeterminateDrawable">@drawable/progress_indeterminate_horizontal</item>
        <item name="minHeight">20dip</item>
        <item name="maxHeight">20dip</item>
        <item name="mirrorForRtl">true</item>
    </style>
</resources>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容