使用Drawables
来源:互联网 发布:知世和艾利欧 编辑:程序博客网 时间:2024/06/05 15:58
该文档翻译自 Android 5.0 文档中对 Material Design 的介绍,原文地址:http://developer.android.com/training/material/drawables.html
Drawables 的以下功能帮助你在你的应用中实现 Material Design:
- 将图像染色
- 提取图像中的显著颜色
- 矢量图像(Vector Drawables)
该课程向你展示如何在你的应用中使用这些特性。
将图像资源染色
在 Android5.0 (API level 21)或以上版本,你可以定义一个 Alpha 遮罩来对位图和 9-patches 染色。你可以使用颜色资源(color resources)或 用于解决颜色资源的主题属性(theme attributes)(例如,?android:attr/colorPrimary)来对它们染色。通常来说,会自动匹配你的主题。
你可以使用 setTint() 方法将染色应用到BitmapDrawable 或 NinePatchDrawable 对象上。你还可以使用 android:tint 和android:tintMode 属性在你的布局中设置染色的颜色和模式
从一张图像中提取显著色彩
Android 支持库21版本及以上包含了 Palette 类,它可以让你从一张图像中提取出其中比较突出的色彩。该类会提取以下显著色彩:
- 鲜艳的色彩 (Vibrant)
- 鲜艳的深色 (Vibrant dark)
- 鲜艳的浅色 (Vibrant light)
- 柔和的色彩 (Muted)
- 柔和的深色 (Muted dark)
- 柔和的浅色 (Muted light)
要提取这些颜色,在你加载图片的后台线程中传递一个 Bitmap 对象到 Palette.generate() 静态方法中。如果你不能使用该线程,你可以调用 Palette.generateAsync() 方法并提供一个监听器作为替代。
你可以使用 Palette类中的 getter 方法获得从图像中提取出的突出色彩,如使用Palette.getVibrantColor方法
要在你的项目中使用 Palette 类,在你的应用模块中使用下面的 Gradle 依赖项。
dependencies { ... compile 'com.android.support:palette-v7:+'}
更多信息,请查看 Palette 类的 API 文档。
创建矢量图形(Vector Drawables)
在 Android 5.0(API level 21)及以上版本,你可以定义矢量图形,其缩放后不会损失清晰度。你只需为一个矢量图形提供一个资源文件,而不是像位图那样为每种屏幕密度都提供一份资源文件。要创建一个矢量图像,你需要在一个 XML 元素<vector> 中定义其形状的详细参数。
下面的示例定义了一个心形的矢量图形:
<!-- res/drawable/heart.xml --><vector xmlns:android="http://schemas.android.com/apk/res/android" <!--Drawable的实际尺寸--> android:height="256dp" android:width="256dp" <!-- 虚拟画布(canvas)的尺寸 --> android:viewportWidth="32" android:viewportHeight="32"> <!-- 绘制路径 --> <path android:fillColor="#8fff" android:pathData="M20.5,9.5 c-1.955,0,-3.83,1.268,-4.5,3 c-0.67,-1.732,-2.547,-3,-4.5,-3 C8.957,9.5,7,11.432,7,14 c0,3.53,3.793,6.257,9,11.5 c5.207,-5.242,9,-7.97,9,-11.5 C25,11.432,23.043,9.5,20.5,9.5z" /></vector>
矢量图形在 Android 中用 VectorDrawable 对象代表。更多有关 pathData 语法的信息,请查看SVG Path reference 。更多有关对矢量图形的属性执行动画的信息,查看 Animating Vector Drawables。
- 使用Drawables
- Drawables
- <七>Drawables的使用(一)
- <九>Drawables的使用(二)---VectorDrawable
- Android Material Design-Working with Drawables(使用Drawable)-(五)
- Android Material Design-Working with Drawables(使用Drawable)-(五)
- Drawables:9-patch
- Canvas and Drawables
- Canvas and Drawables
- 从XML创建Drawables
- android-Canvas and Drawables
- Android 总结 - Canvas & Drawables
- Android Canvas and Drawables (android 图形绘制 canvas 和 Drawables)
- The power of TextView(Drawables)
- android图形系统详解二:Drawables
- Android绘图必杀技---Canvas和Drawables
- The power of TextView (Part 1): Drawables
- Canvas and Drawables 翻译第一集
- linux下vi编辑器的常用命令
- Android系统Recovery工作原理之使用update.zip升级过程分析(三)---Android系统的三种启动模式
- java 1.7新特性
- Android模拟 HTTP multipart/form-data 请求协议信息实现图片上传
- 一亿数字取前N个
- 使用Drawables
- 【问底】徐汉彬:大规模网站架构的缓存机制和几何分形学
- Ceph删除/添加故障OSD(ceph-deploy)
- .pch找不到的解决方法
- JVM 内存溢出 java.lang.OutOfMemoryError: PermGen space myeclipse10
- Android打包之Ant打包
- 两个栈实现队列的功能
- 从头到尾彻底解析Hash表算法
- Java的类名与文件名必须一致