Working with Drawables

The following capabilities for drawables help you implement material design in your apps:

  • Drawable tinting
  • Prominent color extraction
  • Vector drawables

This lesson shows you how to use these features in your app.

Tint Drawable Resources


With Android 5.0 (API level 21) and above, you can tint bitmaps and nine-patches defined as alpha masks. You can tint them with color resources or theme attributes that resolve to color resources (for example, ?android:attr/colorPrimary). Usually, you create these assets only once and color them automatically to match your theme.

You can apply a tint to BitmapDrawable or NinePatchDrawable objects with the setTint() method. You can also set the tint color and mode in your layouts with the android:tint and android:tintMode attributes.

Extract Prominent Colors from an Image


The Android Support Library r21 and above includes the Palette class, which lets you extract prominent colors from an image. This class extracts the following prominent colors:

  • Vibrant
  • Vibrant dark
  • Vibrant light
  • Muted
  • Muted dark
  • Muted light

To extract these colors, pass a Bitmap object to the Palette.generate() static method in the background thread where you load your images. If you can't use that thread, call the Palette.generateAsync() method and provide a listener instead.

You can retrieve the prominent colors from the image using the getter methods in the Palette class, such as Palette.getVibrantColor.

To use the Palette class in your project, add the following Gradle dependency to your app's module:

dependencies {
    ...
    compile 'com.android.support:palette-v7:21.0.+'
}

For more information, see the API reference for the Palette class.

Create Vector Drawables


Video

Android Vector Graphics

In Android 5.0 (API Level 21) and above, you can define vector drawables, which scale without losing definition. You need only one asset file for a vector image, as opposed to an asset file for each screen density in the case of bitmap images. To create a vector image, you define the details of the shape inside a <vector> XML element.

The following example defines a vector image with the shape of a heart:

<!-- res/drawable/heart.xml -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    <!-- intrinsic size of the drawable -->
    android:height="256dp"
    android:width="256dp"
    <!-- size of the virtual canvas -->
    android:viewportWidth="32"
    android:viewportHeight="32">   <!-- draw a path -->
  <pathandroid: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>

Vector images are represented in Android as VectorDrawable objects. For more information about the pathData syntax, see the SVG Path reference. For more information about animating the properties of vector drawables, see Animating Vector Drawables.

 

Material Design系列第五篇——Working with Drawables的更多相关文章

  1. Material Design系列第七篇——Maintaining Compatibility

    Maintaining Compatibility This lesson teaches you to Define Alternative Styles Provide Alternative L ...

  2. Material Design系列第六篇——Defining Custom Animations

    Defining Custom Animations //自定义动画 This lesson teaches you to //本节课知识点 Customize Touch Feedback //1. ...

  3. Material Design系列第四篇——Defining Shadows and Clipping Views

    Defining Shadows and Clipping Views This lesson teaches you to Assign Elevation to Your Views Custom ...

  4. Material Design系列第三篇——Using the Material Theme

    Using the Material Theme This lesson teaches you to Customize the Color Palette Customize the Status ...

  5. Material Design系列第八篇——Creating Lists and Cards

    Creating Lists and Cards //创建列表和卡片 To create complex lists and cards with material design styles in ...

  6. Android Material Design系列之主题样式介绍说明

    今天这篇文章应该算是Material Design系列的补充篇,因为这篇文章本来应该放到前面讲的,因为讲的是主题嘛,对于一些状态和颜色的介绍,因为我们一新建一个项目时,系统自带了三个属性的颜色,现在就 ...

  7. 前端工程师技能之photoshop巧用系列第五篇——雪碧图

    × 目录 [1]定义 [2]应用场景 [3]合并[4]实现[5]维护 前面的话 前面已经介绍过,描述性图片最终要合并为雪碧图.本文是photoshop巧用系列第五篇——雪碧图 定义 css雪碧图(sp ...

  8. Material Design系列第二篇——Getting Started

    Getting Started This lesson teaches you to Apply the Material Theme Design Your Layouts Specify Elev ...

  9. Material Design系列第一篇——Creating Apps with Material Design

    Creating Apps with Material Design //创建Material Design的App Material design is a comprehensive guide ...

随机推荐

  1. CI框架 -- CLI执行php代码

    什么是CLI: CLI (命令行界面)是一种基于文本的和计算机交互的方式. 更多信息, 请查看 维基百科 . 为什么使用CLI? 虽然不是很明显,但是有很多情况下我们需要使用命令行来运行 CodeIg ...

  2. Lemon OA第1篇:简单介绍

    官方网站:http://www.mossle.com/ 声明:我不是Lemon OA作者,只是分享好的资源.以下内容均代表个人见解~ 从官网说起 知道Lemon OA,也是最近研究Activiti,看 ...

  3. iText中输出中文

    原文链接 http://hintcnuie.iteye.com/blog/183690 转载内容 iText中输出中文,有三种方式: 1.使用iTextAsian.jar中的字体 BaseFont.c ...

  4. Java条形码插件

    项目中需要用条形码插件,基于Java平台的 需要比较简单,根据一个12位的数字生成一个条形码图片即可 之前一直用Google的Zxing,因为功能强大,调用简单,网上也有很多资料 后来发现,Zxing ...

  5. Websphere下删除某个文件(ibm-partialapp-delete.props)

    Websphere下删除某个文件(ibm-partialapp-delete.props)

  6. middle

    /** * Created by lkl on 2017/7/31. *//** * Created by lkl on 2017/6/26. *///spark-shell --driver-cla ...

  7. iOS开发异常处理教程

    以下是两篇xcode开发如何处理异常的教程,建议一读 part 1 part 2 梗概如下: 基本上你能碰到两种崩溃的情况:SIGABRT (也叫EXC_CRASH),和EXC_BAD_ACCESS  ...

  8. electron demo项目npm install安装失败解决办法

    electron官网提供的demo项目,在npm install 的时候总是报错显示安装失败, 解决办法:FQ即可成功安装.

  9. VisualSVN错误 Cannot query proxy blanket解决办法

    最近重新做了一下系统,在安装和使用svn过程中遇到了一些问题,记下备忘. 第一次安装好系统之后,安装VisualSVN遇到报错: Custom action InstallWMISchemaExcut ...

  10. 远程执行命令和文件分发shell脚本

    deploy.conf node01,all,other,datanode,journalnode,zookeeper, node02,all,other,datanode,journalnode,z ...