• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1page.title=Working with Drawables
2
3@jd:body
4
5<div id="tb-wrapper">
6<div id="tb">
7<h2>This lesson teaches you to</h2>
8<ol>
9  <li><a href="#DrawableTint">Tint Drawable Resources</a></li>
10  <li><a href="#ColorExtract">Extract Prominent Colors from an Image</a></li>
11  <li><a href="#VectorDrawables">Create Vector Drawables</a></li>
12</ol>
13<h2>You should also read</h2>
14<ul>
15  <li><a href="http://www.google.com/design/spec">Material design specification</a></li>
16  <li><a href="{@docRoot}design/material/index.html">Material design on Android</a></li>
17  <li><a href="{@docRoot}tools/help/vector-asset-studio.html">Vector Asset Studio</a></li>
18  <li><a href="{@docRoot}tools/help/image-asset-studio.html">Image Asset Studio</a></li>
19</ul>
20</div>
21</div>
22
23<p>The following capabilities for drawables help you implement material design in your apps:</p>
24
25<ul>
26<li>Drawable tinting</li>
27<li>Prominent color extraction</li>
28<li>Vector drawables</li>
29</ul>
30
31<p>This lesson shows you how to use these features in your app.</p>
32
33
34<h2 id="DrawableTint">Tint Drawable Resources</h2>
35
36<p>With Android 5.0 (API level 21) and above, you can tint bitmaps and nine-patches defined as
37alpha masks. You can tint them with color resources or theme attributes that resolve to color
38resources (for example, <code>?android:attr/colorPrimary</code>). Usually, you create these assets
39only once and color them automatically to match your theme.</p>
40
41<p>You can apply a tint to {@link android.graphics.drawable.BitmapDrawable}, {@link
42android.graphics.drawable.NinePatchDrawable} or {@link
43android.graphics.drawable.VectorDrawable} objects with the {@code setTint()} method. You can
44also set the tint color and mode in your layouts with the <code>android:tint</code> and
45<code>android:tintMode</code> attributes.</p>
46
47
48<h2 id="ColorExtract">Extract Prominent Colors from an Image</h2>
49
50<p>The Android Support Library r21 and above includes the {@link
51android.support.v7.graphics.Palette} class, which lets you extract prominent colors from an image.
52This class extracts the following prominent colors:</p>
53
54<ul>
55<li>Vibrant</li>
56<li>Vibrant dark</li>
57<li>Vibrant light</li>
58<li>Muted</li>
59<li>Muted dark</li>
60<li>Muted light</li>
61</ul>
62
63<p>To extract these colors, pass a {@link android.graphics.Bitmap} object to the
64{@link android.support.v7.graphics.Palette#generate Palette.generate()} static method in the
65background thread where you load your images. If you can't use that thread, call the
66{@link android.support.v7.graphics.Palette#generateAsync Palette.generateAsync()} method and
67provide a listener instead.</p>
68
69<p>You can retrieve the prominent colors from the image using the getter methods in the
70<code>Palette</code> class, such as <code>Palette.getVibrantColor</code>.</p>
71
72<p>To use the {@link android.support.v7.graphics.Palette} class in your project, add the following
73<a href="{@docRoot}studio/build/index.html#dependencies">Gradle dependency</a> to your
74app's module:</p>
75
76<pre>
77dependencies {
78    ...
79    compile 'com.android.support:palette-v7:21.0.0'
80}
81</pre>
82
83<p>For more information, see the API reference for the {@link android.support.v7.graphics.Palette}
84class.</p>
85
86
87<h2 id="VectorDrawables">Create Vector Drawables</h2>
88
89<!-- video box -->
90<a class="notice-developers-video"
91   href="https://www.youtube.com/watch?v=wlFVIIstKmA"
92   style="margin-top:18px">
93<div>
94    <h3>Video</h3>
95    <p>Android Vector Graphics</p>
96</div>
97</a>
98
99<p>In Android 5.0 (API Level 21) and above, you can define vector drawables, which scale without
100losing definition. You need only one asset file for a vector image, as opposed to an asset file for
101each screen density in the case of bitmap images. To create a vector image, you define the details
102of the shape inside a <code>&lt;vector&gt;</code> XML element.</p>
103
104<p>The following example defines a vector image with the shape of a heart:</p>
105
106<pre>
107&lt;!-- res/drawable/heart.xml -->
108&lt;vector xmlns:android="http://schemas.android.com/apk/res/android"
109    &lt;!-- intrinsic size of the drawable -->
110    android:height="256dp"
111    android:width="256dp"
112    &lt;!-- size of the virtual canvas -->
113    android:viewportWidth="32"
114    android:viewportHeight="32">
115
116  &lt;!-- draw a path -->
117  &lt;path android:fillColor="#8fff"
118      android:pathData="M20.5,9.5
119                        c-1.955,0,-3.83,1.268,-4.5,3
120                        c-0.67,-1.732,-2.547,-3,-4.5,-3
121                        C8.957,9.5,7,11.432,7,14
122                        c0,3.53,3.793,6.257,9,11.5
123                        c5.207,-5.242,9,-7.97,9,-11.5
124                        C25,11.432,23.043,9.5,20.5,9.5z" />
125&lt;/vector>
126</pre>
127
128<p>Vector images are represented in Android as {@link android.graphics.drawable.VectorDrawable}
129objects. For more information about the <code>pathData</code> syntax, see the <a
130href="http://www.w3.org/TR/SVG11/paths.html#PathData">SVG Path reference</a>. For more information
131about animating the properties of vector drawables, see
132<a href="{@docRoot}training/material/animations.html#AnimVector">Animating Vector Drawables</a>.</p>
133