当前位置:   article > 正文

Android12 ---- Material You 应用_com.google.android.material:material

com.google.android.material:material

背景

       Google android S 新特性,当你更换壁纸,整个手机主题的颜色会根据壁纸的配色方案而改变。也就说,每当你更新壁纸,你的手机界面也会焕然一新

       当用户在 Android 12 设备上更改壁纸时,系统会分析图像以选择一种颜色,并使用初始种子颜色通过算法选择主要(Primary)、次要(Secondary)、第三(Tertiary)和错误(Error)颜色。同时,它应用了色彩理论和可访问性规则。从这些颜色中,该算法创建从 0% 亮度(黑色)到 100%(白色)的色调调色板。

环境准备

1、应用的最低 SDK 设置为 31 或更高版本。

2、material主题包应用升级到1.5.0或以上 'com.google.android.material:material:1.5.0'

3、Material You 主题生成网站

Material Theme Builder

       添加自定义的颜色点击EXPORT导出XML,会生成两套主题即日常模式和黑暗模式(values,valu-night),这些文件可以按原样复制替换,但必须更改AndroidManifest.xml 或主题文件中的主题名称以相互匹配。工具导出主题的默认名称是 AppTheme。

  1. //生成的主题继承自Material3
  2. <style name="AppTheme" parent="Theme.Material3.Light.NoActionBar">
  3. <item name="colorPrimary">@color/md_theme_light_primary</item>
  4. <item name="colorOnPrimary">@color/md_theme_light_onPrimary</item>
  5. <item name="colorPrimaryContainer">@color/md_theme_light_primaryContainer</item>
  6. <item name="colorOnPrimaryContainer">@color/md_theme_light_onPrimaryContainer</item>
  7. ......
  8. <item name="colorColor90">#286b2a</item>
  9. <item name="colorOnColor90">#ffffff</item>
  10. <item name="colorColor90Container">#abf5a3</item>
  11. <item name="colorOnColor90Container">#012104</item>
  12. <item name="harmonizeColor90">false</item>
  13. <item name="colorColor29">#00639c</item>
  14. <item name="colorOnColor29">#ffffff</item>
  15. <item name="colorColor29Container">#cce5ff</item>
  16. <item name="colorOnColor29Container">#001d33</item>
  17. <item name="harmonizeColor29">false</item>
  18. <item name="colorPrimaryInverse">@color/md_theme_light_primaryInverse</item>
  19. </style>

对应方式

        Material 库中的DynamicColors类利用 Activity Lifecycle Callbacks来确定何时以及如何应用颜色叠加。使用提供的 API 调用,可以将动态颜色应用于activity或整个应用程序。还可以确定应在何时何地应用动态颜色。

       避免意外影响,应确保应用程序代码组件引用Material主题属性即

android:color="?attr/colorPrimary",而不是应用任何硬编码颜色(HEX代码或@color/)

  1. import android.app.Application;
  2. import com.google.android.material.color.DynamicColors;
  3. public class MyApplication extends Application {
  4. @Override
  5. public void onCreate() {
  6. super.onCreate();
  7. DynamicColors.applyToActivitiesIfAvailable(this);
  8. }
  9. }

       如果不需要整个应用生效

  1. class MainActivity : AppCompatActivity() {
  2. private lateinit var binding: ActivityMainBinding
  3. override fun onCreate(savedInstanceState: Bundle?) {
  4. super.onCreate(savedInstanceState)
  5. DynamicColors.applyIfAvailable(this)
  6. ......
  7. }

       如果想要将原色为其他部分颜色

①color.xml

  1. <resources>
  2. <color name="overlay_light_primary">#9C4146</color>
  3. <color name="overlay_light_onPrimary">#FFFFFF</color>
  4. <color name= "overlay_light_primaryContainer">#FFDADB</color>
  5. <color name="overlay_light_onPrimaryContainer">#400008</color>
  6. </resources >

②style.xml

  1. <style name="AppTheme.Overlay" parent="ThemeOverlay.Material3.DynamicColors.Light">
  2. <item name="colorPrimary">@color/overlay_light_primary</item>
  3. <item name="colorOnPrimary">@color/overlay_light_onPrimary</item>
  4. <item name="colorPrimaryContainer">@color/overlay_light_primaryContainer</item>
  5. <item name="colorOnPrimaryContainer">@color/overlay_light_onPrimaryContainer<item>
  6. </style>

③更换API

DynamicColors.applyToActivitiesIfAvailable(this, R.style.AppTheme_Overlay)

       若果应用中控件的颜色还是无法跟随系统改变颜色,还可以使用以下方法

style.xml中,将颜色取值改为@android:color/system_accent1_100

  1. <style name="AppTheme.Overlay" parent="ThemeOverlay.Material3.DynamicColors.Light">
  2. <item name="colorPrimary">@android:color/system_accent1_100</item>
  3. <item name="colorOnPrimary">@android:color/system_accent1_100</item>
  4. <item name="colorPrimaryContainer">@android:color/system_accent1_100</item>
  5. <item name="colorOnPrimaryContainer">@android:color/system_accent1_100<item>
  6. </style>

具体的色值选取,可以参考

  1. <!-- Lightest shade of the accent color used by the system. White.
  2. This value can be overlaid at runtime by OverlayManager RROs. -->
  3. <color name="system_accent1_0">#ffffff</color>
  4. <!-- Shade of the accent system color at 99% lightness.
  5. This value can be overlaid at runtime by OverlayManager RROs. -->
  6. <color name="system_accent1_10">#F1FFFC</color>
  7. <!-- Shade of the accent system color at 95% lightness.
  8. This value can be overlaid at runtime by OverlayManager RROs. -->
  9. <color name="system_accent1_50">#9CFFF2</color>
  10. <!-- Shade of the accent system color at 90% lightness.
  11. This value can be overlaid at runtime by OverlayManager RROs. -->
  12. <color name="system_accent1_100">#8DF5E3</color>
  13. <!-- Shade of the accent system color at 80% lightness.
  14. This value can be overlaid at runtime by OverlayManager RROs. -->
  15. <color name="system_accent1_200">#71D8C7</color>
  16. <!-- Shade of the accent system color at 70% lightness.
  17. This value can be overlaid at runtime by OverlayManager RROs. -->
  18. <color name="system_accent1_300">#53BCAC</color>
  19. <!-- Shade of the accent system color at 60% lightness.
  20. This value can be overlaid at runtime by OverlayManager RROs. -->
  21. <color name="system_accent1_400">#34A192</color>
  22. <!-- Shade of the accent system color at 49% lightness.
  23. This value can be overlaid at runtime by OverlayManager RROs. -->
  24. <color name="system_accent1_500">#008375</color>
  25. <!-- Shade of the accent system color at 40% lightness.
  26. This value can be overlaid at runtime by OverlayManager RROs. -->
  27. <color name="system_accent1_600">#006C5F</color>
  28. <!-- Shade of the accent system color at 30% lightness.
  29. This value can be overlaid at runtime by OverlayManager RROs. -->
  30. <color name="system_accent1_700">#005747</color>
  31. <!-- Shade of the accent system color at 20% lightness.
  32. This value can be overlaid at runtime by OverlayManager RROs. -->
  33. <color name="system_accent1_800">#003E31</color>
  34. <!-- Shade of the accent system color at 10% lightness.
  35. This value can be overlaid at runtime by OverlayManager RROs. -->
  36. <color name="system_accent1_900">#002214</color>
  37. <!-- Darkest shade of the accent color used by the system. Black.
  38. This value can be overlaid at runtime by OverlayManager RROs. -->
  39. <color name="system_accent1_1000">#000000</color>
  40. <!-- Lightest shade of the secondary accent color used by the system. White.
  41. This value can be overlaid at runtime by OverlayManager RROs. -->
  42. <color name="system_accent2_0">#ffffff</color>
  43. <!-- Shade of the secondary accent system color at 99% lightness.
  44. This value can be overlaid at runtime by OverlayManager RROs. -->
  45. <color name="system_accent2_10">#F0FFFC</color>
  46. <!-- Shade of the secondary accent system color at 95% lightness.
  47. This value can be overlaid at runtime by OverlayManager RROs. -->
  48. <color name="system_accent2_50">#CDFAF1</color>
  49. <!-- Shade of the secondary accent system color at 90% lightness.
  50. This value can be overlaid at runtime by OverlayManager RROs. -->
  51. <color name="system_accent2_100">#BFEBE3</color>
  52. <!-- Shade of the secondary accent system color at 80% lightness.
  53. This value can be overlaid at runtime by OverlayManager RROs. -->
  54. <color name="system_accent2_200">#A4CFC7</color>
  55. <!-- Shade of the secondary accent system color at 70% lightness.
  56. This value can be overlaid at runtime by OverlayManager RROs. -->
  57. <color name="system_accent2_300">#89B4AC</color>
  58. <!-- Shade of the secondary accent system color at 60% lightness.
  59. This value can be overlaid at runtime by OverlayManager RROs. -->
  60. <color name="system_accent2_400">#6F9991</color>
  61. <!-- Shade of the secondary accent system color at 49% lightness.
  62. This value can be overlaid at runtime by OverlayManager RROs. -->
  63. <color name="system_accent2_500">#537C75</color>
  64. <!-- Shade of the secondary accent system color at 40% lightness.
  65. This value can be overlaid at runtime by OverlayManager RROs. -->
  66. <color name="system_accent2_600">#3D665F</color>
  67. <!-- Shade of the secondary accent system color at 30% lightness.
  68. This value can be overlaid at runtime by OverlayManager RROs. -->
  69. <color name="system_accent2_700">#254E47</color>
  70. <!-- Shade of the secondary accent system color at 20% lightness.
  71. This value can be overlaid at runtime by OverlayManager RROs. -->
  72. <color name="system_accent2_800">#0C3731</color>
  73. <!-- Shade of the secondary accent system color at 10% lightness.
  74. This value can be overlaid at runtime by OverlayManager RROs. -->
  75. <color name="system_accent2_900">#00211C</color>
  76. <!-- Darkest shade of the secondary accent color used by the system. Black.
  77. This value can be overlaid at runtime by OverlayManager RROs. -->
  78. <color name="system_accent2_1000">#000000</color>
  79. <!-- Lightest shade of the tertiary accent color used by the system. White.
  80. This value can be overlaid at runtime by OverlayManager RROs. -->
  81. <color name="system_accent3_0">#ffffff</color>
  82. <!-- Shade of the tertiary accent system color at 99% lightness.
  83. This value can be overlaid at runtime by OverlayManager RROs. -->
  84. <color name="system_accent3_10">#FFFBFF</color>
  85. <!-- Shade of the tertiary accent system color at 95% lightness.
  86. This value can be overlaid at runtime by OverlayManager RROs. -->
  87. <color name="system_accent3_50">#F9EAFF</color>
  88. <!-- Shade of the tertiary accent system color at 90% lightness.
  89. This value can be overlaid at runtime by OverlayManager RROs. -->
  90. <color name="system_accent3_100">#ECDBFF</color>
  91. <!-- Shade of the tertiary accent system color at 80% lightness.
  92. This value can be overlaid at runtime by OverlayManager RROs. -->
  93. <color name="system_accent3_200">#CFBFEB</color>
  94. <!-- Shade of the tertiary accent system color at 70% lightness.
  95. This value can be overlaid at runtime by OverlayManager RROs. -->
  96. <color name="system_accent3_300">#B3A4CF</color>
  97. <!-- Shade of the tertiary accent system color at 60% lightness.
  98. This value can be overlaid at runtime by OverlayManager RROs. -->
  99. <color name="system_accent3_400">#988AB3</color>
  100. <!-- Shade of the tertiary accent system color at 49% lightness.
  101. This value can be overlaid at runtime by OverlayManager RROs. -->
  102. <color name="system_accent3_500">#7B6E96</color>
  103. <!-- Shade of the tertiary accent system color at 40% lightness.
  104. This value can be overlaid at runtime by OverlayManager RROs. -->
  105. <color name="system_accent3_600">#64587F</color>
  106. <!-- Shade of the tertiary accent system color at 30% lightness.
  107. This value can be overlaid at runtime by OverlayManager RROs. -->
  108. <color name="system_accent3_700">#4C4165</color>
  109. <!-- Shade of the tertiary accent system color at 20% lightness.
  110. This value can be overlaid at runtime by OverlayManager RROs. -->
  111. <color name="system_accent3_800">#352B4D</color>
  112. <!-- Shade of the tertiary accent system color at 10% lightness.
  113. This value can be overlaid at runtime by OverlayManager RROs. -->
  114. <color name="system_accent3_900">#1E1636</color>
  115. <!-- Darkest shade of the tertiary accent color used by the system. Black.
  116. This value can be overlaid at runtime by OverlayManager RROs. -->
  117. <color name="system_accent3_1000">#000000</color>
  118. <!-- Lightest shade of the neutral color used by the system. White.
  119. This value can be overlaid at runtime by OverlayManager RROs. -->
  120. <color name="system_neutral1_0">#ffffff</color>
  121. <!-- Shade of the neutral system color at 99% lightness.
  122. This value can be overlaid at runtime by OverlayManager RROs. -->
  123. <color name="system_neutral1_10">#fbfbfb</color>
  124. <!-- Shade of the neutral system color at 95% lightness.
  125. This value can be overlaid at runtime by OverlayManager RROs. -->
  126. <color name="system_neutral1_50">#f0f0f0</color>
  127. <!-- Shade of the neutral system color at 90% lightness.
  128. This value can be overlaid at runtime by OverlayManager RROs. -->
  129. <color name="system_neutral1_100">#e2e2e2</color>
  130. <!-- Shade of the neutral system color at 80% lightness.
  131. This value can be overlaid at runtime by OverlayManager RROs. -->
  132. <color name="system_neutral1_200">#c6c6c6</color>
  133. <!-- Shade of the neutral system color at 70% lightness.
  134. This value can be overlaid at runtime by OverlayManager RROs. -->
  135. <color name="system_neutral1_300">#ababab</color>
  136. <!-- Shade of the neutral system color at 60% lightness.
  137. This value can be overlaid at runtime by OverlayManager RROs. -->
  138. <color name="system_neutral1_400">#909090</color>
  139. <!-- Shade of the neutral system color at 49% lightness.
  140. This value can be overlaid at runtime by OverlayManager RROs. -->
  141. <color name="system_neutral1_500">#757575</color>
  142. <!-- Shade of the neutral system color at 40% lightness.
  143. This value can be overlaid at runtime by OverlayManager RROs. -->
  144. <color name="system_neutral1_600">#5e5e5e</color>
  145. <!-- Shade of the neutral system color at 30% lightness.
  146. This value can be overlaid at runtime by OverlayManager RROs. -->
  147. <color name="system_neutral1_700">#464646</color>
  148. <!-- Shade of the neutral system color at 20% lightness.
  149. This value can be overlaid at runtime by OverlayManager RROs. -->
  150. <color name="system_neutral1_800">#303030</color>
  151. <!-- Shade of the neutral system color at 10% lightness.
  152. This value can be overlaid at runtime by OverlayManager RROs. -->
  153. <color name="system_neutral1_900">#1b1b1b</color>
  154. <!-- Darkest shade of the neutral color used by the system. Black.
  155. This value can be overlaid at runtime by OverlayManager RROs. -->
  156. <color name="system_neutral1_1000">#000000</color>
  157. <!-- Lightest shade of the secondary neutral color used by the system. White.
  158. This value can be overlaid at runtime by OverlayManager RROs. -->
  159. <color name="system_neutral2_0">#ffffff</color>
  160. <!-- Shade of the secondary neutral system color at 99% lightness.
  161. This value can be overlaid at runtime by OverlayManager RROs. -->
  162. <color name="system_neutral2_10">#fbfbfb</color>
  163. <!-- Shade of the secondary neutral system color at 95% lightness.
  164. This value can be overlaid at runtime by OverlayManager RROs. -->
  165. <color name="system_neutral2_50">#f0f0f0</color>
  166. <!-- Shade of the secondary neutral system color at 90% lightness.
  167. This value can be overlaid at runtime by OverlayManager RROs. -->
  168. <color name="system_neutral2_100">#e2e2e2</color>
  169. <!-- Shade of the secondary neutral system color at 80% lightness.
  170. This value can be overlaid at runtime by OverlayManager RROs. -->
  171. <color name="system_neutral2_200">#c6c6c6</color>
  172. <!-- Shade of the secondary neutral system color at 70% lightness.
  173. This value can be overlaid at runtime by OverlayManager RROs. -->
  174. <color name="system_neutral2_300">#ababab</color>
  175. <!-- Shade of the secondary neutral system color at 60% lightness.
  176. This value can be overlaid at runtime by OverlayManager RROs. -->
  177. <color name="system_neutral2_400">#909090</color>
  178. <!-- Shade of the secondary neutral system color at 49% lightness.
  179. This value can be overlaid at runtime by OverlayManager RROs. -->
  180. <color name="system_neutral2_500">#757575</color>
  181. <!-- Shade of the secondary neutral system color at 40% lightness.
  182. This value can be overlaid at runtime by OverlayManager RROs. -->
  183. <color name="system_neutral2_600">#5e5e5e</color>
  184. <!-- Shade of the secondary neutral system color at 30% lightness.
  185. This value can be overlaid at runtime by OverlayManager RROs. -->
  186. <color name="system_neutral2_700">#464646</color>
  187. <!-- Shade of the secondary neutral system color at 20% lightness.
  188. This value can be overlaid at runtime by OverlayManager RROs. -->
  189. <color name="system_neutral2_800">#303030</color>
  190. <!-- Shade of the secondary neutral system color at 10% lightness.
  191. This value can be overlaid at runtime by OverlayManager RROs. -->
  192. <color name="system_neutral2_900">#1b1b1b</color>
  193. <!-- Darkest shade of the secondary neutral color used by the system. Black.
  194. This value can be overlaid at runtime by OverlayManager RROs. -->
  195. <color name="system_neutral2_1000">#000000</color>
  196. <!-- Accessibility shortcut icon background color -->
  197. <color name="accessibility_feature_background">#5F6368</color> <!-- Google grey 700 -->
  198. <color name="accessibility_magnification_background">#F50D60</color>
  199. <color name="accessibility_daltonizer_background">#00BCD4</color>
  200. <color name="accessibility_color_inversion_background">#546E7A</color>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/144542
推荐阅读