系统主题样式-以ProgressBar为例

这篇文章将分析系统ProgressBar的主题及样式

当前应用环境:

compileSdkVersion 24
targetSdkVersion 24
Theme.AppCompat.Light
appcompat-v7:25.3.1

先来看看系统ProgressBar的继承结构
ProgressBar继承自View,常见的子类包括SeekBar及RatingBar

By default, the progress bar is a spinning wheel (an indeterminate indicator).
进度条默认情况下为无进度圆形样式
如果改成条形XML中使用style="@android:style/Widget.ProgressBar.Horizontal"

我们来验证一下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

android24模拟器显示效果:


1.gif

但是在android19模拟器上的效果为:


2.gif

更神奇的是android19模拟器上改变一下背景颜色:


3.gif

虽然默认都是无进度的圆形,但是效果为什么不一样?

主题样式原码分析

关于Activity是如何加载对应的主题,请参考:https://www.cnblogs.com/chenxibobo/p/6136681.html
总之最后调用Resources中的selectSystemTheme函数。

    Resources.java
    public static int selectDefaultTheme(int curTheme, int targetSdkVersion) {
        return selectSystemTheme(curTheme, targetSdkVersion,
                com.android.internal.R.style.Theme,
                com.android.internal.R.style.Theme_Holo,
                com.android.internal.R.style.Theme_DeviceDefault,
                com.android.internal.R.style.Theme_DeviceDefault_Light_DarkActionBar);
    }

    public static int selectSystemTheme(int curTheme, int targetSdkVersion, int orig, int holo,
            int dark, int deviceDefault) {
        if (curTheme != 0) {//如果设置了主题,直接返回
            return curTheme;
        }
        //没有设置的情况下,如果targetSdkVersion 小于11则返回R.style.Theme
        if (targetSdkVersion < Build.VERSION_CODES.HONEYCOMB) {
            return orig;
        }
        //targetSdkVersion 如果小于14则返回R.style.Theme_Holo
        if (targetSdkVersion < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            return holo;
        }
        //targetSdkVersion 如果小于24则返回R.style.Theme_DeviceDefault
        if (targetSdkVersion < Build.VERSION_CODES.N) {
            return dark;
        }
        return deviceDefault;//返回R.style.Theme_DeviceDefault_Light_DarkActionBar
    }

我们当前设置了主题,并且主题为Theme.AppCompat.Light。

appcompat-v7包 values.xml

    //对于父类主题,不同Android版本对应不同版本的values.xml,查看下图
    <style name="Theme.AppCompat.Light" parent="Base.Theme.AppCompat.Light"/>
    <style name="Base.Theme.AppCompat.Light" parent="Base.V7.Theme.AppCompat.Light">
    //Theme.AppCompat.Light主题,最终都会采用该主题,下面会具体分析
    <style name="Base.V7.Theme.AppCompat.Light" parent="Platform.AppCompat.Light">
    </style>

查看Base.Theme.AppCompat.Light,不同Android版本对应相应的values.xml


image.png

appcompat-v7包 values-v23.xml

    <style name="Base.Theme.AppCompat.Light" parent="Base.V23.Theme.AppCompat.Light"/>
    <style name="Base.V23.Theme.AppCompat.Light" parent="Base.V22.Theme.AppCompat.Light">
      ...
    </style>

appcompat-v7包 values-v22.xml

    <style name="Base.Theme.AppCompat.Light" parent="Base.V22.Theme.AppCompat.Light"/>
    <style name="Base.V22.Theme.AppCompat.Light" parent="Base.V21.Theme.AppCompat.Light">
       ...
    </style>

appcompat-v7包 values-v21.xml

    <style name="Base.Theme.AppCompat.Light" parent="Base.V21.Theme.AppCompat.Light"/>
    <style name="Base.V21.Theme.AppCompat.Light" parent="Base.V7.Theme.AppCompat.Light">
       ...
    </style>

最终到达之前提到的 appcompat-v7包 values.xml

    //Theme.AppCompat.Light主题,最终都会采用该主题
    <style name="Base.V7.Theme.AppCompat.Light" parent="Platform.AppCompat.Light">
    </style>

查看Platform.AppCompat.Light


11.png
注意,这里就是为什么不同android 版本显示的效果不同的原因。

appcompat-v7包 values-v25.xml

    <style name="Platform.AppCompat.Light" parent="Platform.V25.AppCompat.Light"/>
    <style name="Platform.V25.AppCompat.Light" parent="android:Theme.Material.Light.NoActionBar">
    </style>

appcompat-v7包 values-v21.xml

    <style name="Platform.AppCompat.Light" parent="Platform.V21.AppCompat.Light"/>
    <style name="Platform.V21.AppCompat.Light" parent="android:Theme.Material.Light.NoActionBar">
       ...
    </style>
5.0及以上主题为android:Theme.Material

appcompat-v7包 values-v14.xml

    <style name="Platform.AppCompat.Light" parent="Platform.V14.AppCompat.Light"/>
    <style name="Platform.V14.AppCompat.Light" parent="Platform.V11.AppCompat.Light">
      ...
    </style>

appcompat-v7包 values-v11.xml

    <style name="Platform.AppCompat.Light" parent="Platform.V11.AppCompat.Light"/>
    <style name="Platform.V11.AppCompat.Light" parent="android:Theme.Holo.Light">
      ...
    </style>

3.0及以上主题为android:Theme.Holo

appcompat-v7包 values.xml

    <style name="Platform.AppCompat.Light" parent="android:Theme.Light">
      ...
    </style>

3.0以下主题为android:Theme

到此与Resources类中的selectSystemTheme()方法便能对应上了。

我们来分析1.gif(Android 24):

系统根据Android版本,这里会选择android:Theme.Material.Light.NoActionBar主题,继承自android:Theme.Material.Light。

themes_material.xml

    <style name="Theme.Material.Light" parent="Theme.Light">
        <!-- Widget styles -->
        <item name="progressBarStyle">@style/Widget.Material.Light.ProgressBar</item>
    </style>
    <style name="Widget.Material.Light.ProgressBar" parent="Widget.Material.ProgressBar"/>

styles_material.xml

    <style name="Widget.Material.ProgressBar" parent="Widget.ProgressBar">
        <item name="indeterminateDrawable">@drawable/progress_medium_material</item>
    </style>

progress_medium_material.xml

<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/vector_drawable_progress_bar_medium" >

    <target
        android:name="progressBar"
        android:animation="@anim/progress_indeterminate_material" />

    <target
        android:name="root"
        android:animation="@anim/progress_indeterminate_rotation_material" />
</animated-vector>

这里不再详细分析此Drawable,这里涉及到AnimatedVectorDrawable, SVG等相关内容
可以参考我的另一篇文章Android SVG
至此,1.gif的效果便分析完成了。

我们再来分析2.gif 及3.gif

其实这两张图片是完全一模一样的,只不过图2因为背景与控件Drawable中的部分内容颜色相同,没有显示出来而已。
这两张图相对应的android版本为19,主题为android:Theme.Holo.Light

themes_holo.xml

    <style name="Theme.Holo.Light" parent="Theme.Light">
        <!-- Widget styles -->
        <item name="progressBarStyle">@style/Widget.Holo.Light.ProgressBar</item>

    </style>

styles_holo.xml

    <style name="Widget.Holo.Light.ProgressBar" parent="Widget.Holo.ProgressBar" />
    <style name="Widget.Holo.ProgressBar" parent="Widget.ProgressBar">
        <item name="indeterminateDrawable">@drawable/progress_medium_holo</item>
    </style>

progress_medium_holo.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    //两张图片同时相反方向旋转,同一时间旋转的角度不同,造成自转的效果。
    <item>
        <rotate
             android:drawable="@drawable/spinner_48_outer_holo"
             android:pivotX="50%"
             android:pivotY="50%"
             android:fromDegrees="0"
             android:toDegrees="1080" />
    </item>
    <item>
        <rotate
             android:drawable="@drawable/spinner_48_inner_holo"
             android:pivotX="50%"
             android:pivotY="50%"
             android:fromDegrees="720"
             android:toDegrees="0" />
    </item>
</layer-list>

@drawable/spinner_48_outer_holo


image.png

@drawable/spinner_48_inner_holo


image.png

至此,2.gif 3.gif的效果便分析完了。

接下来我们去掉Application主题,看一下效果:
progress.gif

因为当前的的targetSdkVersion 是24,所以Resources.selectSystemTheme()返回的主题是R.style.Theme_DeviceDefault_Light_DarkActionBar。

我们分析下原码:

themes_device_defaults.xml

    <style name="Theme.DeviceDefault.Light.DarkActionBar" parent="Theme.Material.Light.DarkActionBar" />

themes_material.xml 貌似没有看到相关的属性

    <style name="Theme.Material.Light.DarkActionBar">
        <item name="actionBarWidgetTheme">@null</item>
        <item name="actionBarTheme">@style/ThemeOverlay.Material.Dark.ActionBar</item>
        <item name="popupTheme">@style/ThemeOverlay.Material.Light</item>       
        <item name="colorPrimaryDark">@color/primary_dark_material_dark</item>
        <item name="colorPrimary">@color/primary_material_dark</item>
    </style>

themes_material.xml 父类主题

    <!-- Material theme (light version). -->
    <style name="Theme.Material.Light" parent="Theme.Light">
        <!-- Widget styles -->
        <item name="progressBarStyle">@style/Widget.Material.Light.ProgressBar</item>
        <!-- Color palette -->
        //progressbar的indeterminateDrawable中控件的颜色设置为android:tint="?attr/colorControlActivated"
        <item name="colorControlActivated">?attr/colorAccent</item>
        //最终指向colorAccent的颜色
        <item name="colorAccent">@color/accent_material_light</item>
    </style>

styles_material.xml

<style name="Widget.Material.Light.ProgressBar" parent="Widget.Material.ProgressBar"/>

再一次看到Widget.Material.ProgressBar。

colors_material.xml

    <color name="accent_material_light">@color/material_deep_teal_500</color>
    <color name="material_deep_teal_500">#ff009688</color>//正是我们控件的颜色。

所以progressbar的indeterminateDrawable没有改变,只是颜色改变了。

参考:

https://www.cnblogs.com/chenxibobo/p/6136681.html

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,911评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,014评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 142,129评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,283评论 1 264
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,159评论 4 357
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,161评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,565评论 3 382
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,251评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,531评论 1 292
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,619评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,383评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,255评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,624评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,916评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,199评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,553评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,756评论 2 335