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. unity--------------------------WheelCollider和小车实验的总结

    WheelCollider总结 写了前面两篇文章,我想总结一下WheelCollider! 让我们能够更清晰的学会物理车的开发! 1.车的层次结构 一般这样分,车身,车身的包围盒,四个轮子和四个轮子的 ...

  2. 学习Unity的步骤

    作者:王选易链接:https://www.zhihu.com/question/23790314/answer/46815232来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  3. 使用OpenSSL创建自己的CA root certificate

    在密码学中,CA(Certificate Authority,认证机构)是指一个被多个用户信任的机构,该机构能够创建和指派公钥证书. 为规范起见,我们先介绍本文可能涉及的术语, asymmetric ...

  4. Linux账号和权限管理

    一. 用户和组的管理  - Linux中用户种类 种类 特点 root 是管理员,拥有至高无上的权限,不受限制,UID为0 普通用户 管理员创建的用户,受权限限制,UID一般从500开始,可以登录系统 ...

  5. C# WinForm下,隐藏主窗体的方法

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  6. ubuntu14.04_64位安装tensorflow-gpu

    第一步(可直接跳到第二步):安装nvidia显卡驱动 linux用户可以通过官方ppa解决安装GPU驱动的问题.使用如下命令添加Graphic Drivers PPA: sudo add-apt-re ...

  7. unity3d 调用Start 注意

    在unity3d中,同一个脚本被绑定到多个物体上的时候,只有active的物体才会调用void Start ()  方法, 如果物体是NO Active 的状态,则不会调用Start,Awake也不会 ...

  8. 带你玩转Eclipse项目转成AndroidStudio项目

    随着Android对Eclipse开发工具的淘汰,越来越多的公司使用AndroidStudio进行相应的Android开发工作.如此,原来用Eclipse开发的项目,怎么导入到AndroidStudi ...

  9. hive 配置元数据以mysql 存储

    <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="confi ...

  10. 谈谈Android中的SurfaceTexture

    2015.7.2更新 由于很多人要代码,我把代码下载链接放在这里了.不过还是要说一下,surfaceTexture和OpenGL ES结合才能发挥出它最大的效果,我这种写法只是我自己的想法,还有很多种 ...