如何应用Material Design 3和Material You

material you 是下一代 material design 的发展方向,也是一种全新的设计愿景: 方便您打造个性化的样式设计、满足各种需求并自适应各种屏幕;jetpack compose 是用于构建原生 android 界面的新款现代工具包,可以帮助您更快地构建更出色的应用。
您可能对现有的 compose material 库十分了解,它基于 material design 2 规范,其中包括了 material 主题、material 组件和深色主题等功能。新的 compose material 3 jetpack 库现已发布 alpha 版,它基于 material design 3 规范,包括了更新后的主题、组件以及动态配色这类 material you 个性化功能,旨在与新的 android 12 视觉样式和系统界面相得益彰。接下来,我们将使用 jetchat 来说明如何应用 material design 3 和 material you。
jetchat 是一款使用 jetpack compose 构建的示例聊天应用,目前使用 material design 2 中的主题和组件。我们将在 jetchat 中,应用由我们的设计人员提供的 compose material 3 库的更新,其中包括更广泛的色调颜色、对组件的最新更新,甚至包括动态配色以使应用更加个性化,从而使其更加美观。
在开始前,我们首先要将 material 3 的依赖项添加到模块的 build.gradle 文件中:
  implementation 'androidx.compose.material31.0.0-alpha01' materialtheme 我们先来看看 materialtheme。现有的 materialtheme 可组合项是 material design 2 的实现,它通过调整颜色、排版和形状系统,可以在整个应用内实现对 material 2 组件进行主题设置。我们为 material design 3 引入了新版本的 materialtheme,可以通过调整配色方案和排版系统对 material 3 组件的主题进行设置,而更新 shape 的功能也会在不久之后加入。
                  import androidx.compose.material3.materialtheme @composablefun materialtheme ( colorscheme: colorscheme, typography: typography, // 更新 shape 的功能即将到来 content: @composable () -> unit)   首先,我们看一下配色方案。material design 3 将颜色细分到特定名称的颜色槽中。比如 material 3 组件使用的 primary、background 和 error,这些颜色槽共同形成一种配色方案。部分颜色槽来自 material design 2,同时也引入了一些新的颜色槽以扩充整体调色板。这些颜色槽都包含了美观的全新默认基准颜色,在浅色和深色主题上都可以应用。
上面这些颜色取自一组色调调色板,例如,我们来看一下 primary 颜色槽。该颜色槽使用的颜色值来自 primary 色调调色板中的不同色调,并根据浅色和深色主题选择相应的色调,以满足无障碍功能要求。
compose 使用新的 colorscheme 类对此进行建模,其参数以 material design 3 配色方案中的颜色槽命名。您可以使用 lightcolorscheme 函数创建具有浅色基准值的 colorscheme 实例;也可以使用自定义颜色覆盖默认值,或者使用 darkcolorscheme 设置深色默认基准值;您还可以使用 issystemindarktheme 工具函数,根据系统设置在浅色和深色配色方案之间切换。
                                      val applightcolorscheme = lightcolorscheme ( primary = color(...), // secondary、tertiary 等等 // 具有浅色基准值的 colorscheme 实例) val appdarkcolorscheme = darkcolorscheme( // primary、secondary、tertiary 等等 // 具有深色基准值的 colorscheme 实例) val dark = issystemindarktheme()val colorscheme = if (dark) appdarkcolorscheme else applightcolorscheme // 将 colorscheme 作为参数传递给 materialtheme。materialtheme ( colorscheme = colorscheme, // 字型) { // 应用内容}   接下来,我们来看看 jetchat 的配色方案。jetchat 的配色方案由 materialtheme builder 工具生成,我们使用 jetchat 品牌颜色中的蓝色和黄色作为 primary 颜色、secondary 颜色和 tertiary 颜色的来源,生成了非常适合 jetchat 的 material 3 配色方案,其中涵盖了用于浅色和深色主题的颜色。jetchat 所使用的品牌颜色取自 materialtheme builder 工具生成的一组自定义色调调色板,下图中显示了 primary 颜色,即蓝色的色调调色板,以及配色方案中匹配的 primary 颜色槽。
要实现 jetchat 配色方案,首先使用 color 类声明这些颜色。materialtheme builder 工具还可以为您导出生成的代码。接下来,便可以使用相应的颜色值声明 jetchat 浅色和深色配色方案。
                                            // 来自名为'blue'的色调调色盘的 primary 颜色val blue10 = color (0xff000965)val blue20 = color (0xff00159e)val blue30 = color (0xff0023da)val blue40 = color (0xff1e40ff)val blue80 = color (0xffbbc3ff)val blue90 = color (0xffdde0ff) val jetchatlightcolorscheme = lightcolorscheme ( primary = blue40, onprimary = color.white, primarycontainer = blue90, onprimarycontainer = blue10, // secondary、tertiary、surface 等等) val jetchatdarkcolorscheme = darkcolorscheme ( primary = blue80, onprimary = blue20, primarycontainer = blue30, onprimarycontainer = blue90, // secondary、tertiary、surface 等等)   我们为 jetchat 主题创建了一个新的可组合函数,该函数接收一个用于判断深色主题的参数和一个应用内容参数,从而使我们可以在 jetchat 的浅色和深色配色方案之间切换。接下来,我们将 colorscheme 值和 content 传递给内部的 materialtheme 可组合项,这使我们能够封装 jetchat 内容并为应用提供主题。
                    @composablefun jetchattheme ( dark: boolean = issystemindarktheme(), content: @composable () -> unit) { val colorscheme = if (dark) jetchatdarkcolorscheme else jetchatlightcolorscheme materialtheme ( colorscheme = colorscheme, content = content, )}   下面让我们看一下 jetchat 对话界面,界面中的不同部分使用了配色方案中的不同颜色槽。例如,根据用户不同,消息头像的边框颜色使用 primary 颜色或 tertiary 颜色。这里使用 materialtheme.colorscheme 访问主题颜色值。
              @composablefun message(...) { val avatarbordercolor = if (isuserme) {        materialtheme.colorscheme.primary } else { materialtheme.colorscheme.tertiary } ...}   动态配色
接下来,让我们来了解什么是动态配色。动态配色是 material you 的重要部分,即用算法从用户的壁纸中提取自定义颜色并应用于应用和系统界面,您可将此作为起点来生成完整的浅色和深色配色方案。
动态配色可在 android 12 及更高版本中使用,要在 compose 中实现动态 colorscheme,需要首先检查 build.version.sdk。如果动态配色可用,我们便可以设置动态 colorscheme;如果不可用,则可以回退到像以前一样使用 lightcolorscheme 或 darkcolorscheme:
                val dynamic = build.version.sok_int >= build.version_codes.sval colorscheme = if (dynamic) { val context = localcontext.current    // 使用 dynamiclightcolorscheme 函数创建具有浅色动态值的 colorscheme 实例 // 或使用 dynamicdarkcolorscheme 创建具有深色动态值的实例    // 传入 context 以便从 android 系统获取动态配色资源 if (dark) dynamiclightcolorscheme(context) else dynamicdarkcolorscheme(context)} else { // 使用 lightcolorscheme 或者 darkcolorscheme}   目前,jetchat 一直在使用品牌的蓝色配色方案,但我们希望增加对基于壁纸的动态配色方案的支持,以配合用户的个性化调整。在本例中,色调调色板基于壁纸中的颜色生成,而动态配色方案则派生自这些色调调色板,其中包括用于浅色和深色主题的颜色。
为了在 jetchat 中实现这一点,我们首先更新 jetchattheme 为动态配色添加一个新参数,然后使用该动态配色参数设置动态 colorscheme,或者在不可用时回退到品牌的蓝色配色方案。与前面一样将 colorscheme 值和 content 传递给内部的 materialtheme 可组合项。
                                      @composablefun jetchattheme ( dark: boolean = issystemindarktheme (), dynamic: boolean = build. version.sdk_int >= build.version_codes.s, content: @composable () -> unit) { // colorscheme 配置以及 materialtheme val colorscheme = if (dynamic) { val context = localcontext.current if (dark) dynamicdarkcolorscheme(context) else dynamiclightcolorscheme(context) } else { if (dark) jetchatdarkcolor scheme else jetchat light color scheme } materialtheme( colorscheme = colorscheme, content = content, )}   现在,在 android 12 及更高版本上,jetchat 界面可根据用户壁纸自动调整配色,无论是浅色主题还是深色主题都可提供适合品牌的美观体验。
排版
现在我们已经了解了配色方案,接下来让我们来看看排版。material design 3 有了新的字体规格,包括了由 material design 2 适配而来的文本样式。样式的命名和分组简化为显示、大标题、标题、正文和标签;每个分组都有大号、中号和小号字体。
compose 使用新的 typography 类对字体规格进行建模,其参数以 material design 3 字体规格中的样式命名。我们可以使用 roboto 基准值创建一个 typography 实例,用自定义文本样式覆盖默认值,最后将 typography 作为参数传递给 materialtheme。
                                        import androidx.compose.material3.typography class typography ( val displaylarge: textstyle, val displaymedium: textstyle, val displaysmall: textstyle, // headlinelarge、titlemedium、bodysmall 等等) val apptypography = typography ( bodylarge = textstyle(...), // displaylarge、titlemedium、labelsmall 等等 // 使用默认的 roboto 基准值) materialtheme ( typography = apptypography, // colorscheme) { //app content}   我们再来看看 jetchat 的排版。设计人员为我们提供了新的品牌字体规格,用到了自定义字体 montserrat 和 karla:
我们首先使用 fontfamily 类声明这些字体,该类将保存 font 类的实例。我们可以使用字体资源 id 和字体粗细构造 font 类,然后使用 typography 类声明 jetchat 字体样式,并使用 textstyle 类覆盖每个文本样式,包括我们的字体、字号、字体粗细等其他排版值。最后,同样的,将 typography 作为参数传递给 materialtheme:
                                                    val montserratfontfamily = fontfamily ( font(r.font.montserrat_regular), font(r.font montserrat_light, fontweight light), font(r.font.montserrat_semibold, fontweight. semibold)) val karlafontfamily = fontfamily ( font(r.font.karla_regular), font(r.font.karla_bold, fontweight. bold)) val jetchattypography = typography( bodylarge = textstyle( fontfamily = karlafontfamily, fontweight = fontweight. normal, fontsize = 16.sp, lineheight = 24.sp, letterspacing = 0.15.sp ), // titlemedium、labelsmall 等等) materialtheme ( typography = jetchattypography, // colorscheme、content)  
我们来看一下 jetchat 对话界面,界面中的每个部分使用了 jetchat 字体规格中的不同文本样式。例如,消息中的联系人和时间戳,分别使用了 titlemedium 和 labelsmall 样式。它们通过 materialtheme.typography 表示访问主题字体值。
            @composablefun message(...) {    … text (style = materialtheme.typography.titlemedium, ...) … text (style = materialtheme.typography.labelsmall, ...)}  
高度
在了解了 material 3 主题相关的更新后,接下来让我们看看 material design 另一个关键更新——高度。概括来说,material 2 中使用阴影表示高度,而 material 3 中改为使用色调颜色叠加层表示高度。这是一种区分容器和表面的新方式,增加色调高度会使色调变得更为突出。
在 material 2 中高度叠加层是深色主题的一部分,在 material 3 中也已更改为色调颜色叠加层。
我们以 surface 组件为例,surface 是用于支持大多数 material 组件的可组合项,现有的 surface 可组合项实现的是 material design 2 的高度系统。在 material design 2 中 surface 接收一个 elevation 参数并处理深色主题中的阴影和叠加层渲染。我们为 material design 3 引入了新版 surface,它接受一个 tonalelevation 参数,并会在浅色和深色主题中处理色调颜色叠加层渲染。让我们看看前后有何不同:
组件更新
material 3 对许多组件进行了更新,比如按钮、应用栏、对话框、fab 和导航组件。此类更新利用了新的 material 3 主题设置值,并包含了对每个组件规范的最新更新。
例如 material 2 中的 bottomnavigation。它符合 material design 2 规范,并接受 backgroundcolor 和 elevation 等参数。在 material 3 中该可组合项更名为 navigationbar,它符合 material design 3 规范,其中的参数更改为 containercolor 和 tonalelevation,以更准确地反映各自的用途。
                                          // materail 2 中的 navigationbarimport androidx.compose.material.bottomnavigation @composablefun bottomnavigation ( // m2 默认值 backgroundcolor: color, elevation: dp, …) // materail 3 中的 navigationbarimport androidx.compose.material3.navigationbar @composablefun navigationbar ( // m3 默认值 containercolor: color, tonalelevation: dp, …)    
compose material 3 中的组件进行了很多更新,为了让您全面了解所有的组件以及它们的实现方式,我们更新了 compose material catalog 应用,并新增了 material 3 部分。请在 aosp 上查看源代码并在 google play 中下载该应用。
下面我们来看看 jetchat 中的一个例子。在个人资料界面上有一个用于撰写消息的扩展 fab,该组件已从 material 2 更新为 material 3 版本。这是 material 2 版本的一个简单实现,使用了 extendedfloatingactionbutton 可组合项,内部使用了 icon 和 text、可组合项以及自定义的 primary 背景颜色。
import androidx.compose.material.extendedfloatingactionbuttonimport androidx.compose.material.iconimport androidx.compose.material.text extendedfloatingactionbutton( icon = { icon(...) }, text = { text(...) }, backgroundcolor = materialtheme.colors.primary, ...)  
material 3 对该组件的更新如这里所示,可组合项的依赖导入已更改为 material 3,我们使用更名后的 containercolor 参数和 material 3 配色方案中的 tertiary 颜色。
                    import androidx.compose.material3.extendedfloatingactionbuttonimport androidx.compose.material3.iconimport androidx.compose.material3.text extendedfloatingactionbutton( icon = { icon(...) }, text = { text(...) },    containercolor = materialtheme.colorscheme.tertiary, ...)  
视觉效果
material you 的某些方面来自新的 android 12 视觉样式和系统界面,其中的两个重要变化是波纹和滚动效果。现在,波纹效果会在按下时使用细微的闪光照亮表面,滚动效果则会在滚动容器的边缘使用拉伸效果。实现这些更改不需要额外的工作,在 compose foundation 1.1 及更高版本的滚动容器可组合项中拉伸滚动默认处于开启状态;android 12 上提供的闪光波纹适用于所有 material 组件。
                // 拉伸滚动// 适用于 lazycolumn、lazy row、lazyverticalgrid 等组件// composefoundation 1.1.0+ 可用 // 闪光波纹// 适用于所有 material 2 和 material 3 组件// android 12+ 可用  
与 android view 的互操作性改进
与 android 视图的互操作性是使用 compose 开发应用的一个重要部分,我们已经在 material 3 中进行了一些更新来支持这一点。mdc-androidcompose theme adapter 库是一款支持重用 android xml 主题的 material 组件,以方便我们在 jetpack compose 中设置主题。
现有的 mdctheme 可组合项与 material 2 xml 主题兼容,我们还引入了一个新的 mdc3theme 可组合项,它与 material 3 xml 主题兼容。
尾声
现在是在您的 android 应用中试用 compose material 3 的好时机,我们准备了一系列资源来帮助您顺利完成旅程。我们提供了新的关于 compose material 3 的 api 文档,并在 android studio 中提供了新的 empty compose activity 模板,其中包含有关 material 3 的更新。此外,我们还更新了 compose 中的主题设置指南,以及在前面看到的 jetchat 示例和 compose material catalog 应用,以及 mdc-android composetheme adapter 互操作性库。
原文标题:实战 | 在应用中使用 compose material 3
文章出处:【微信公众号:谷歌开发者】欢迎添加关注!文章转载请注明出处。


宇阳科技MLCC产品荣获“第十届中国电子信息博览会创新奖”
RFID智能仓储管理系统为智慧仓储发展奠定了基础
什么原因导致微型减速电动机发热?顺力电机
震荡!乐视超级大佬被曝离职:竟如此回应
智慧环保-污水处理远程监控解决方案
如何应用Material Design 3和Material You
三坐标测量技术的定义及应用范围
色环电感供应商科普0512色环电感超声震荡测试不良的常见原因gujing
机器视觉技术:什么是线阵工业相机
全面屏iPhone8还会有吗?iPhone7、6都被玩出花了,双面屏的苹果见过吗
生产质量管理体系的概念、分类、要求等详解
重庆铁塔助力运营商在重庆市利用5G基站建成了首座交通指示灯杆
IP百科知识之VXLAN
清华大学提出了针对深度学习加速的FPGA虚拟化方案
深度学习:基于语境的文本分类弱监督学习
华为忍痛关闭爱尔兰RF IC设计中心
华为即将向美国公司出售5G技术了吗
杜邦Liveo与意法半导体将合作开发一种新的智能可穿戴设备概念
ASML向中国出口集成电路光刻机持开放态度
环路滤波器参数如何设置?如何设置滤波器的截止频率呢?