android应用图标

Use application drawables with theme attributes to seamlessly support multiple app themes or different build variants.

使用具有主题属性的应用程序可绘制对象无缝支持多个应用程序主题或不同的构建变体。

A lot of things have changed since Android Lollipop was released in regards to how visual assets are handled in apps. In addition to vectorDrawables, which was a huge change to the platform, there are other features that make application theming and styling easier to implement and maintain. This article will focus on using theme attributes with vector drawables to show how to easily support different app branding and multiple themes, like dark mode, in a straightforward way.

自发布Android Lollipop以来,关于在应用程序中处理视觉资产的方式发生了很多变化。 除了vectorDrawables(这是对该平台的巨大更改)之外,还有其他功能使应用程序主题和样式易于实现和维护。 本文将重点讨论将主题属性与矢量可绘制对象一起使用,以展示如何以简单的方式轻松地支持不同的应用程序品牌和多个主题(如深色模式)。

The approach described in this post is what we are currently applying in Tuenti to easily maintain icons in a huge project with multiple brands.

这篇文章中描述的方法是我们目前在Tuenti中应用的方法,以轻松地维护具有多个品牌的大型项目中的图标。

一种资产来统治一切 (One asset to rule them all)

In a nutshell, what you’ll end up with after reading this post are these automagically styled icons!

简而言之,阅读这些文章后最终将得到这些自动样式化的图标!

This post assumes your minSdk is 21 or higher and you’re already using vectorDrawables in your app. If that’s not the case then consider migrating your pngs to vectorDrawables, unless for some diabolical reason you enjoy the hell of dealing with pngs in a multi-density screen world. Seriously, just migrate.

这篇文章假设您的minSdk为21或更高,并且您已经在应用程序中使用vectorDrawables 。 如果不是这种情况,那么请考虑将png迁移到vectorDrawables,除非出于某种令人讨厌的原因,您喜欢在多密度屏幕世界中处理png。 认真地,只是迁移。

给我看代码 (Show me the code)

If you want to jump straight to the code take a look at the sample app. It showcases how to use theme attributes to handle different app themes and dynamically change the appearance of your icons.

如果您想直接跳转到代码,请查看示例应用程序。 它展示了如何使用主题属性来处理不同的应用程序主题并动态更改图标的外观。

什么是主题? (What's a Theme?)

First of all, it’s important to be very clear about the difference between a theme and a style in Android. I highly recommend checking out this awesome series of posts on Android styling by Nick Butcher to learn more. But, in a nutshell, a theme is a collection of named resources that can be referenced later by styles, layouts, views, etc.

首先,很重要的一点是要清楚Android中主题和样式之间的区别。 我强烈建议您查看有关Android样式的一系列精彩文章 通过尼克·巴彻 ( Nick Butcher)了解更多 简而言之, 主题是命名资源的集合,以后可以通过样式,布局,视图等进行引用。

救援的主题属性 (Theme attributes to the rescue)

The named resources mentioned earlier are the properties that define a theme in Android and they’re called theme attributes. Many of the properties are already in the Android platform or Appcompat libraries. You can see the most common ones in this great article. But, if you can’t find a theme attribute that suits your design, you can always define custom ones in your project res/values/attrs.xml file. For example:

前面提到的命名资源是在Android中定义主题的属性,它们称为主题属性 。 许多属性已经在Android平台或Appcompat库中。 您可以在这篇很棒的文章中看到最常见的文章 。 但是,如果找不到适合您设计的主题属性,则始终可以在项目res / values / attrs.xml文件中定义自定义属性。 例如:

<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="BrandColors"><!-- General --><attr name="colorIconPrimaryAlpha" format="color|reference" /></declare-styleable>
</resources>

And then in your res/values/styles.xml file you can reference this new created theme attribute and assign a color value:

然后在res / values / styles.xml文件中,您可以引用此新创建的主题属性并分配颜色值:

<resources><!-- Base application theme. --><style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"><item name="colorAccent">@color/dark_blue</item><item name="colorPrimary">@color/blue</item><item name="colorIconPrimaryAlpha">@color/blue_transparent</item>...</style><resources>

Now, how can theme attributes be used with vectorDrawables so your icons automatically change appearance when the app theme is changed or a different app build variant is compiled?

现在,如何将主题属性与vectorDrawables一起使用,以便在更改应用程序主题或编译其他应用程序构建变体时,图标自动更改外观?

This is frequently how you’ll find most of the vector XML files in an app.

这通常是您在应用程序中找到大多数矢量XML文件的方式。

<vector xmlns:android="http://schemas.android.com/apk/res/android"android:width="24dp"android:height="24dp"android:viewportWidth="24"android:viewportHeight="24"><pathandroid:fillColor="#3282b8"android:pathData="M5,8h14V6H5z"android:strokeAlpha="0.3"android:fillAlpha="0.3"/><pathandroid:fillColor="#3282b8"android:pathData="M19,4h-1L18,2h-2v2L8,4L8,2L6,2v2L5,4c-1.11,0 -1.99,0.9 -1.99,2L3,20c0,1.1 0.89,2 2,2h14c1.1,0 2,-0.9 2,-2L21,6c0,-1.1 -0.9,-2 -2,-2zM19,20L5,20L5,10h14v10zM19,8L5,8L5,6h14v2zM12,13h5v5h-5z"/>
</vector>

Notice how the color for each vector <path> is hardcoded with a specific color like, android:fillColor=”#3282b8". This kind of code should be removed from the vector assets and a semantic color should be used instead. Semantic colors are just theme attributes that represent reusable colors with specific meaning.

请注意,每个向量<path>的颜色是如何使用特定颜色(例如android:fillColor =”#3282b8”)进行硬编码的, 应从向量资源中删除此类代码,而应使用语义颜色只是代表具有特定含义的可重用颜色的主题属性。

The end goal is to define a set of semantic colors that can be referenced from the app’s vector assets, as shown here.

最终目标是定义一组语义颜色,可以从应用程序的矢量资产中引用这些颜色,如下所示。

<vector xmlns:android="http://schemas.android.com/apk/res/android"android:width="24dp"android:height="24dp"android:viewportWidth="24"android:viewportHeight="24"><pathandroid:fillColor="?attr/colorIconPrimaryAlpha"android:pathData="M13.17,6l-0.59,-0.59L11.17,4L6,4v12h16L22,6h-8.83zM17.5,10.5L21,15L7,15l4.5,-6 3.5,4.51 2.5,-3.01z" /><pathandroid:fillColor="?attr/colorPrimary"android:pathData="M2,6L0,6v5h0.01L0,20c0,1.1 0.9,2 2,2h18v-2L2,20L2,6zM7,15h14l-3.5,-4.5 -2.5,3.01L11.5,9zM22,4h-8l-2,-2L6,2c-1.1,0 -1.99,0.9 -1.99,2L4,16c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L24,6c0,-1.1 -0.9,-2 -2,-2zM22,16L6,16L6,4h5.17l1.41,1.41 0.59,0.59L22,6v10z" />
</vector>

With the semantic colors defined, the vector assets will automatically change color whenever the app’s theme is changed to a different primaryColor or another build variant is compiled with a different colors.xml palette.

使用定义的语义颜色,只要将应用程序的主题更改为其他primaryColor或使用不同的colors.xml调色板编译另一个构建变体,矢量资产就会自动更改颜色。

You might be thinking you can achieve the same results by defining colors for your drawables in your layouts or dynamically with code. You wouldn’t be wrong. But that approach has some limitations. Say your code looks like this:

您可能会认为,可以通过在布局中为可绘制对象定义颜色或使用代码动态地获得相同的结果。 你不会错的。 但是这种方法有一些局限性。 假设您的代码如下所示:

<android.support.v7.widget.AppCompatImageView        android:id="@+id/my_appcompat_imageview"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/my_image"        android:tint="@color/blue"/>

or this:

或这个:

val drawable = getResources().getDrawable(R.drawable.my_image)val wrapDrawable = DrawableCompat.wrap(normalDrawable)DrawableCompat.setTint(wrapDrawable, getResources().getColor(R.color.colorPrimaryLight))
  • First of all, coloring assets that way doesn’t’ change the color of the icon. All it does is paint a color on top. That means if you use a color with transparency for the asset you won’t get the results you expect. There are workarounds, of course. But, the point is to keep things as simple as possible.首先,以这种方式为资产着色不会改变图标的​​颜色。 它所做的只是在顶部涂上颜色。 这意味着,如果对资产使用具有透明性的颜色,则不会获得预期的结果。 当然,有解决方法。 但是,关键是要使事情尽可能简单。
  • Secondly, when you color drawables that way you can only use one color. So, if your vectorDrawable has several paths with different colors you won’t be able to change the color for each vector path.其次,当您以这种方式为可绘制对象着色时,只能使用一种颜色。 因此,如果您的vectorDrawable具有多个具有不同颜色的路径,则将无法更改每个向量路径的颜色。

定义一组语义颜色 (Define a set of semantic colors)

The goal is to have a nice set of semantic colors that can be reused in all your app build variants/brands and themes. Something like this:

我们的目标是拥有一套不错的语义颜色,可以在您所有应用程序构建变体/品牌和主题中重复使用。 像这样:

An example semantic color palette by the design team
设计团队的示例语义调色板

This is also great if you work with a design team, because you can define your semantic colors along with them. You can then make your design directly using the semantic palette. Consequently, when your drawable is imported you won’t have to worry if it’s going to be used in dark mode or any other brand variant of your app. Restyling your app becomes a piece of cake.

如果您与设计团队合作,这也很棒,因为您可以定义语义颜色以及它们。 然后,您可以直接使用语义调色板进行设计。 因此,在导入可绘制对象时,您无需担心它是否将在深色模式或应用程序的任何其他品牌变体中使用。 重新设计您的应用程序变得轻而易举。

让我们充满活力! (Let's get dynamic!)

With the semantic colors in place, the vectorDrawables are defined using references to theme attributes. But, what happens when you suddenly need to change the color of an icon at runtime? Let’s say, when an item is clicked. It’s a piece of cake! Just define a theme for the state of your clicked icon and change it dynamically.

保留语义颜色后,使用对主题属性的引用来定义vectorDrawables。 但是,当您突然需要在运行时更改图标的颜色时,会发生什么? 假设点击了某个项目。 很简单的! 只需为单击的图标的状态定义一个主题并动态更改即​​可。

<resources><!-- Put your app themes here --><style name="IconHighlightedTheme" parent="MyAppTheme"><item name="colorPrimary">@color/green</item><item name="colorIconPrimaryAlpha">@color/green_transparent</item></style></resources>

Now from your view, lets say MainActivity.kt:

现在,从您的角度来看,假设MainActivity.kt:

private fun changeSingleIconTheme(imageView: ImageView, @DrawableRes drawableId: Int) {val wrapper = ContextThemeWrapper(this, R.style.IconHighlightedTheme)val drawable = ResourcesCompat.getDrawable(resources, drawableId, wrapper.theme)imageView.setImageDrawable(drawable)
}

And that's it. You can reuse your themes and easily apply them dynamically for specific views on your layout.

就是这样。 您可以重复使用主题,并轻松地将它们动态地应用于布局上的特定视图。

多种口味的应用程序? 没问题 (Multi-flavored app? No problem)

Last but not least. Say you have an app with multiple build variants/brands and they all have different color palettes. When you use this approach you can still have a single instance of your visual assets and you won’t have to add a new one for every new brand.

最后但并非最不重要的。 假设您有一个具有多个构建变体/品牌的应用,并且它们都有不同的调色板。 使用这种方法时,您仍然可以拥有视觉资产的单个实例,而不必为每个新品牌添加一个新实例。

Let’s say there are a couple brands defined as product flavors for the project in your app’s build.gradle.

假设您的应用程序的build.gradle中有几个品牌定义为该项目的产品口味

flavorDimensions "brand"productFlavors {mike {dimension "brand"applicationIdSuffix ".mike"versionNameSuffix "-mike"}odidas {dimension "brand"applicationIdSuffix ".odidas"versionNameSuffix "-odidas"}}

Each brand has a different color palette in the colors.xml file.

每个品牌在colors.xml文件中都有不同的调色板。

Color palettes for every build flavor/brand in your project
项目中每种构建风格/品牌的调色板

When you have the color palettes defined, you just have to redefine the app themes to reference the new colors. So, in each newly created brand folder you just need to add a new file, that can be called something like brand_themes.xml. Then set the app themes to use it for coloring the icons in the different app variants.

定义了调色板后,只需重新定义应用程序主题即可引用新颜色。 因此,在每个新创建的品牌文件夹中,您只需要添加一个新文件即可,该文件可以称为brand_ themes.xml。 然后设置应用程序主题以将其用于为不同应用程序变体中的图标着色。

With everything in place, you can now compile your different app variants and see the results.

一切就绪后,您现在可以编译不同的应用程序变体并查看结果。

Multi brand project compilation
多品牌项目汇编

Android Lint是您的朋友 (Android Lint is your friend)

If you’re convinced this approach is valuable and you’ve realized you don’t need to replicate all your icons for every app theme or brand variant. There are two options for enforcing this new approach through your entire Android project.

如果您确信这种方法很有价值,并且已经意识到不需要为每个应用程序主题或品牌变体复制所有图标。 有两种方法可以在整个Android项目中强制采用这种新方法。

  • Rely on yourself or your teammates to never use hardcoded color values in your assets, layouts, or views. And be hyper vigilant about hardcoded values during code reviews (not the best option).依靠您自己或您的队友,永远不要在资产,布局或视图中使用硬编码的颜色值。 并在代码检查期间对硬编码值保持高度警惕(不是最佳选择)。

or

要么

  • You can use android tooling to detect issues automatically with linting. Check out this great post about using custom linting rules to warn about hardcoded colors in your project. What’s more, you can also detect if you’re using colors like white, blue, red, etc, across your app instead of the semantic colors defined such as colorPrimary, colorDanger, colorWarning, etc.

    您可以使用android工具来自动检测棉绒问题。 查阅有关使用自定义毛刺规则来警告项目中硬编码颜色的精彩文章。 此外,您还可以检测应用中是否使用了白色,蓝色,红色等颜色,而不是使用诸如colorPrimarycolorDangercolorWarning等定义的语义颜色。

还有一件事:VectorCompat! (One more thing: VectorCompat!)

One very important thing when using this approach is to use VectorCompat. For some reason, theme attributes in vectorDrawables don't always work in Android versions 21, 22 and 23. So use VectorCompat and everything will work fine in API 21 and later. Just add the following lines to your apps’s build.gradle file.

使用此方法时,一件非常重要的事情是使用VectorCompat 。 出于某种原因,vectorDrawables中的主题属性在Android 21、22和23版本中并不总是起作用。因此,使用VectorCompat可以在API 21及更高版本中正常工作。 只需将以下行添加到应用程序的build.gradle文件中。

android {    defaultConfig {        vectorDrawables.useSupportLibrary = true}}

And when setting your drawables directly from a layout.xml file use app:srcCompat=”@drawable/yourVector”

当直接从layout.xml文件设置可绘制对象时,请使用app:srcCompat =“ @ drawable / yourVector”

PRO TIP: Bulk drawable import in Android Studio. When a lot of assets are being added to an Android project, having to select the .svg files one by one and import them with the “vector asset” option in Android Studio can get pretty tedious. Recent versions of AS, however, have a new resource manager section with a great feature that went by almost unnoticed: bulk import for vector assets. Check it out!

专业提示: Android Studio中的批量可绘制导入 将大量资产添加到Android项目时,必须选择。 svg文件一个接一个地导入,并在Android Studio中使用“矢量资产”选项导入它们会变得非常乏味。 但是,AS的最新版本具有一个新的资源管理器部分,它具有一个几乎未被注意到的强大功能:矢量资产的批量导入。 看看这个!

翻译自: https://medium.com/makingtuenti/managing-your-android-app-iconography-a2f4aa9cb49d

android应用图标


http://www.taodudu.cc/news/show-6593527.html

相关文章:

  • Android——获取手机应用图标
  • 远程视频通话会议系统EasyRTC进入会议直播间出现黑屏是什么原因?
  • SRS搭建webrtc直播一条龙教学!!!!!!
  • 电脑录屏黑屏怎么回事 电脑录屏黑屏了还能录吗
  • OBS屏幕录制黑屏问题解决
  • 小学计算机·第2册,小学第二册《信息技术》教学计划
  • 《新概念第一册》第一课 Excuse me.
  • 学习新概念第一册 第一堂课 音标和基础知识储备(1)
  • 001 新概念第一册第一课笔记
  • 2018区块链整体架构及应用》(PPT全文)
  • 终于把区块链的技术与应用讲清楚了(57 张 PPT)
  • 64页PPT讲透区块链核心技术在行业的应用
  • 区块链知识系列 - PBFT 共识
  • Overview of Blockchain区块链概述
  • 演讲文档和视频《元宇宙与区块链IT基础设施》下载
  • 九龙证券|受益行业红利,这些龙头股获资金青睐!
  • 第5节 寻找主力踪迹——龙虎榜数据解读
  • 示波器常规使用
  • 如何操作无使用说明书的示波器
  • 示波器的基本使用 USBee逻辑分析仪的使用 USBee AX-Pro示波器的使用
  • 示波器探头有X1和X10档,当测量一个信号时应该如何选择?
  • 硬盘装win10系统教程|不需要任何工具
  • linux硬盘分区工具,Linux安装过程中的硬盘分区工具是()
  • 最简单windows7 硬盘安装工具 nt6 hdd installer v2.8.1,图文、原理
  • Java JPA技术
  • A*与JPS
  • JPCAP监听原理
  • java jpcap使用的注意事项
  • JPA的CRITICAL API
  • Java- jpa

android应用图标_管理您的android应用图标相关推荐

  1. android aar项目_一文了解Android游戏SDK开发

    去年从平安离职之后,加入了一家游戏公司,负责游戏SDK相关的业务开发和维护工作,经过半年来的摸索,对于游戏SDK的开发有了一定的理解,下面就对游戏SDK开发涉及到的知识点进行简单的梳理. SDK SD ...

  2. android实例教程_活动之间的Android意向处理示例教程

    android实例教程 In our last Android Tutorial, we discussed the Android Hello World Application. In this ...

  3. el-table 树形表格 自定义展开图标_耍好控件 | 产品图标体系是如何炼成的?

    温馨提示:建议阅读时间9分钟.文末还为大家准备了网易云音乐Android视觉规范一份,进行学习与参考,记得领取哦~ 前不久我在讲标签栏专题的时候,有聊到过一次图标(可回顾:<了解图标落地,让前端 ...

  4. 怎么更改exe文件的图标_如何修改EXE文件的图标

    怎么更改exe文件的图标 Let's face it: some apps have really ugly icons. Sure, you could always create a shortc ...

  5. android 跳转权限管理的代码,Android权限管理

    Android权限管理 说明 在targetSdkVersion的值为23或者更高,就要进行权限管理,否则如果运行在Android6.0或以上的设备会没有相应权限而导致崩溃 请求权限后,在onRequ ...

  6. android wine教程_技术|如何在 Android 上借助 Wine 来运行 Windows Apps

    Wine(一种 Linux 上的程序,不是你喝的葡萄酒)是在类 Unix 操作系统上运行 Windows 程序的一个自由开源的兼容层.创建于 1993 年,借助它你可以在 Linux 和 macOS ...

  7. android canvas 工作流_行情艰难,Android初中级面试题助你逆风翻盘,每题都有详细答案...

    码个蛋(codeegg) 第 905 次推文 作者:夜猫少年 链接:https://juejin.im/post/5c8211fee51d453a136e36b0 Activity篇 1.说下Acti ...

  8. android 分组柱状图_整理了一个 android 上的波形图及柱状图绘制控件

    SimpleWaveform [说明:以前画过波形图,最近又需要画,略不同,但还得重复写.在网上搜了一下,只找到一个复杂的框架,而我们往往只需要画简单的波形或柱状图.所以我整理提取了过去的代码,有了这 ...

  9. android 录音原始文件_音频采集:Android基于AudioRecord的实现

    前言 这篇文章简单介绍下移动端Android系统下利用AudioRecord进行音频采集方法. 按照惯例开始前先提供一份源码 AudioRecordLib . AudioRecord采集的核心实现在于 ...

最新文章

  1. MegEngine基本概念
  2. C++模板之核心:typename
  3. 判断线段相交 + vector. erase迭代指针 的使用 poj 2653 Pick-up sticks
  4. sql的limit用法
  5. Android实用笔记——使用Spinner实现下拉列表
  6. python3常用模块_Python3 常用模块
  7. 【PAT甲级 - 1013】Battle Over Cities (25分)(并查集)
  8. 重装系统计算机名称回对ug,电脑重装系统后UG软件没有卸载的情况下重新启动UG软件的方法...
  9. 使用requests库实现多线程下载
  10. java实现用户分组,java实现分组算法,根据每组多少人来进行分组
  11. WIN32汇编列表框的使用
  12. pg数据库表存放在哪里_pg数据库系统表
  13. 2016年软件评测师真题精选
  14. docker,deamon.json文件说明
  15. 使用pip无法完全卸载autoitlibrary,提示 we cannot accurately determine which files belong to it which would‘解决办法
  16. 通俗科普:弦论要求空间必须是25维的解释
  17. linux centOS 没有网,怎么办
  18. 上穿均线压力的大阳线特征:
  19. 鸿蒙之至始于衣冠,学坊之声/Class Story | 孝老爱亲,我们这样做——双语303班蚂蚁学坊亲子活动...
  20. tyvj P2018 「Nescafé26」小猫爬山 解题报告

热门文章

  1. Linux分区大小调整
  2. 给孩子们(抄书转贴,80年代看的,影响我一生的文章)
  3. js判断字符长度(包括中文英文和数字)
  4. 基于springBoot的开源运维监控工具——WGCLOUD
  5. 搜索引擎变天了!谷歌宣布开放「生成式搜索平台」!AI 大模型颠覆搜索体验
  6. 船舶电子电气工程专业出来的交响_船舶电子电气工程专业
  7. Python爬虫实例:爬取国内所有医院信息
  8. 网页HTML5制作flex布局骰子,css3 flex布局结合transform生成一个3D骰子
  9. CocosCreator原生开发 dalvik.system.PathClassLoader[DexPathList[[zip file couldn‘t find “libcocos2djs.so“
  10. 基于intel低功耗平台边缘计算解决方案助力半导体设备升级