1abb7d134c02ac60091108c491dafb00877093170John Hoford/*
2abb7d134c02ac60091108c491dafb00877093170John Hoford * Copyright (C) 2014 The Android Open Source Project
3abb7d134c02ac60091108c491dafb00877093170John Hoford *
4abb7d134c02ac60091108c491dafb00877093170John Hoford * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5abb7d134c02ac60091108c491dafb00877093170John Hoford * in compliance with the License. You may obtain a copy of the License at
6abb7d134c02ac60091108c491dafb00877093170John Hoford *
7abb7d134c02ac60091108c491dafb00877093170John Hoford * http://www.apache.org/licenses/LICENSE-2.0
8abb7d134c02ac60091108c491dafb00877093170John Hoford *
9abb7d134c02ac60091108c491dafb00877093170John Hoford * Unless required by applicable law or agreed to in writing, software distributed under the License
10abb7d134c02ac60091108c491dafb00877093170John Hoford * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11abb7d134c02ac60091108c491dafb00877093170John Hoford * or implied. See the License for the specific language governing permissions and limitations under
12abb7d134c02ac60091108c491dafb00877093170John Hoford * the License.
13abb7d134c02ac60091108c491dafb00877093170John Hoford */
14abb7d134c02ac60091108c491dafb00877093170John Hoford
15abb7d134c02ac60091108c491dafb00877093170John Hofordpackage android.graphics.drawable;
16abb7d134c02ac60091108c491dafb00877093170John Hoford
17b3c56086d802ae28888dd97ba1f49bd6cee0b673Alan Viveretteimport android.content.res.ColorStateList;
18abb7d134c02ac60091108c491dafb00877093170John Hofordimport android.content.res.Resources;
19abb7d134c02ac60091108c491dafb00877093170John Hofordimport android.content.res.Resources.Theme;
204b3988d159e1c9faa2a7e16c9aca9951264bb429Alan Viveretteimport android.content.res.TypedArray;
21482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghuiimport android.graphics.Bitmap;
22abb7d134c02ac60091108c491dafb00877093170John Hofordimport android.graphics.Canvas;
232af745b56cf4bdfdd9c05d90a4ccc740bf77a4feztenghuiimport android.graphics.Color;
24abb7d134c02ac60091108c491dafb00877093170John Hofordimport android.graphics.ColorFilter;
25abb7d134c02ac60091108c491dafb00877093170John Hofordimport android.graphics.Matrix;
26abb7d134c02ac60091108c491dafb00877093170John Hofordimport android.graphics.Paint;
27abb7d134c02ac60091108c491dafb00877093170John Hofordimport android.graphics.Path;
28abb7d134c02ac60091108c491dafb00877093170John Hofordimport android.graphics.PathMeasure;
29abb7d134c02ac60091108c491dafb00877093170John Hofordimport android.graphics.PixelFormat;
30b3c56086d802ae28888dd97ba1f49bd6cee0b673Alan Viveretteimport android.graphics.PorterDuffColorFilter;
31abb7d134c02ac60091108c491dafb00877093170John Hofordimport android.graphics.Rect;
32abb7d134c02ac60091108c491dafb00877093170John Hofordimport android.graphics.Region;
33b3c56086d802ae28888dd97ba1f49bd6cee0b673Alan Viveretteimport android.graphics.PorterDuff.Mode;
34e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghuiimport android.util.ArrayMap;
35abb7d134c02ac60091108c491dafb00877093170John Hofordimport android.util.AttributeSet;
36738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghuiimport android.util.LayoutDirection;
37abb7d134c02ac60091108c491dafb00877093170John Hofordimport android.util.Log;
38cf4832f69c8786b098ce18c24319021f8cd6733aztenghuiimport android.util.PathParser;
394b1a7c203d5e32c8b2dc7f4f54f28559ca31860aAlan Viveretteimport android.util.Xml;
40abb7d134c02ac60091108c491dafb00877093170John Hoford
41abb7d134c02ac60091108c491dafb00877093170John Hofordimport com.android.internal.R;
42abb7d134c02ac60091108c491dafb00877093170John Hoford
43abb7d134c02ac60091108c491dafb00877093170John Hofordimport org.xmlpull.v1.XmlPullParser;
44abb7d134c02ac60091108c491dafb00877093170John Hofordimport org.xmlpull.v1.XmlPullParserException;
45abb7d134c02ac60091108c491dafb00877093170John Hoford
46abb7d134c02ac60091108c491dafb00877093170John Hofordimport java.io.IOException;
47abb7d134c02ac60091108c491dafb00877093170John Hofordimport java.util.ArrayList;
484d24caf1dec2babf273b18c99638fe2a0635ced4ztenghuiimport java.util.Stack;
494b3988d159e1c9faa2a7e16c9aca9951264bb429Alan Viverette
50abb7d134c02ac60091108c491dafb00877093170John Hoford/**
516beeb75723cec42603b47664bce794a2b97d7bacChet Haase * This lets you create a drawable based on an XML vector graphic. It can be
52d20974bd51852c71114b6e0e4ffdc3613a4d90aaAlan Viverette * defined in an XML file with the <code>&lt;vector></code> element.
53abb7d134c02ac60091108c491dafb00877093170John Hoford * <p/>
5446e546c28fd52b4dedf0a0fbd313db589cb9048bztenghui * The vector drawable has the following elements:
55abb7d134c02ac60091108c491dafb00877093170John Hoford * <p/>
56177dffd869ff1f5bdf816faead01a7dc4980bf75ztenghui * <dt><code>&lt;vector></code></dt>
57a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dl>
58a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dd>Used to defined a vector drawable
59a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dl>
60c3fdffa98136bdad1fee9c77598d541d792920e1ztenghui * <dt><code>android:name</code></dt>
61c3fdffa98136bdad1fee9c77598d541d792920e1ztenghui * <dd>Defines the name of this vector drawable.</dd>
62a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>android:width</code></dt>
63a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dd>Used to defined the intrinsic width of the drawable.
64a95c8abb366d9c39450513335f550b56da13b30aztenghui * This support all the dimension units, normally specified with dp.</dd>
65a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>android:height</code></dt>
66a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dd>Used to defined the intrinsic height the drawable.
67a95c8abb366d9c39450513335f550b56da13b30aztenghui * This support all the dimension units, normally specified with dp.</dd>
68a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>android:viewportWidth</code></dt>
69a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dd>Used to defined the width of the viewport space. Viewport is basically
70a95c8abb366d9c39450513335f550b56da13b30aztenghui * the virtual canvas where the paths are drawn on.</dd>
71a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>android:viewportHeight</code></dt>
72a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dd>Used to defined the height of the viewport space. Viewport is basically
73a95c8abb366d9c39450513335f550b56da13b30aztenghui * the virtual canvas where the paths are drawn on.</dd>
74a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>android:tint</code></dt>
75a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dd>The color to apply to the drawable as a tint. By default, no tint is applied.</dd>
76a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>android:tintMode</code></dt>
77a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dd>The Porter-Duff blending mode for the tint color. The default value is src_in.</dd>
78a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>android:autoMirrored</code></dt>
79a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dd>Indicates if the drawable needs to be mirrored when its layout direction is
80a95c8abb366d9c39450513335f550b56da13b30aztenghui * RTL (right-to-left).</dd>
81c3fdffa98136bdad1fee9c77598d541d792920e1ztenghui * <dt><code>android:alpha</code></dt>
82c3fdffa98136bdad1fee9c77598d541d792920e1ztenghui * <dd>The opacity of this drawable.</dd>
83a95c8abb366d9c39450513335f550b56da13b30aztenghui * </dl></dd>
84a95c8abb366d9c39450513335f550b56da13b30aztenghui * </dl>
85a95c8abb366d9c39450513335f550b56da13b30aztenghui *
86a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dl>
876d9c422de5e0a3890dd723cb1b9264d4507053e9ztenghui * <dt><code>&lt;group></code></dt>
88452f6ece7fe2fd1a85fca53f54e90bf041083b21ztenghui * <dd>Defines a group of paths or subgroups, plus transformation information.
89452f6ece7fe2fd1a85fca53f54e90bf041083b21ztenghui * The transformations are defined in the same coordinates as the viewport.
90a95c8abb366d9c39450513335f550b56da13b30aztenghui * And the transformations are applied in the order of scale, rotate then translate.
91a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dl>
92c3fdffa98136bdad1fee9c77598d541d792920e1ztenghui * <dt><code>android:name</code></dt>
93c3fdffa98136bdad1fee9c77598d541d792920e1ztenghui * <dd>Defines the name of the group.</dd>
94a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>android:rotation</code></dt>
95a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dd>The degrees of rotation of the group.</dd>
96a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>android:pivotX</code></dt>
97a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dd>The X coordinate of the pivot for the scale and rotation of the group.
98a95c8abb366d9c39450513335f550b56da13b30aztenghui * This is defined in the viewport space.</dd>
99a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>android:pivotY</code></dt>
100a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dd>The Y coordinate of the pivot for the scale and rotation of the group.
101a95c8abb366d9c39450513335f550b56da13b30aztenghui * This is defined in the viewport space.</dd>
102a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>android:scaleX</code></dt>
103a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dd>The amount of scale on the X Coordinate.</dd>
104a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>android:scaleY</code></dt>
105a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dd>The amount of scale on the Y coordinate.</dd>
106a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>android:translateX</code></dt>
107a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dd>The amount of translation on the X coordinate.
108a95c8abb366d9c39450513335f550b56da13b30aztenghui * This is defined in the viewport space.</dd>
109a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>android:translateY</code></dt>
110a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dd>The amount of translation on the Y coordinate.
111a95c8abb366d9c39450513335f550b56da13b30aztenghui * This is defined in the viewport space.</dd>
112a95c8abb366d9c39450513335f550b56da13b30aztenghui * </dl></dd>
113a95c8abb366d9c39450513335f550b56da13b30aztenghui * </dl>
114a95c8abb366d9c39450513335f550b56da13b30aztenghui *
115a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dl>
116abb7d134c02ac60091108c491dafb00877093170John Hoford * <dt><code>&lt;path></code></dt>
1176d9c422de5e0a3890dd723cb1b9264d4507053e9ztenghui * <dd>Defines paths to be drawn.
118abb7d134c02ac60091108c491dafb00877093170John Hoford * <dl>
119a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>android:name</code></dt>
120a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dd>Defines the name of the path.</dd>
121a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>android:pathData</code></dt>
12246e546c28fd52b4dedf0a0fbd313db589cb9048bztenghui * <dd>Defines path string. This is using exactly same format as "d" attribute
123a95c8abb366d9c39450513335f550b56da13b30aztenghui * in the SVG's path data. This is defined in the viewport space.</dd>
124a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>android:fillColor</code></dt>
125a70b4742a7a0b2edc57cbbbf83d2c6d43ebc300dztenghui * <dd>Defines the color to fill the path (none if not present).</dd>
126a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>android:strokeColor</code></dt>
127d20974bd51852c71114b6e0e4ffdc3613a4d90aaAlan Viverette * <dd>Defines the color to draw the path outline (none if not present).</dd>
128a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>android:strokeWidth</code></dt>
129a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dd>The width a path stroke.</dd>
1302e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui * <dt><code>android:strokeAlpha</code></dt>
1312e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui * <dd>The opacity of a path stroke.</dd>
1322e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui * <dt><code>android:fillAlpha</code></dt>
1332e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui * <dd>The opacity to fill the path with.</dd>
134a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>android:trimPathStart</code></dt>
135a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dd>The fraction of the path to trim from the start, in the range from 0 to 1.</dd>
136a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>android:trimPathEnd</code></dt>
137a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dd>The fraction of the path to trim from the end, in the range from 0 to 1.</dd>
138a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>android:trimPathOffset</code></dt>
139a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dd>Shift trim region (allows showed region to include the start and end), in the range
140a95c8abb366d9c39450513335f550b56da13b30aztenghui * from 0 to 1.</dd>
141a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>android:strokeLineCap</code></dt>
142a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dd>Sets the linecap for a stroked path: butt, round, square.</dd>
143a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>android:strokeLineJoin</code></dt>
144a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dd>Sets the lineJoin for a stroked path: miter,round,bevel.</dd>
145a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>android:strokeMiterLimit</code></dt>
146a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dd>Sets the Miter limit for a stroked path.</dd>
147a95c8abb366d9c39450513335f550b56da13b30aztenghui * </dl></dd>
148a95c8abb366d9c39450513335f550b56da13b30aztenghui * </dl>
149a95c8abb366d9c39450513335f550b56da13b30aztenghui *
150a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dl>
151a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>&lt;clip-path></code></dt>
152a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dd>Defines path to be the current clip.
153a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dl>
154a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>android:name</code></dt>
155a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dd>Defines the name of the clip path.</dd>
156a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dt><code>android:pathData</code></dt>
157a95c8abb366d9c39450513335f550b56da13b30aztenghui * <dd>Defines clip path string. This is using exactly same format as "d" attribute
158a95c8abb366d9c39450513335f550b56da13b30aztenghui * in the SVG's path data.</dd>
159a95c8abb366d9c39450513335f550b56da13b30aztenghui * </dl></dd>
160abb7d134c02ac60091108c491dafb00877093170John Hoford * </dl>
161a95c8abb366d9c39450513335f550b56da13b30aztenghui * <li>Here is a simple VectorDrawable in this vectordrawable.xml file.
162a95c8abb366d9c39450513335f550b56da13b30aztenghui * <pre>
163a95c8abb366d9c39450513335f550b56da13b30aztenghui * &lt;vector xmlns:android=&quot;http://schemas.android.com/apk/res/android";
164a95c8abb366d9c39450513335f550b56da13b30aztenghui *     android:height=&quot;64dp&quot;
165a95c8abb366d9c39450513335f550b56da13b30aztenghui *     android:width=&quot;64dp&quot;
166a95c8abb366d9c39450513335f550b56da13b30aztenghui *     android:viewportHeight=&quot;600&quot;
167a95c8abb366d9c39450513335f550b56da13b30aztenghui *     android:viewportWidth=&quot;600&quot; &gt;
168a95c8abb366d9c39450513335f550b56da13b30aztenghui *     &lt;group
169a95c8abb366d9c39450513335f550b56da13b30aztenghui *         android:name=&quot;rotationGroup&quot;
170a95c8abb366d9c39450513335f550b56da13b30aztenghui *         android:pivotX=&quot;300.0&quot;
171a95c8abb366d9c39450513335f550b56da13b30aztenghui *         android:pivotY=&quot;300.0&quot;
172a95c8abb366d9c39450513335f550b56da13b30aztenghui *         android:rotation=&quot;45.0&quot; &gt;
173a95c8abb366d9c39450513335f550b56da13b30aztenghui *         &lt;path
174a95c8abb366d9c39450513335f550b56da13b30aztenghui *             android:name=&quot;v&quot;
175a95c8abb366d9c39450513335f550b56da13b30aztenghui *             android:fillColor=&quot;#000000&quot;
176a95c8abb366d9c39450513335f550b56da13b30aztenghui *             android:pathData=&quot;M300,70 l 0,-70 70,70 0,0 -70,70z&quot; /&gt;
177a95c8abb366d9c39450513335f550b56da13b30aztenghui *     &lt;/group&gt;
178a95c8abb366d9c39450513335f550b56da13b30aztenghui * &lt;/vector&gt;
179a95c8abb366d9c39450513335f550b56da13b30aztenghui * </pre></li>
180abb7d134c02ac60091108c491dafb00877093170John Hoford */
181a95c8abb366d9c39450513335f550b56da13b30aztenghui
182abb7d134c02ac60091108c491dafb00877093170John Hofordpublic class VectorDrawable extends Drawable {
1839453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette    private static final String LOGTAG = VectorDrawable.class.getSimpleName();
1849453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette
185a95c8abb366d9c39450513335f550b56da13b30aztenghui    private static final String SHAPE_CLIP_PATH = "clip-path";
1866d9c422de5e0a3890dd723cb1b9264d4507053e9ztenghui    private static final String SHAPE_GROUP = "group";
187abb7d134c02ac60091108c491dafb00877093170John Hoford    private static final String SHAPE_PATH = "path";
188abb7d134c02ac60091108c491dafb00877093170John Hoford    private static final String SHAPE_VECTOR = "vector";
189abb7d134c02ac60091108c491dafb00877093170John Hoford
190abb7d134c02ac60091108c491dafb00877093170John Hoford    private static final int LINECAP_BUTT = 0;
191abb7d134c02ac60091108c491dafb00877093170John Hoford    private static final int LINECAP_ROUND = 1;
192abb7d134c02ac60091108c491dafb00877093170John Hoford    private static final int LINECAP_SQUARE = 2;
1939453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette
194abb7d134c02ac60091108c491dafb00877093170John Hoford    private static final int LINEJOIN_MITER = 0;
195abb7d134c02ac60091108c491dafb00877093170John Hoford    private static final int LINEJOIN_ROUND = 1;
196abb7d134c02ac60091108c491dafb00877093170John Hoford    private static final int LINEJOIN_BEVEL = 2;
1979453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette
1984d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui    private static final boolean DBG_VECTOR_DRAWABLE = false;
1994d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui
20055f765441c6cec6b2990de12f68d1c4ac444d35cztenghui    private VectorDrawableState mVectorState;
201e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
202b3c56086d802ae28888dd97ba1f49bd6cee0b673Alan Viverette    private PorterDuffColorFilter mTintFilter;
203b07b086bd42181f62718a6394b56be3917b12511Alan Viverette    private ColorFilter mColorFilter;
204b3c56086d802ae28888dd97ba1f49bd6cee0b673Alan Viverette
20555f765441c6cec6b2990de12f68d1c4ac444d35cztenghui    private boolean mMutated;
20655f765441c6cec6b2990de12f68d1c4ac444d35cztenghui
207482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui    // AnimatedVectorDrawable needs to turn off the cache all the time, otherwise,
208482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui    // caching the bitmap by default is allowed.
209482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui    private boolean mAllowCaching = true;
210482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui
211abb7d134c02ac60091108c491dafb00877093170John Hoford    public VectorDrawable() {
21255f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        mVectorState = new VectorDrawableState();
213abb7d134c02ac60091108c491dafb00877093170John Hoford    }
214abb7d134c02ac60091108c491dafb00877093170John Hoford
2159453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette    private VectorDrawable(VectorDrawableState state, Resources res, Theme theme) {
21616c1bd5db8f4f18e1eee8b19006bba5f06a88123ztenghui        if (theme != null && state.canApplyTheme()) {
21716c1bd5db8f4f18e1eee8b19006bba5f06a88123ztenghui            // If we need to apply a theme, implicitly mutate.
21816c1bd5db8f4f18e1eee8b19006bba5f06a88123ztenghui            mVectorState = new VectorDrawableState(state);
2199453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette            applyTheme(theme);
22016c1bd5db8f4f18e1eee8b19006bba5f06a88123ztenghui        } else {
22116c1bd5db8f4f18e1eee8b19006bba5f06a88123ztenghui            mVectorState = state;
2229453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette        }
223b3c56086d802ae28888dd97ba1f49bd6cee0b673Alan Viverette
224b3c56086d802ae28888dd97ba1f49bd6cee0b673Alan Viverette        mTintFilter = updateTintFilter(mTintFilter, state.mTint, state.mTintMode);
225abb7d134c02ac60091108c491dafb00877093170John Hoford    }
226abb7d134c02ac60091108c491dafb00877093170John Hoford
22755f765441c6cec6b2990de12f68d1c4ac444d35cztenghui    @Override
22855f765441c6cec6b2990de12f68d1c4ac444d35cztenghui    public Drawable mutate() {
22955f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        if (!mMutated && super.mutate() == this) {
23055f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            mVectorState = new VectorDrawableState(mVectorState);
23155f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            mMutated = true;
23255f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        }
23355f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        return this;
23455f765441c6cec6b2990de12f68d1c4ac444d35cztenghui    }
23555f765441c6cec6b2990de12f68d1c4ac444d35cztenghui
236e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui    Object getTargetByName(String name) {
23755f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        return mVectorState.mVPathRenderer.mVGTargetsMap.get(name);
238e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui    }
239e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
240abb7d134c02ac60091108c491dafb00877093170John Hoford    @Override
241abb7d134c02ac60091108c491dafb00877093170John Hoford    public ConstantState getConstantState() {
24255f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        mVectorState.mChangingConfigurations = getChangingConfigurations();
24355f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        return mVectorState;
244abb7d134c02ac60091108c491dafb00877093170John Hoford    }
245abb7d134c02ac60091108c491dafb00877093170John Hoford
2464554a6a5137d8e9bdfb623ad84ff344a48b7eb9dAlan Viverette    @Override
247abb7d134c02ac60091108c491dafb00877093170John Hoford    public void draw(Canvas canvas) {
2484b3988d159e1c9faa2a7e16c9aca9951264bb429Alan Viverette        final Rect bounds = getBounds();
2491968201b44567262e7b2e382eee3d88990748d89Dan Sandler        if (bounds.width() == 0 || bounds.height() == 0) {
2501968201b44567262e7b2e382eee3d88990748d89Dan Sandler            // too small to draw
2511968201b44567262e7b2e382eee3d88990748d89Dan Sandler            return;
2521968201b44567262e7b2e382eee3d88990748d89Dan Sandler        }
2531968201b44567262e7b2e382eee3d88990748d89Dan Sandler
2540670f029a4e59448aa53b46a98e60dff6404f360ztenghui        final int saveCount = canvas.save();
255738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui        final boolean needMirroring = needMirroring();
256738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui
2574b3988d159e1c9faa2a7e16c9aca9951264bb429Alan Viverette        canvas.translate(bounds.left, bounds.top);
258738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui        if (needMirroring) {
259738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui            canvas.translate(bounds.width(), 0);
260738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui            canvas.scale(-1.0f, 1.0f);
261738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui        }
262482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui
263b07b086bd42181f62718a6394b56be3917b12511Alan Viverette        // Color filters always override tint filters.
264b07b086bd42181f62718a6394b56be3917b12511Alan Viverette        final ColorFilter colorFilter = mColorFilter == null ? mTintFilter : mColorFilter;
265b07b086bd42181f62718a6394b56be3917b12511Alan Viverette
266482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui        if (!mAllowCaching) {
2678490354dc651dd8f25036ef0dda7917fb33013edztenghui            // AnimatedVectorDrawable
2688490354dc651dd8f25036ef0dda7917fb33013edztenghui            if (!mVectorState.hasTranslucentRoot()) {
269b07b086bd42181f62718a6394b56be3917b12511Alan Viverette                mVectorState.mVPathRenderer.draw(
270b07b086bd42181f62718a6394b56be3917b12511Alan Viverette                        canvas, bounds.width(), bounds.height(), colorFilter);
2718490354dc651dd8f25036ef0dda7917fb33013edztenghui            } else {
2728490354dc651dd8f25036ef0dda7917fb33013edztenghui                mVectorState.createCachedBitmapIfNeeded(bounds);
2738490354dc651dd8f25036ef0dda7917fb33013edztenghui                mVectorState.updateCachedBitmap(bounds);
274b07b086bd42181f62718a6394b56be3917b12511Alan Viverette                mVectorState.drawCachedBitmapWithRootAlpha(canvas, colorFilter);
2758490354dc651dd8f25036ef0dda7917fb33013edztenghui            }
276482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui        } else {
2778490354dc651dd8f25036ef0dda7917fb33013edztenghui            // Static Vector Drawable case.
2788490354dc651dd8f25036ef0dda7917fb33013edztenghui            mVectorState.createCachedBitmapIfNeeded(bounds);
2798490354dc651dd8f25036ef0dda7917fb33013edztenghui            if (!mVectorState.canReuseCache()) {
2808490354dc651dd8f25036ef0dda7917fb33013edztenghui                mVectorState.updateCachedBitmap(bounds);
281482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui                mVectorState.updateCacheStates();
282482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui            }
283b07b086bd42181f62718a6394b56be3917b12511Alan Viverette            mVectorState.drawCachedBitmapWithRootAlpha(canvas, colorFilter);
284482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui        }
285738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui
2864b3988d159e1c9faa2a7e16c9aca9951264bb429Alan Viverette        canvas.restoreToCount(saveCount);
287abb7d134c02ac60091108c491dafb00877093170John Hoford    }
288abb7d134c02ac60091108c491dafb00877093170John Hoford
289abb7d134c02ac60091108c491dafb00877093170John Hoford    @Override
290e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui    public int getAlpha() {
291e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        return mVectorState.mVPathRenderer.getRootAlpha();
292e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui    }
293e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
294e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui    @Override
295abb7d134c02ac60091108c491dafb00877093170John Hoford    public void setAlpha(int alpha) {
2962af745b56cf4bdfdd9c05d90a4ccc740bf77a4feztenghui        if (mVectorState.mVPathRenderer.getRootAlpha() != alpha) {
2972af745b56cf4bdfdd9c05d90a4ccc740bf77a4feztenghui            mVectorState.mVPathRenderer.setRootAlpha(alpha);
298abb7d134c02ac60091108c491dafb00877093170John Hoford            invalidateSelf();
299abb7d134c02ac60091108c491dafb00877093170John Hoford        }
300abb7d134c02ac60091108c491dafb00877093170John Hoford    }
301abb7d134c02ac60091108c491dafb00877093170John Hoford
302abb7d134c02ac60091108c491dafb00877093170John Hoford    @Override
303abb7d134c02ac60091108c491dafb00877093170John Hoford    public void setColorFilter(ColorFilter colorFilter) {
304b07b086bd42181f62718a6394b56be3917b12511Alan Viverette        mColorFilter = colorFilter;
305fd6e411767735478ee0f69ba01d77def4c6b2627Alan Viverette        invalidateSelf();
306abb7d134c02ac60091108c491dafb00877093170John Hoford    }
307abb7d134c02ac60091108c491dafb00877093170John Hoford
308abb7d134c02ac60091108c491dafb00877093170John Hoford    @Override
309a426445dfdab43886dd894f2ba8a1d55bfcbb278Alan Viverette    public void setTintList(ColorStateList tint) {
310b3c56086d802ae28888dd97ba1f49bd6cee0b673Alan Viverette        final VectorDrawableState state = mVectorState;
311a426445dfdab43886dd894f2ba8a1d55bfcbb278Alan Viverette        if (state.mTint != tint) {
312b3c56086d802ae28888dd97ba1f49bd6cee0b673Alan Viverette            state.mTint = tint;
313a426445dfdab43886dd894f2ba8a1d55bfcbb278Alan Viverette            mTintFilter = updateTintFilter(mTintFilter, tint, state.mTintMode);
314a426445dfdab43886dd894f2ba8a1d55bfcbb278Alan Viverette            invalidateSelf();
315a426445dfdab43886dd894f2ba8a1d55bfcbb278Alan Viverette        }
316a426445dfdab43886dd894f2ba8a1d55bfcbb278Alan Viverette    }
317b3c56086d802ae28888dd97ba1f49bd6cee0b673Alan Viverette
318a426445dfdab43886dd894f2ba8a1d55bfcbb278Alan Viverette    @Override
319a426445dfdab43886dd894f2ba8a1d55bfcbb278Alan Viverette    public void setTintMode(Mode tintMode) {
320a426445dfdab43886dd894f2ba8a1d55bfcbb278Alan Viverette        final VectorDrawableState state = mVectorState;
321a426445dfdab43886dd894f2ba8a1d55bfcbb278Alan Viverette        if (state.mTintMode != tintMode) {
322a426445dfdab43886dd894f2ba8a1d55bfcbb278Alan Viverette            state.mTintMode = tintMode;
323a426445dfdab43886dd894f2ba8a1d55bfcbb278Alan Viverette            mTintFilter = updateTintFilter(mTintFilter, state.mTint, tintMode);
324b3c56086d802ae28888dd97ba1f49bd6cee0b673Alan Viverette            invalidateSelf();
325b3c56086d802ae28888dd97ba1f49bd6cee0b673Alan Viverette        }
326b3c56086d802ae28888dd97ba1f49bd6cee0b673Alan Viverette    }
327b3c56086d802ae28888dd97ba1f49bd6cee0b673Alan Viverette
328b3c56086d802ae28888dd97ba1f49bd6cee0b673Alan Viverette    @Override
329607bd848269fb802550e63aa61945790616f97a7Alan Viverette    public boolean isStateful() {
330607bd848269fb802550e63aa61945790616f97a7Alan Viverette        return super.isStateful() || (mVectorState != null && mVectorState.mTint != null
331607bd848269fb802550e63aa61945790616f97a7Alan Viverette                && mVectorState.mTint.isStateful());
332607bd848269fb802550e63aa61945790616f97a7Alan Viverette    }
333607bd848269fb802550e63aa61945790616f97a7Alan Viverette
334607bd848269fb802550e63aa61945790616f97a7Alan Viverette    @Override
335b3c56086d802ae28888dd97ba1f49bd6cee0b673Alan Viverette    protected boolean onStateChange(int[] stateSet) {
336b3c56086d802ae28888dd97ba1f49bd6cee0b673Alan Viverette        final VectorDrawableState state = mVectorState;
337b3c56086d802ae28888dd97ba1f49bd6cee0b673Alan Viverette        if (state.mTint != null && state.mTintMode != null) {
338b3c56086d802ae28888dd97ba1f49bd6cee0b673Alan Viverette            mTintFilter = updateTintFilter(mTintFilter, state.mTint, state.mTintMode);
339607bd848269fb802550e63aa61945790616f97a7Alan Viverette            invalidateSelf();
340b3c56086d802ae28888dd97ba1f49bd6cee0b673Alan Viverette            return true;
341b3c56086d802ae28888dd97ba1f49bd6cee0b673Alan Viverette        }
342b3c56086d802ae28888dd97ba1f49bd6cee0b673Alan Viverette        return false;
343b3c56086d802ae28888dd97ba1f49bd6cee0b673Alan Viverette    }
344b3c56086d802ae28888dd97ba1f49bd6cee0b673Alan Viverette
345b3c56086d802ae28888dd97ba1f49bd6cee0b673Alan Viverette    @Override
346abb7d134c02ac60091108c491dafb00877093170John Hoford    public int getOpacity() {
347abb7d134c02ac60091108c491dafb00877093170John Hoford        return PixelFormat.TRANSLUCENT;
348abb7d134c02ac60091108c491dafb00877093170John Hoford    }
349abb7d134c02ac60091108c491dafb00877093170John Hoford
350abb7d134c02ac60091108c491dafb00877093170John Hoford    @Override
351abb7d134c02ac60091108c491dafb00877093170John Hoford    public int getIntrinsicWidth() {
352ddbbb8ae8561c9e61becd03faa40997d76ab5a51ztenghui        return (int) mVectorState.mVPathRenderer.mBaseWidth;
353abb7d134c02ac60091108c491dafb00877093170John Hoford    }
354abb7d134c02ac60091108c491dafb00877093170John Hoford
355abb7d134c02ac60091108c491dafb00877093170John Hoford    @Override
356abb7d134c02ac60091108c491dafb00877093170John Hoford    public int getIntrinsicHeight() {
357ddbbb8ae8561c9e61becd03faa40997d76ab5a51ztenghui        return (int) mVectorState.mVPathRenderer.mBaseHeight;
358abb7d134c02ac60091108c491dafb00877093170John Hoford    }
359abb7d134c02ac60091108c491dafb00877093170John Hoford
360abb7d134c02ac60091108c491dafb00877093170John Hoford    @Override
3619453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette    public boolean canApplyTheme() {
3629453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette        return super.canApplyTheme() || mVectorState != null && mVectorState.canApplyTheme();
3639453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette    }
3649453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette
3659453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette    @Override
3669453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette    public void applyTheme(Theme t) {
3679453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette        super.applyTheme(t);
3689453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette
3699453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette        final VectorDrawableState state = mVectorState;
370607bd848269fb802550e63aa61945790616f97a7Alan Viverette        if (state != null && state.mThemeAttrs != null) {
371607bd848269fb802550e63aa61945790616f97a7Alan Viverette            final TypedArray a = t.resolveAttributes(state.mThemeAttrs, R.styleable.VectorDrawable);
372607bd848269fb802550e63aa61945790616f97a7Alan Viverette            try {
373607bd848269fb802550e63aa61945790616f97a7Alan Viverette                state.mCacheDirty = true;
374607bd848269fb802550e63aa61945790616f97a7Alan Viverette                updateStateFromTypedArray(a);
375607bd848269fb802550e63aa61945790616f97a7Alan Viverette            } catch (XmlPullParserException e) {
376607bd848269fb802550e63aa61945790616f97a7Alan Viverette                throw new RuntimeException(e);
377607bd848269fb802550e63aa61945790616f97a7Alan Viverette            } finally {
378607bd848269fb802550e63aa61945790616f97a7Alan Viverette                a.recycle();
379607bd848269fb802550e63aa61945790616f97a7Alan Viverette            }
380607bd848269fb802550e63aa61945790616f97a7Alan Viverette
381607bd848269fb802550e63aa61945790616f97a7Alan Viverette            mTintFilter = updateTintFilter(mTintFilter, state.mTint, state.mTintMode);
382607bd848269fb802550e63aa61945790616f97a7Alan Viverette        }
383607bd848269fb802550e63aa61945790616f97a7Alan Viverette
384ddbbb8ae8561c9e61becd03faa40997d76ab5a51ztenghui        final VPathRenderer path = state.mVPathRenderer;
3859453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette        if (path != null && path.canApplyTheme()) {
3869453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette            path.applyTheme(t);
3879453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette        }
3889453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette    }
3899453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette
390fd3c4744f265c5277e6e2641a18d5ec3dff19f6bGeorge Mount    /**
391fd3c4744f265c5277e6e2641a18d5ec3dff19f6bGeorge Mount     * The size of a pixel when scaled from the intrinsic dimension to the viewport dimension.
392fd3c4744f265c5277e6e2641a18d5ec3dff19f6bGeorge Mount     * This is used to calculate the path animation accuracy.
393fd3c4744f265c5277e6e2641a18d5ec3dff19f6bGeorge Mount     *
394fd3c4744f265c5277e6e2641a18d5ec3dff19f6bGeorge Mount     * @hide
395fd3c4744f265c5277e6e2641a18d5ec3dff19f6bGeorge Mount     */
396fd3c4744f265c5277e6e2641a18d5ec3dff19f6bGeorge Mount    public float getPixelSize() {
397fd3c4744f265c5277e6e2641a18d5ec3dff19f6bGeorge Mount        if (mVectorState == null && mVectorState.mVPathRenderer == null ||
398fd3c4744f265c5277e6e2641a18d5ec3dff19f6bGeorge Mount                mVectorState.mVPathRenderer.mBaseWidth == 0 ||
399fd3c4744f265c5277e6e2641a18d5ec3dff19f6bGeorge Mount                mVectorState.mVPathRenderer.mBaseHeight == 0 ||
400fd3c4744f265c5277e6e2641a18d5ec3dff19f6bGeorge Mount                mVectorState.mVPathRenderer.mViewportHeight == 0 ||
401fd3c4744f265c5277e6e2641a18d5ec3dff19f6bGeorge Mount                mVectorState.mVPathRenderer.mViewportWidth == 0) {
402fd3c4744f265c5277e6e2641a18d5ec3dff19f6bGeorge Mount            return 1; // fall back to 1:1 pixel mapping.
403fd3c4744f265c5277e6e2641a18d5ec3dff19f6bGeorge Mount        }
404fd3c4744f265c5277e6e2641a18d5ec3dff19f6bGeorge Mount        float intrinsicWidth = mVectorState.mVPathRenderer.mBaseWidth;
405fd3c4744f265c5277e6e2641a18d5ec3dff19f6bGeorge Mount        float intrinsicHeight = mVectorState.mVPathRenderer.mBaseHeight;
406fd3c4744f265c5277e6e2641a18d5ec3dff19f6bGeorge Mount        float viewportWidth = mVectorState.mVPathRenderer.mViewportWidth;
407fd3c4744f265c5277e6e2641a18d5ec3dff19f6bGeorge Mount        float viewportHeight = mVectorState.mVPathRenderer.mViewportHeight;
408fd3c4744f265c5277e6e2641a18d5ec3dff19f6bGeorge Mount        float scaleX = viewportWidth / intrinsicWidth;
409fd3c4744f265c5277e6e2641a18d5ec3dff19f6bGeorge Mount        float scaleY = viewportHeight / intrinsicHeight;
410fd3c4744f265c5277e6e2641a18d5ec3dff19f6bGeorge Mount        return Math.min(scaleX, scaleY);
411fd3c4744f265c5277e6e2641a18d5ec3dff19f6bGeorge Mount    }
412fd3c4744f265c5277e6e2641a18d5ec3dff19f6bGeorge Mount
4134b1a7c203d5e32c8b2dc7f4f54f28559ca31860aAlan Viverette    /** @hide */
4144b1a7c203d5e32c8b2dc7f4f54f28559ca31860aAlan Viverette    public static VectorDrawable create(Resources resources, int rid) {
4154b1a7c203d5e32c8b2dc7f4f54f28559ca31860aAlan Viverette        try {
416874bcd82c223ce58c9d76edcf619b3988c672307ztenghui            final XmlPullParser parser = resources.getXml(rid);
417874bcd82c223ce58c9d76edcf619b3988c672307ztenghui            final AttributeSet attrs = Xml.asAttributeSet(parser);
418874bcd82c223ce58c9d76edcf619b3988c672307ztenghui            int type;
419874bcd82c223ce58c9d76edcf619b3988c672307ztenghui            while ((type=parser.next()) != XmlPullParser.START_TAG &&
420874bcd82c223ce58c9d76edcf619b3988c672307ztenghui                    type != XmlPullParser.END_DOCUMENT) {
421874bcd82c223ce58c9d76edcf619b3988c672307ztenghui                // Empty loop
422874bcd82c223ce58c9d76edcf619b3988c672307ztenghui            }
423874bcd82c223ce58c9d76edcf619b3988c672307ztenghui            if (type != XmlPullParser.START_TAG) {
424874bcd82c223ce58c9d76edcf619b3988c672307ztenghui                throw new XmlPullParserException("No start tag found");
425874bcd82c223ce58c9d76edcf619b3988c672307ztenghui            }
4264b1a7c203d5e32c8b2dc7f4f54f28559ca31860aAlan Viverette
4274b1a7c203d5e32c8b2dc7f4f54f28559ca31860aAlan Viverette            final VectorDrawable drawable = new VectorDrawable();
428874bcd82c223ce58c9d76edcf619b3988c672307ztenghui            drawable.inflate(resources, parser, attrs);
4294b1a7c203d5e32c8b2dc7f4f54f28559ca31860aAlan Viverette
4304b1a7c203d5e32c8b2dc7f4f54f28559ca31860aAlan Viverette            return drawable;
4314b1a7c203d5e32c8b2dc7f4f54f28559ca31860aAlan Viverette        } catch (XmlPullParserException e) {
4324b1a7c203d5e32c8b2dc7f4f54f28559ca31860aAlan Viverette            Log.e(LOGTAG, "parser error", e);
4334b1a7c203d5e32c8b2dc7f4f54f28559ca31860aAlan Viverette        } catch (IOException e) {
4344b1a7c203d5e32c8b2dc7f4f54f28559ca31860aAlan Viverette            Log.e(LOGTAG, "parser error", e);
4354b1a7c203d5e32c8b2dc7f4f54f28559ca31860aAlan Viverette        }
4364b1a7c203d5e32c8b2dc7f4f54f28559ca31860aAlan Viverette        return null;
4374b1a7c203d5e32c8b2dc7f4f54f28559ca31860aAlan Viverette    }
4384b1a7c203d5e32c8b2dc7f4f54f28559ca31860aAlan Viverette
4392af745b56cf4bdfdd9c05d90a4ccc740bf77a4feztenghui    private static int applyAlpha(int color, float alpha) {
4402af745b56cf4bdfdd9c05d90a4ccc740bf77a4feztenghui        int alphaBytes = Color.alpha(color);
4412af745b56cf4bdfdd9c05d90a4ccc740bf77a4feztenghui        color &= 0x00FFFFFF;
4422af745b56cf4bdfdd9c05d90a4ccc740bf77a4feztenghui        color |= ((int) (alphaBytes * alpha)) << 24;
4432af745b56cf4bdfdd9c05d90a4ccc740bf77a4feztenghui        return color;
4442af745b56cf4bdfdd9c05d90a4ccc740bf77a4feztenghui    }
4452af745b56cf4bdfdd9c05d90a4ccc740bf77a4feztenghui
4469cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette    @Override
4479cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette    public void inflate(Resources res, XmlPullParser parser, AttributeSet attrs, Theme theme)
4489cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette            throws XmlPullParserException, IOException {
449a95c8abb366d9c39450513335f550b56da13b30aztenghui        final VectorDrawableState state = mVectorState;
450a95c8abb366d9c39450513335f550b56da13b30aztenghui        final VPathRenderer pathRenderer = new VPathRenderer();
451a95c8abb366d9c39450513335f550b56da13b30aztenghui        state.mVPathRenderer = pathRenderer;
452a95c8abb366d9c39450513335f550b56da13b30aztenghui
453607bd848269fb802550e63aa61945790616f97a7Alan Viverette        final TypedArray a = obtainAttributes(res, theme, attrs, R.styleable.VectorDrawable);
4549cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette        updateStateFromTypedArray(a);
4559cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette        a.recycle();
4569cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette
457a95c8abb366d9c39450513335f550b56da13b30aztenghui        state.mCacheDirty = true;
45855f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        inflateInternal(res, parser, attrs, theme);
4599cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette
4609cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette        mTintFilter = updateTintFilter(mTintFilter, state.mTint, state.mTintMode);
4619cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette    }
4629cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette
463a95c8abb366d9c39450513335f550b56da13b30aztenghui    private void updateStateFromTypedArray(TypedArray a) throws XmlPullParserException {
4649cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette        final VectorDrawableState state = mVectorState;
465a95c8abb366d9c39450513335f550b56da13b30aztenghui        final VPathRenderer pathRenderer = state.mVPathRenderer;
4669cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette
4678e5e11b99fac942122ee2d6cdd30af51564861aeAlan Viverette        // Account for any configuration changes.
4688e5e11b99fac942122ee2d6cdd30af51564861aeAlan Viverette        state.mChangingConfigurations |= a.getChangingConfigurations();
4698e5e11b99fac942122ee2d6cdd30af51564861aeAlan Viverette
4709cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette        // Extract the theme attributes, if any.
4719cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette        state.mThemeAttrs = a.extractThemeAttrs();
4729cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette
4739cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette        final int tintMode = a.getInt(R.styleable.VectorDrawable_tintMode, -1);
4749cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette        if (tintMode != -1) {
4759cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette            state.mTintMode = Drawable.parseTintMode(tintMode, Mode.SRC_IN);
4769cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette        }
4779cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette
4789cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette        final ColorStateList tint = a.getColorStateList(R.styleable.VectorDrawable_tint);
4799cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette        if (tint != null) {
4809cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette            state.mTint = tint;
4819cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette        }
482738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui
483738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui        state.mAutoMirrored = a.getBoolean(
484738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui                R.styleable.VectorDrawable_autoMirrored, state.mAutoMirrored);
485a95c8abb366d9c39450513335f550b56da13b30aztenghui
486a95c8abb366d9c39450513335f550b56da13b30aztenghui        pathRenderer.mViewportWidth = a.getFloat(
487a95c8abb366d9c39450513335f550b56da13b30aztenghui                R.styleable.VectorDrawable_viewportWidth, pathRenderer.mViewportWidth);
488a95c8abb366d9c39450513335f550b56da13b30aztenghui        pathRenderer.mViewportHeight = a.getFloat(
489a95c8abb366d9c39450513335f550b56da13b30aztenghui                R.styleable.VectorDrawable_viewportHeight, pathRenderer.mViewportHeight);
490a95c8abb366d9c39450513335f550b56da13b30aztenghui
491a95c8abb366d9c39450513335f550b56da13b30aztenghui        if (pathRenderer.mViewportWidth <= 0) {
492a95c8abb366d9c39450513335f550b56da13b30aztenghui            throw new XmlPullParserException(a.getPositionDescription() +
4931c4d8b2173bf4b5bbac94748959752599775628dztenghui                    "<vector> tag requires viewportWidth > 0");
494a95c8abb366d9c39450513335f550b56da13b30aztenghui        } else if (pathRenderer.mViewportHeight <= 0) {
495a95c8abb366d9c39450513335f550b56da13b30aztenghui            throw new XmlPullParserException(a.getPositionDescription() +
4961c4d8b2173bf4b5bbac94748959752599775628dztenghui                    "<vector> tag requires viewportHeight > 0");
497a95c8abb366d9c39450513335f550b56da13b30aztenghui        }
498a95c8abb366d9c39450513335f550b56da13b30aztenghui
499a95c8abb366d9c39450513335f550b56da13b30aztenghui        pathRenderer.mBaseWidth = a.getDimension(
500a95c8abb366d9c39450513335f550b56da13b30aztenghui                R.styleable.VectorDrawable_width, pathRenderer.mBaseWidth);
501a95c8abb366d9c39450513335f550b56da13b30aztenghui        pathRenderer.mBaseHeight = a.getDimension(
502a95c8abb366d9c39450513335f550b56da13b30aztenghui                R.styleable.VectorDrawable_height, pathRenderer.mBaseHeight);
503a95c8abb366d9c39450513335f550b56da13b30aztenghui
504a95c8abb366d9c39450513335f550b56da13b30aztenghui        if (pathRenderer.mBaseWidth <= 0) {
505a95c8abb366d9c39450513335f550b56da13b30aztenghui            throw new XmlPullParserException(a.getPositionDescription() +
5061c4d8b2173bf4b5bbac94748959752599775628dztenghui                    "<vector> tag requires width > 0");
507a95c8abb366d9c39450513335f550b56da13b30aztenghui        } else if (pathRenderer.mBaseHeight <= 0) {
508a95c8abb366d9c39450513335f550b56da13b30aztenghui            throw new XmlPullParserException(a.getPositionDescription() +
5091c4d8b2173bf4b5bbac94748959752599775628dztenghui                    "<vector> tag requires height > 0");
510a95c8abb366d9c39450513335f550b56da13b30aztenghui        }
5118490354dc651dd8f25036ef0dda7917fb33013edztenghui
5128490354dc651dd8f25036ef0dda7917fb33013edztenghui        final float alphaInFloat = a.getFloat(R.styleable.VectorDrawable_alpha,
5138490354dc651dd8f25036ef0dda7917fb33013edztenghui                pathRenderer.getAlpha());
5148490354dc651dd8f25036ef0dda7917fb33013edztenghui        pathRenderer.setAlpha(alphaInFloat);
5158490354dc651dd8f25036ef0dda7917fb33013edztenghui
5165004032ebc2aee97c5884b7f91cc33d2f98ae8b5Alan Viverette        final String name = a.getString(R.styleable.VectorDrawable_name);
5175004032ebc2aee97c5884b7f91cc33d2f98ae8b5Alan Viverette        if (name != null) {
5185004032ebc2aee97c5884b7f91cc33d2f98ae8b5Alan Viverette            pathRenderer.mRootName = name;
5195004032ebc2aee97c5884b7f91cc33d2f98ae8b5Alan Viverette            pathRenderer.mVGTargetsMap.put(name, pathRenderer);
5208490354dc651dd8f25036ef0dda7917fb33013edztenghui        }
5219cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette    }
5229cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette
52355f765441c6cec6b2990de12f68d1c4ac444d35cztenghui    private void inflateInternal(Resources res, XmlPullParser parser, AttributeSet attrs,
5248e5e11b99fac942122ee2d6cdd30af51564861aeAlan Viverette            Theme theme) throws XmlPullParserException, IOException {
5258e5e11b99fac942122ee2d6cdd30af51564861aeAlan Viverette        final VectorDrawableState state = mVectorState;
526a95c8abb366d9c39450513335f550b56da13b30aztenghui        final VPathRenderer pathRenderer = state.mVPathRenderer;
527abb7d134c02ac60091108c491dafb00877093170John Hoford        boolean noPathTag = true;
5289453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette
5294d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui        // Use a stack to help to build the group tree.
5304d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui        // The top of the stack is always the current group.
5314d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui        final Stack<VGroup> groupStack = new Stack<VGroup>();
5324d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui        groupStack.push(pathRenderer.mRootGroup);
533abb7d134c02ac60091108c491dafb00877093170John Hoford
534abb7d134c02ac60091108c491dafb00877093170John Hoford        int eventType = parser.getEventType();
535abb7d134c02ac60091108c491dafb00877093170John Hoford        while (eventType != XmlPullParser.END_DOCUMENT) {
536abb7d134c02ac60091108c491dafb00877093170John Hoford            if (eventType == XmlPullParser.START_TAG) {
5379453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette                final String tagName = parser.getName();
5384d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui                final VGroup currentGroup = groupStack.peek();
5394d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui
540abb7d134c02ac60091108c491dafb00877093170John Hoford                if (SHAPE_PATH.equals(tagName)) {
541a95c8abb366d9c39450513335f550b56da13b30aztenghui                    final VFullPath path = new VFullPath();
5429453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette                    path.inflate(res, attrs, theme);
5438d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    currentGroup.mChildren.add(path);
544e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui                    if (path.getPathName() != null) {
54555f765441c6cec6b2990de12f68d1c4ac444d35cztenghui                        pathRenderer.mVGTargetsMap.put(path.getPathName(), path);
546e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui                    }
547abb7d134c02ac60091108c491dafb00877093170John Hoford                    noPathTag = false;
5488e5e11b99fac942122ee2d6cdd30af51564861aeAlan Viverette                    state.mChangingConfigurations |= path.mChangingConfigurations;
549a95c8abb366d9c39450513335f550b56da13b30aztenghui                } else if (SHAPE_CLIP_PATH.equals(tagName)) {
550a95c8abb366d9c39450513335f550b56da13b30aztenghui                    final VClipPath path = new VClipPath();
551a95c8abb366d9c39450513335f550b56da13b30aztenghui                    path.inflate(res, attrs, theme);
552a95c8abb366d9c39450513335f550b56da13b30aztenghui                    currentGroup.mChildren.add(path);
553a95c8abb366d9c39450513335f550b56da13b30aztenghui                    if (path.getPathName() != null) {
554a95c8abb366d9c39450513335f550b56da13b30aztenghui                        pathRenderer.mVGTargetsMap.put(path.getPathName(), path);
555a95c8abb366d9c39450513335f550b56da13b30aztenghui                    }
556a95c8abb366d9c39450513335f550b56da13b30aztenghui                    state.mChangingConfigurations |= path.mChangingConfigurations;
5576d9c422de5e0a3890dd723cb1b9264d4507053e9ztenghui                } else if (SHAPE_GROUP.equals(tagName)) {
5584d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui                    VGroup newChildGroup = new VGroup();
5594d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui                    newChildGroup.inflate(res, attrs, theme);
5608d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    currentGroup.mChildren.add(newChildGroup);
5614d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui                    groupStack.push(newChildGroup);
562e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui                    if (newChildGroup.getGroupName() != null) {
56355f765441c6cec6b2990de12f68d1c4ac444d35cztenghui                        pathRenderer.mVGTargetsMap.put(newChildGroup.getGroupName(),
56455f765441c6cec6b2990de12f68d1c4ac444d35cztenghui                                newChildGroup);
565e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui                    }
5668e5e11b99fac942122ee2d6cdd30af51564861aeAlan Viverette                    state.mChangingConfigurations |= newChildGroup.mChangingConfigurations;
567abb7d134c02ac60091108c491dafb00877093170John Hoford                }
5684d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui            } else if (eventType == XmlPullParser.END_TAG) {
5694d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui                final String tagName = parser.getName();
5704d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui                if (SHAPE_GROUP.equals(tagName)) {
5714d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui                    groupStack.pop();
5724d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui                }
573abb7d134c02ac60091108c491dafb00877093170John Hoford            }
574abb7d134c02ac60091108c491dafb00877093170John Hoford            eventType = parser.next();
575abb7d134c02ac60091108c491dafb00877093170John Hoford        }
5769453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette
5774d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui        // Print the tree out for debug.
5784d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui        if (DBG_VECTOR_DRAWABLE) {
5794d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui            printGroupTree(pathRenderer.mRootGroup, 0);
5806d9c422de5e0a3890dd723cb1b9264d4507053e9ztenghui        }
5816d9c422de5e0a3890dd723cb1b9264d4507053e9ztenghui
582a95c8abb366d9c39450513335f550b56da13b30aztenghui        if (noPathTag) {
5839453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette            final StringBuffer tag = new StringBuffer();
5849453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette
585a95c8abb366d9c39450513335f550b56da13b30aztenghui            if (tag.length() > 0) {
586a95c8abb366d9c39450513335f550b56da13b30aztenghui                tag.append(" or ");
587abb7d134c02ac60091108c491dafb00877093170John Hoford            }
588a95c8abb366d9c39450513335f550b56da13b30aztenghui            tag.append(SHAPE_PATH);
5899453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette
5909453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette            throw new XmlPullParserException("no " + tag + " defined");
591abb7d134c02ac60091108c491dafb00877093170John Hoford        }
592abb7d134c02ac60091108c491dafb00877093170John Hoford    }
593abb7d134c02ac60091108c491dafb00877093170John Hoford
5944d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui    private void printGroupTree(VGroup currentGroup, int level) {
5954d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui        String indent = "";
5968d0d24f7a2b1a59060aa18926984491691d3c667ztenghui        for (int i = 0; i < level; i++) {
5974d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui            indent += "    ";
5984d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui        }
5994d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui        // Print the current node
6008d0d24f7a2b1a59060aa18926984491691d3c667ztenghui        Log.v(LOGTAG, indent + "current group is :" + currentGroup.getGroupName()
6014d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui                + " rotation is " + currentGroup.mRotate);
6028d0d24f7a2b1a59060aa18926984491691d3c667ztenghui        Log.v(LOGTAG, indent + "matrix is :" + currentGroup.getLocalMatrix().toString());
6038d0d24f7a2b1a59060aa18926984491691d3c667ztenghui        // Then print all the children groups
6048d0d24f7a2b1a59060aa18926984491691d3c667ztenghui        for (int i = 0; i < currentGroup.mChildren.size(); i++) {
6058d0d24f7a2b1a59060aa18926984491691d3c667ztenghui            Object child = currentGroup.mChildren.get(i);
6068d0d24f7a2b1a59060aa18926984491691d3c667ztenghui            if (child instanceof VGroup) {
6078d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                printGroupTree((VGroup) child, level + 1);
6088d0d24f7a2b1a59060aa18926984491691d3c667ztenghui            }
6094d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui        }
6104d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui    }
6114d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui
61255f765441c6cec6b2990de12f68d1c4ac444d35cztenghui    @Override
61355f765441c6cec6b2990de12f68d1c4ac444d35cztenghui    public int getChangingConfigurations() {
61455f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        return super.getChangingConfigurations() | mVectorState.mChangingConfigurations;
61555f765441c6cec6b2990de12f68d1c4ac444d35cztenghui    }
61655f765441c6cec6b2990de12f68d1c4ac444d35cztenghui
617482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui    void setAllowCaching(boolean allowCaching) {
618482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui        mAllowCaching = allowCaching;
619482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui    }
620482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui
621738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui    private boolean needMirroring() {
622738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui        return isAutoMirrored() && getLayoutDirection() == LayoutDirection.RTL;
623738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui    }
624738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui
625738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui    @Override
626738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui    public void setAutoMirrored(boolean mirrored) {
627738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui        if (mVectorState.mAutoMirrored != mirrored) {
628738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui            mVectorState.mAutoMirrored = mirrored;
629738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui            invalidateSelf();
630738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui        }
631738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui    }
632738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui
633738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui    @Override
634738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui    public boolean isAutoMirrored() {
635738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui        return mVectorState.mAutoMirrored;
636738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui    }
637738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui
6385c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette    private static class VectorDrawableState extends ConstantState {
6399cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette        int[] mThemeAttrs;
6405c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette        int mChangingConfigurations;
641ddbbb8ae8561c9e61becd03faa40997d76ab5a51ztenghui        VPathRenderer mVPathRenderer;
642607bd848269fb802550e63aa61945790616f97a7Alan Viverette        ColorStateList mTint = null;
643607bd848269fb802550e63aa61945790616f97a7Alan Viverette        Mode mTintMode = DEFAULT_TINT_MODE;
644738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui        boolean mAutoMirrored;
6455c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette
646482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui        Bitmap mCachedBitmap;
647482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui        int[] mCachedThemeAttrs;
648482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui        ColorStateList mCachedTint;
649482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui        Mode mCachedTintMode;
650482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui        int mCachedRootAlpha;
651670689af43358568bf2b5916284f1cac5762f428ztenghui        boolean mCachedAutoMirrored;
652670689af43358568bf2b5916284f1cac5762f428ztenghui        boolean mCacheDirty;
653482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui
654b07b086bd42181f62718a6394b56be3917b12511Alan Viverette        /** Temporary paint object used to draw cached bitmaps. */
655b07b086bd42181f62718a6394b56be3917b12511Alan Viverette        Paint mTempPaint;
656b07b086bd42181f62718a6394b56be3917b12511Alan Viverette
65755f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        // Deep copy for mutate() or implicitly mutate.
6585c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette        public VectorDrawableState(VectorDrawableState copy) {
6595c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette            if (copy != null) {
6609cd14fc710cf81cc6d1c47d599abe349d3aa87dcAlan Viverette                mThemeAttrs = copy.mThemeAttrs;
6615c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette                mChangingConfigurations = copy.mChangingConfigurations;
662ddbbb8ae8561c9e61becd03faa40997d76ab5a51ztenghui                mVPathRenderer = new VPathRenderer(copy.mVPathRenderer);
6636f6578e81c1df207da47e2e1337382341f271206Chet Haase                if (copy.mVPathRenderer.mFillPaint != null) {
6646f6578e81c1df207da47e2e1337382341f271206Chet Haase                    mVPathRenderer.mFillPaint = new Paint(copy.mVPathRenderer.mFillPaint);
6656f6578e81c1df207da47e2e1337382341f271206Chet Haase                }
6666f6578e81c1df207da47e2e1337382341f271206Chet Haase                if (copy.mVPathRenderer.mStrokePaint != null) {
6676f6578e81c1df207da47e2e1337382341f271206Chet Haase                    mVPathRenderer.mStrokePaint = new Paint(copy.mVPathRenderer.mStrokePaint);
6686f6578e81c1df207da47e2e1337382341f271206Chet Haase                }
669b3c56086d802ae28888dd97ba1f49bd6cee0b673Alan Viverette                mTint = copy.mTint;
670b3c56086d802ae28888dd97ba1f49bd6cee0b673Alan Viverette                mTintMode = copy.mTintMode;
671738177caf6a755a59ca6b17bb968be0aa4e8e10fztenghui                mAutoMirrored = copy.mAutoMirrored;
6725c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette            }
6735c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette        }
6745c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette
675b07b086bd42181f62718a6394b56be3917b12511Alan Viverette        public void drawCachedBitmapWithRootAlpha(Canvas canvas, ColorFilter filter) {
6768490354dc651dd8f25036ef0dda7917fb33013edztenghui            // The bitmap's size is the same as the bounds.
677b07b086bd42181f62718a6394b56be3917b12511Alan Viverette            final Paint p = getPaint(filter);
678b07b086bd42181f62718a6394b56be3917b12511Alan Viverette            canvas.drawBitmap(mCachedBitmap, 0, 0, p);
6798490354dc651dd8f25036ef0dda7917fb33013edztenghui        }
6808490354dc651dd8f25036ef0dda7917fb33013edztenghui
6818490354dc651dd8f25036ef0dda7917fb33013edztenghui        public boolean hasTranslucentRoot() {
6828490354dc651dd8f25036ef0dda7917fb33013edztenghui            return mVPathRenderer.getRootAlpha() < 255;
6838490354dc651dd8f25036ef0dda7917fb33013edztenghui        }
6848490354dc651dd8f25036ef0dda7917fb33013edztenghui
6858490354dc651dd8f25036ef0dda7917fb33013edztenghui        /**
6868490354dc651dd8f25036ef0dda7917fb33013edztenghui         * @return null when there is no need for alpha paint.
6878490354dc651dd8f25036ef0dda7917fb33013edztenghui         */
688b07b086bd42181f62718a6394b56be3917b12511Alan Viverette        public Paint getPaint(ColorFilter filter) {
689b07b086bd42181f62718a6394b56be3917b12511Alan Viverette            if (!hasTranslucentRoot() && filter == null) {
690b07b086bd42181f62718a6394b56be3917b12511Alan Viverette                return null;
6918490354dc651dd8f25036ef0dda7917fb33013edztenghui            }
692b07b086bd42181f62718a6394b56be3917b12511Alan Viverette
693b07b086bd42181f62718a6394b56be3917b12511Alan Viverette            if (mTempPaint == null) {
694b07b086bd42181f62718a6394b56be3917b12511Alan Viverette                mTempPaint = new Paint();
695eec6164e6f6178343219bdedcb1e26779fae7f89ztenghui                mTempPaint.setFilterBitmap(true);
696b07b086bd42181f62718a6394b56be3917b12511Alan Viverette            }
697b07b086bd42181f62718a6394b56be3917b12511Alan Viverette            mTempPaint.setAlpha(mVPathRenderer.getRootAlpha());
698b07b086bd42181f62718a6394b56be3917b12511Alan Viverette            mTempPaint.setColorFilter(filter);
699b07b086bd42181f62718a6394b56be3917b12511Alan Viverette            return mTempPaint;
7008490354dc651dd8f25036ef0dda7917fb33013edztenghui        }
7018490354dc651dd8f25036ef0dda7917fb33013edztenghui
7028490354dc651dd8f25036ef0dda7917fb33013edztenghui        public void updateCachedBitmap(Rect bounds) {
7038490354dc651dd8f25036ef0dda7917fb33013edztenghui            mCachedBitmap.eraseColor(Color.TRANSPARENT);
7048490354dc651dd8f25036ef0dda7917fb33013edztenghui            Canvas tmpCanvas = new Canvas(mCachedBitmap);
705b07b086bd42181f62718a6394b56be3917b12511Alan Viverette            mVPathRenderer.draw(tmpCanvas, bounds.width(), bounds.height(), null);
7068490354dc651dd8f25036ef0dda7917fb33013edztenghui        }
7078490354dc651dd8f25036ef0dda7917fb33013edztenghui
7088490354dc651dd8f25036ef0dda7917fb33013edztenghui        public void createCachedBitmapIfNeeded(Rect bounds) {
7098490354dc651dd8f25036ef0dda7917fb33013edztenghui            if (mCachedBitmap == null || !canReuseBitmap(bounds.width(),
7108490354dc651dd8f25036ef0dda7917fb33013edztenghui                    bounds.height())) {
7118490354dc651dd8f25036ef0dda7917fb33013edztenghui                mCachedBitmap = Bitmap.createBitmap(bounds.width(), bounds.height(),
7128490354dc651dd8f25036ef0dda7917fb33013edztenghui                        Bitmap.Config.ARGB_8888);
7139c13ed366f205d94b2f3f4ddc96b55b1b9faadbaztenghui                mCacheDirty = true;
7148490354dc651dd8f25036ef0dda7917fb33013edztenghui            }
7158490354dc651dd8f25036ef0dda7917fb33013edztenghui
7168490354dc651dd8f25036ef0dda7917fb33013edztenghui        }
7178490354dc651dd8f25036ef0dda7917fb33013edztenghui
7188490354dc651dd8f25036ef0dda7917fb33013edztenghui        public boolean canReuseBitmap(int width, int height) {
7198490354dc651dd8f25036ef0dda7917fb33013edztenghui            if (width == mCachedBitmap.getWidth()
7208490354dc651dd8f25036ef0dda7917fb33013edztenghui                    && height == mCachedBitmap.getHeight()) {
7218490354dc651dd8f25036ef0dda7917fb33013edztenghui                return true;
7228490354dc651dd8f25036ef0dda7917fb33013edztenghui            }
7238490354dc651dd8f25036ef0dda7917fb33013edztenghui            return false;
7248490354dc651dd8f25036ef0dda7917fb33013edztenghui        }
7258490354dc651dd8f25036ef0dda7917fb33013edztenghui
7268490354dc651dd8f25036ef0dda7917fb33013edztenghui        public boolean canReuseCache() {
727670689af43358568bf2b5916284f1cac5762f428ztenghui            if (!mCacheDirty
728670689af43358568bf2b5916284f1cac5762f428ztenghui                    && mCachedThemeAttrs == mThemeAttrs
729482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui                    && mCachedTint == mTint
730482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui                    && mCachedTintMode == mTintMode
731670689af43358568bf2b5916284f1cac5762f428ztenghui                    && mCachedAutoMirrored == mAutoMirrored
7328490354dc651dd8f25036ef0dda7917fb33013edztenghui                    && mCachedRootAlpha == mVPathRenderer.getRootAlpha()) {
733482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui                return true;
734482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui            }
735482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui            return false;
736482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui        }
737482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui
738482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui        public void updateCacheStates() {
739482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui            // Use shallow copy here and shallow comparison in canReuseCache(),
740482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui            // likely hit cache miss more, but practically not much difference.
741482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui            mCachedThemeAttrs = mThemeAttrs;
742482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui            mCachedTint = mTint;
743482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui            mCachedTintMode = mTintMode;
744482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui            mCachedRootAlpha = mVPathRenderer.getRootAlpha();
745670689af43358568bf2b5916284f1cac5762f428ztenghui            mCachedAutoMirrored = mAutoMirrored;
746670689af43358568bf2b5916284f1cac5762f428ztenghui            mCacheDirty = false;
747482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui        }
748482eb53c8a7d5180a2d0ce3b0a779c18960aa8cdztenghui
749607bd848269fb802550e63aa61945790616f97a7Alan Viverette        @Override
750607bd848269fb802550e63aa61945790616f97a7Alan Viverette        public boolean canApplyTheme() {
751607bd848269fb802550e63aa61945790616f97a7Alan Viverette            return super.canApplyTheme() || mThemeAttrs != null
752607bd848269fb802550e63aa61945790616f97a7Alan Viverette                    || (mVPathRenderer != null && mVPathRenderer.canApplyTheme());
753607bd848269fb802550e63aa61945790616f97a7Alan Viverette        }
754607bd848269fb802550e63aa61945790616f97a7Alan Viverette
75555f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        public VectorDrawableState() {
75655f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            mVPathRenderer = new VPathRenderer();
75755f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        }
75855f765441c6cec6b2990de12f68d1c4ac444d35cztenghui
7595c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette        @Override
7605c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette        public Drawable newDrawable() {
7615c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette            return new VectorDrawable(this, null, null);
7625c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette        }
7635c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette
7645c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette        @Override
7655c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette        public Drawable newDrawable(Resources res) {
7665c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette            return new VectorDrawable(this, res, null);
7675c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette        }
7685c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette
7695c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette        @Override
7705c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette        public Drawable newDrawable(Resources res, Theme theme) {
7715c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette            return new VectorDrawable(this, res, theme);
7725c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette        }
7735c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette
7745c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette        @Override
7755c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette        public int getChangingConfigurations() {
7765c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette            return mChangingConfigurations;
7775c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette        }
7785c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette    }
7795c981a100ef30d2925588b69f8b9fe349ee5bfdeAlan Viverette
780ddbbb8ae8561c9e61becd03faa40997d76ab5a51ztenghui    private static class VPathRenderer {
7814d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui        /* Right now the internal data structure is organized as a tree.
7824d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui         * Each node can be a group node, or a path.
7834d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui         * A group node can have groups or paths as children, but a path node has
7844d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui         * no children.
7854d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui         * One example can be:
7864d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui         *                 Root Group
7874d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui         *                /    |     \
7884d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui         *           Group    Path    Group
7894d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui         *          /     \             |
7904d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui         *         Path   Path         Path
7914d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui         *
7924d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui         */
79355f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        // Variables that only used temporarily inside the draw() call, so there
79455f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        // is no need for deep copying.
7956f6578e81c1df207da47e2e1337382341f271206Chet Haase        private final Path mPath;
7966f6578e81c1df207da47e2e1337382341f271206Chet Haase        private final Path mRenderPath;
7974d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui        private static final Matrix IDENTITY_MATRIX = new Matrix();
79855f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        private final Matrix mFinalPathMatrix = new Matrix();
7994b3988d159e1c9faa2a7e16c9aca9951264bb429Alan Viverette
8004b3988d159e1c9faa2a7e16c9aca9951264bb429Alan Viverette        private Paint mStrokePaint;
8014b3988d159e1c9faa2a7e16c9aca9951264bb429Alan Viverette        private Paint mFillPaint;
802abb7d134c02ac60091108c491dafb00877093170John Hoford        private PathMeasure mPathMeasure;
8034b3988d159e1c9faa2a7e16c9aca9951264bb429Alan Viverette
80455f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        /////////////////////////////////////////////////////
80555f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        // Variables below need to be copied (deep copy if applicable) for mutation.
8068e5e11b99fac942122ee2d6cdd30af51564861aeAlan Viverette        private int mChangingConfigurations;
80755f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        private final VGroup mRootGroup;
808a95c8abb366d9c39450513335f550b56da13b30aztenghui        float mBaseWidth = 0;
809a95c8abb366d9c39450513335f550b56da13b30aztenghui        float mBaseHeight = 0;
810a95c8abb366d9c39450513335f550b56da13b30aztenghui        float mViewportWidth = 0;
811a95c8abb366d9c39450513335f550b56da13b30aztenghui        float mViewportHeight = 0;
8128490354dc651dd8f25036ef0dda7917fb33013edztenghui        int mRootAlpha = 0xFF;
8138490354dc651dd8f25036ef0dda7917fb33013edztenghui        String mRootName = null;
8149453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette
81555f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        final ArrayMap<String, Object> mVGTargetsMap = new ArrayMap<String, Object>();
816abb7d134c02ac60091108c491dafb00877093170John Hoford
817ddbbb8ae8561c9e61becd03faa40997d76ab5a51ztenghui        public VPathRenderer() {
8184d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui            mRootGroup = new VGroup();
8196f6578e81c1df207da47e2e1337382341f271206Chet Haase            mPath = new Path();
8206f6578e81c1df207da47e2e1337382341f271206Chet Haase            mRenderPath = new Path();
821abb7d134c02ac60091108c491dafb00877093170John Hoford        }
8229453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette
8232af745b56cf4bdfdd9c05d90a4ccc740bf77a4feztenghui        public void setRootAlpha(int alpha) {
8242af745b56cf4bdfdd9c05d90a4ccc740bf77a4feztenghui            mRootAlpha = alpha;
8252af745b56cf4bdfdd9c05d90a4ccc740bf77a4feztenghui        }
8262af745b56cf4bdfdd9c05d90a4ccc740bf77a4feztenghui
8272af745b56cf4bdfdd9c05d90a4ccc740bf77a4feztenghui        public int getRootAlpha() {
8282af745b56cf4bdfdd9c05d90a4ccc740bf77a4feztenghui            return mRootAlpha;
8292af745b56cf4bdfdd9c05d90a4ccc740bf77a4feztenghui        }
8302af745b56cf4bdfdd9c05d90a4ccc740bf77a4feztenghui
8318490354dc651dd8f25036ef0dda7917fb33013edztenghui        // setAlpha() and getAlpha() are used mostly for animation purpose, since
8328490354dc651dd8f25036ef0dda7917fb33013edztenghui        // Animator like to use alpha from 0 to 1.
8338490354dc651dd8f25036ef0dda7917fb33013edztenghui        public void setAlpha(float alpha) {
8348490354dc651dd8f25036ef0dda7917fb33013edztenghui            setRootAlpha((int) (alpha * 255));
8358490354dc651dd8f25036ef0dda7917fb33013edztenghui        }
8368490354dc651dd8f25036ef0dda7917fb33013edztenghui
8378490354dc651dd8f25036ef0dda7917fb33013edztenghui        @SuppressWarnings("unused")
8388490354dc651dd8f25036ef0dda7917fb33013edztenghui        public float getAlpha() {
8398490354dc651dd8f25036ef0dda7917fb33013edztenghui            return getRootAlpha() / 255.0f;
8408490354dc651dd8f25036ef0dda7917fb33013edztenghui        }
8418490354dc651dd8f25036ef0dda7917fb33013edztenghui
842ddbbb8ae8561c9e61becd03faa40997d76ab5a51ztenghui        public VPathRenderer(VPathRenderer copy) {
84355f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            mRootGroup = new VGroup(copy.mRootGroup, mVGTargetsMap);
8446f6578e81c1df207da47e2e1337382341f271206Chet Haase            mPath = new Path(copy.mPath);
8456f6578e81c1df207da47e2e1337382341f271206Chet Haase            mRenderPath = new Path(copy.mRenderPath);
846abb7d134c02ac60091108c491dafb00877093170John Hoford            mBaseWidth = copy.mBaseWidth;
847abb7d134c02ac60091108c491dafb00877093170John Hoford            mBaseHeight = copy.mBaseHeight;
8485eb5cde467081d1af628e5463ba55f110265a86fztenghui            mViewportWidth = copy.mViewportWidth;
849abb7d134c02ac60091108c491dafb00877093170John Hoford            mViewportHeight = copy.mViewportHeight;
8508e5e11b99fac942122ee2d6cdd30af51564861aeAlan Viverette            mChangingConfigurations = copy.mChangingConfigurations;
85155f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            mRootAlpha = copy.mRootAlpha;
8528490354dc651dd8f25036ef0dda7917fb33013edztenghui            mRootName = copy.mRootName;
8538490354dc651dd8f25036ef0dda7917fb33013edztenghui            if (copy.mRootName != null) {
8548490354dc651dd8f25036ef0dda7917fb33013edztenghui                mVGTargetsMap.put(copy.mRootName, this);
8558490354dc651dd8f25036ef0dda7917fb33013edztenghui            }
856abb7d134c02ac60091108c491dafb00877093170John Hoford        }
857abb7d134c02ac60091108c491dafb00877093170John Hoford
8589453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette        public boolean canApplyTheme() {
8594d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui            // If one of the paths can apply theme, then return true;
8604d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui            return recursiveCanApplyTheme(mRootGroup);
8614d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui        }
8624d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui
8634d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui        private boolean recursiveCanApplyTheme(VGroup currentGroup) {
8644d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui            // We can do a tree traverse here, if there is one path return true,
8654d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui            // then we return true for the whole tree.
8668d0d24f7a2b1a59060aa18926984491691d3c667ztenghui            final ArrayList<Object> children = currentGroup.mChildren;
8678d0d24f7a2b1a59060aa18926984491691d3c667ztenghui
8688d0d24f7a2b1a59060aa18926984491691d3c667ztenghui            for (int i = 0; i < children.size(); i++) {
8698d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                Object child = children.get(i);
8708d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                if (child instanceof VGroup) {
8718d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    VGroup childGroup = (VGroup) child;
8728d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    if (childGroup.canApplyTheme()
8738d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                            || recursiveCanApplyTheme(childGroup)) {
8748d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                        return true;
8758d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    }
8768d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                } else if (child instanceof VPath) {
8778d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    VPath childPath = (VPath) child;
8788d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    if (childPath.canApplyTheme()) {
8798d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                        return true;
8808d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    }
8814d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui                }
8824d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui            }
8839453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette            return false;
8849453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette        }
8859453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette
8869453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette        public void applyTheme(Theme t) {
8874d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui            // Apply theme to every path of the tree.
8884d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui            recursiveApplyTheme(mRootGroup, t);
8894d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui        }
8904d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui
8914d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui        private void recursiveApplyTheme(VGroup currentGroup, Theme t) {
8924d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui            // We can do a tree traverse here, apply theme to all paths which
8934d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui            // can apply theme.
8948d0d24f7a2b1a59060aa18926984491691d3c667ztenghui            final ArrayList<Object> children = currentGroup.mChildren;
8958d0d24f7a2b1a59060aa18926984491691d3c667ztenghui            for (int i = 0; i < children.size(); i++) {
8968d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                Object child = children.get(i);
8978d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                if (child instanceof VGroup) {
8988d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    VGroup childGroup = (VGroup) child;
8998d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    if (childGroup.canApplyTheme()) {
9008d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                        childGroup.applyTheme(t);
9018d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    }
9028d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    recursiveApplyTheme(childGroup, t);
9038d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                } else if (child instanceof VPath) {
9048d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    VPath childPath = (VPath) child;
9058d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    if (childPath.canApplyTheme()) {
9068d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                        childPath.applyTheme(t);
9078d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    }
9084d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui                }
9094d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui            }
910abb7d134c02ac60091108c491dafb00877093170John Hoford        }
911abb7d134c02ac60091108c491dafb00877093170John Hoford
9124d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui        private void drawGroupTree(VGroup currentGroup, Matrix currentMatrix,
913b07b086bd42181f62718a6394b56be3917b12511Alan Viverette                Canvas canvas, int w, int h, ColorFilter filter) {
9144d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui            // Calculate current group's matrix by preConcat the parent's and
9154d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui            // and the current one on the top of the stack.
9164d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui            // Basically the Mfinal = Mviewport * M0 * M1 * M2;
9174d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui            // Mi the local matrix at level i of the group tree.
9184d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui            currentGroup.mStackedMatrix.set(currentMatrix);
9194d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui
9204d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui            currentGroup.mStackedMatrix.preConcat(currentGroup.mLocalMatrix);
9214d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui
9228d0d24f7a2b1a59060aa18926984491691d3c667ztenghui            // Draw the group tree in the same order as the XML file.
9238d0d24f7a2b1a59060aa18926984491691d3c667ztenghui            for (int i = 0; i < currentGroup.mChildren.size(); i++) {
9248d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                Object child = currentGroup.mChildren.get(i);
9258d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                if (child instanceof VGroup) {
9268d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    VGroup childGroup = (VGroup) child;
9278d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    drawGroupTree(childGroup, currentGroup.mStackedMatrix,
928b07b086bd42181f62718a6394b56be3917b12511Alan Viverette                            canvas, w, h, filter);
9298d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                } else if (child instanceof VPath) {
9308d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    VPath childPath = (VPath) child;
931b07b086bd42181f62718a6394b56be3917b12511Alan Viverette                    drawPath(currentGroup, childPath, canvas, w, h, filter);
9328d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                }
933abb7d134c02ac60091108c491dafb00877093170John Hoford            }
9344d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui        }
935abb7d134c02ac60091108c491dafb00877093170John Hoford
936b07b086bd42181f62718a6394b56be3917b12511Alan Viverette        public void draw(Canvas canvas, int w, int h, ColorFilter filter) {
9374d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui            // Travese the tree in pre-order to draw.
938b07b086bd42181f62718a6394b56be3917b12511Alan Viverette            drawGroupTree(mRootGroup, IDENTITY_MATRIX, canvas, w, h, filter);
939abb7d134c02ac60091108c491dafb00877093170John Hoford        }
940abb7d134c02ac60091108c491dafb00877093170John Hoford
941b07b086bd42181f62718a6394b56be3917b12511Alan Viverette        private void drawPath(VGroup vGroup, VPath vPath, Canvas canvas, int w, int h,
942b07b086bd42181f62718a6394b56be3917b12511Alan Viverette                ColorFilter filter) {
9438490354dc651dd8f25036ef0dda7917fb33013edztenghui            final float scaleX = w / mViewportWidth;
9449d59a76dd0e850558e83ef1a48fefd865fac1336ztenghui            final float scaleY = h / mViewportHeight;
9459d59a76dd0e850558e83ef1a48fefd865fac1336ztenghui            final float minScale = Math.min(scaleX, scaleY);
946abb7d134c02ac60091108c491dafb00877093170John Hoford
9474d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui            mFinalPathMatrix.set(vGroup.mStackedMatrix);
9485a836f74df027bb568da17fbde4e641b6a56d2a9ztenghui            mFinalPathMatrix.postScale(scaleX, scaleY);
949abb7d134c02ac60091108c491dafb00877093170John Hoford
9508d0d24f7a2b1a59060aa18926984491691d3c667ztenghui            vPath.toPath(mPath);
9518d0d24f7a2b1a59060aa18926984491691d3c667ztenghui            final Path path = mPath;
952abb7d134c02ac60091108c491dafb00877093170John Hoford
9538d0d24f7a2b1a59060aa18926984491691d3c667ztenghui            mRenderPath.reset();
9549453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette
955a95c8abb366d9c39450513335f550b56da13b30aztenghui            if (vPath.isClipPath()) {
956a95c8abb366d9c39450513335f550b56da13b30aztenghui                mRenderPath.addPath(path, mFinalPathMatrix);
9578d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                canvas.clipPath(mRenderPath, Region.Op.REPLACE);
9588d0d24f7a2b1a59060aa18926984491691d3c667ztenghui            } else {
959a95c8abb366d9c39450513335f550b56da13b30aztenghui                VFullPath fullPath = (VFullPath) vPath;
960a95c8abb366d9c39450513335f550b56da13b30aztenghui                if (fullPath.mTrimPathStart != 0.0f || fullPath.mTrimPathEnd != 1.0f) {
961a95c8abb366d9c39450513335f550b56da13b30aztenghui                    float start = (fullPath.mTrimPathStart + fullPath.mTrimPathOffset) % 1.0f;
962a95c8abb366d9c39450513335f550b56da13b30aztenghui                    float end = (fullPath.mTrimPathEnd + fullPath.mTrimPathOffset) % 1.0f;
963a95c8abb366d9c39450513335f550b56da13b30aztenghui
964a95c8abb366d9c39450513335f550b56da13b30aztenghui                    if (mPathMeasure == null) {
965a95c8abb366d9c39450513335f550b56da13b30aztenghui                        mPathMeasure = new PathMeasure();
966a95c8abb366d9c39450513335f550b56da13b30aztenghui                    }
967a95c8abb366d9c39450513335f550b56da13b30aztenghui                    mPathMeasure.setPath(mPath, false);
968a95c8abb366d9c39450513335f550b56da13b30aztenghui
969a95c8abb366d9c39450513335f550b56da13b30aztenghui                    float len = mPathMeasure.getLength();
970a95c8abb366d9c39450513335f550b56da13b30aztenghui                    start = start * len;
971a95c8abb366d9c39450513335f550b56da13b30aztenghui                    end = end * len;
972a95c8abb366d9c39450513335f550b56da13b30aztenghui                    path.reset();
973a95c8abb366d9c39450513335f550b56da13b30aztenghui                    if (start > end) {
974a95c8abb366d9c39450513335f550b56da13b30aztenghui                        mPathMeasure.getSegment(start, len, path, true);
975a95c8abb366d9c39450513335f550b56da13b30aztenghui                        mPathMeasure.getSegment(0f, end, path, true);
976a95c8abb366d9c39450513335f550b56da13b30aztenghui                    } else {
977a95c8abb366d9c39450513335f550b56da13b30aztenghui                        mPathMeasure.getSegment(start, end, path, true);
978a95c8abb366d9c39450513335f550b56da13b30aztenghui                    }
979a95c8abb366d9c39450513335f550b56da13b30aztenghui                    path.rLineTo(0, 0); // fix bug in measure
980a95c8abb366d9c39450513335f550b56da13b30aztenghui                }
981a95c8abb366d9c39450513335f550b56da13b30aztenghui                mRenderPath.addPath(path, mFinalPathMatrix);
982a95c8abb366d9c39450513335f550b56da13b30aztenghui
983a70b4742a7a0b2edc57cbbbf83d2c6d43ebc300dztenghui                if (fullPath.mFillColor != Color.TRANSPARENT) {
9848d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    if (mFillPaint == null) {
9858d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                        mFillPaint = new Paint();
9868d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                        mFillPaint.setStyle(Paint.Style.FILL);
9878d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                        mFillPaint.setAntiAlias(true);
98863cfd85bcce488a3f3952bd1db523a727d30ca39ztenghui                    }
989b07b086bd42181f62718a6394b56be3917b12511Alan Viverette
990b07b086bd42181f62718a6394b56be3917b12511Alan Viverette                    final Paint fillPaint = mFillPaint;
991b07b086bd42181f62718a6394b56be3917b12511Alan Viverette                    fillPaint.setColor(applyAlpha(fullPath.mFillColor, fullPath.mFillAlpha));
992b07b086bd42181f62718a6394b56be3917b12511Alan Viverette                    fillPaint.setColorFilter(filter);
993b07b086bd42181f62718a6394b56be3917b12511Alan Viverette                    canvas.drawPath(mRenderPath, fillPaint);
9948d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                }
99563cfd85bcce488a3f3952bd1db523a727d30ca39ztenghui
996a70b4742a7a0b2edc57cbbbf83d2c6d43ebc300dztenghui                if (fullPath.mStrokeColor != Color.TRANSPARENT) {
9978d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    if (mStrokePaint == null) {
9988d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                        mStrokePaint = new Paint();
9998d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                        mStrokePaint.setStyle(Paint.Style.STROKE);
10008d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                        mStrokePaint.setAntiAlias(true);
10018d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    }
10024b3988d159e1c9faa2a7e16c9aca9951264bb429Alan Viverette
10038d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    final Paint strokePaint = mStrokePaint;
1004a95c8abb366d9c39450513335f550b56da13b30aztenghui                    if (fullPath.mStrokeLineJoin != null) {
1005a95c8abb366d9c39450513335f550b56da13b30aztenghui                        strokePaint.setStrokeJoin(fullPath.mStrokeLineJoin);
10068d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    }
1007abb7d134c02ac60091108c491dafb00877093170John Hoford
1008a95c8abb366d9c39450513335f550b56da13b30aztenghui                    if (fullPath.mStrokeLineCap != null) {
1009a95c8abb366d9c39450513335f550b56da13b30aztenghui                        strokePaint.setStrokeCap(fullPath.mStrokeLineCap);
10108d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    }
101163cfd85bcce488a3f3952bd1db523a727d30ca39ztenghui
1012c4d651ec8a8c190c5df173166357c2fdf47472ceztenghui                    strokePaint.setStrokeMiter(fullPath.mStrokeMiterlimit);
1013b07b086bd42181f62718a6394b56be3917b12511Alan Viverette                    strokePaint.setColor(applyAlpha(fullPath.mStrokeColor, fullPath.mStrokeAlpha));
1014b07b086bd42181f62718a6394b56be3917b12511Alan Viverette                    strokePaint.setColorFilter(filter);
1015a95c8abb366d9c39450513335f550b56da13b30aztenghui                    strokePaint.setStrokeWidth(fullPath.mStrokeWidth * minScale);
10168d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    canvas.drawPath(mRenderPath, strokePaint);
101763cfd85bcce488a3f3952bd1db523a727d30ca39ztenghui                }
1018abb7d134c02ac60091108c491dafb00877093170John Hoford            }
1019abb7d134c02ac60091108c491dafb00877093170John Hoford        }
1020abb7d134c02ac60091108c491dafb00877093170John Hoford    }
1021abb7d134c02ac60091108c491dafb00877093170John Hoford
1022a95c8abb366d9c39450513335f550b56da13b30aztenghui    private static class VGroup {
102355f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        // mStackedMatrix is only used temporarily when drawing, it combines all
102455f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        // the parents' local matrices with the current one.
102555f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        private final Matrix mStackedMatrix = new Matrix();
102655f765441c6cec6b2990de12f68d1c4ac444d35cztenghui
102755f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        /////////////////////////////////////////////////////
102855f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        // Variables below need to be copied (deep copy if applicable) for mutation.
10298d0d24f7a2b1a59060aa18926984491691d3c667ztenghui        final ArrayList<Object> mChildren = new ArrayList<Object>();
1030abb7d134c02ac60091108c491dafb00877093170John Hoford
103163cfd85bcce488a3f3952bd1db523a727d30ca39ztenghui        private float mRotate = 0;
103263cfd85bcce488a3f3952bd1db523a727d30ca39ztenghui        private float mPivotX = 0;
103363cfd85bcce488a3f3952bd1db523a727d30ca39ztenghui        private float mPivotY = 0;
1034452f6ece7fe2fd1a85fca53f54e90bf041083b21ztenghui        private float mScaleX = 1;
1035452f6ece7fe2fd1a85fca53f54e90bf041083b21ztenghui        private float mScaleY = 1;
1036452f6ece7fe2fd1a85fca53f54e90bf041083b21ztenghui        private float mTranslateX = 0;
1037452f6ece7fe2fd1a85fca53f54e90bf041083b21ztenghui        private float mTranslateY = 0;
10384d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui
103955f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        // mLocalMatrix is updated based on the update of transformation information,
104055f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        // either parsed from the XML or by animation.
10414d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui        private final Matrix mLocalMatrix = new Matrix();
10428e5e11b99fac942122ee2d6cdd30af51564861aeAlan Viverette        private int mChangingConfigurations;
104363cfd85bcce488a3f3952bd1db523a727d30ca39ztenghui        private int[] mThemeAttrs;
1044e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        private String mGroupName = null;
1045e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
104655f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        public VGroup(VGroup copy, ArrayMap<String, Object> targetsMap) {
104755f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            mRotate = copy.mRotate;
104855f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            mPivotX = copy.mPivotX;
104955f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            mPivotY = copy.mPivotY;
105055f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            mScaleX = copy.mScaleX;
105155f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            mScaleY = copy.mScaleY;
105255f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            mTranslateX = copy.mTranslateX;
105355f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            mTranslateY = copy.mTranslateY;
105455f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            mThemeAttrs = copy.mThemeAttrs;
105555f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            mGroupName = copy.mGroupName;
105655f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            mChangingConfigurations = copy.mChangingConfigurations;
105755f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            if (mGroupName != null) {
105855f765441c6cec6b2990de12f68d1c4ac444d35cztenghui                targetsMap.put(mGroupName, this);
105955f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            }
106055f765441c6cec6b2990de12f68d1c4ac444d35cztenghui
106155f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            mLocalMatrix.set(copy.mLocalMatrix);
106255f765441c6cec6b2990de12f68d1c4ac444d35cztenghui
10638d0d24f7a2b1a59060aa18926984491691d3c667ztenghui            final ArrayList<Object> children = copy.mChildren;
10648d0d24f7a2b1a59060aa18926984491691d3c667ztenghui            for (int i = 0; i < children.size(); i++) {
10658d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                Object copyChild = children.get(i);
10668d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                if (copyChild instanceof VGroup) {
10678d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    VGroup copyGroup = (VGroup) copyChild;
10688d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    mChildren.add(new VGroup(copyGroup, targetsMap));
1069a95c8abb366d9c39450513335f550b56da13b30aztenghui                } else {
1070a95c8abb366d9c39450513335f550b56da13b30aztenghui                    VPath newPath = null;
1071a95c8abb366d9c39450513335f550b56da13b30aztenghui                    if (copyChild instanceof VFullPath) {
1072a95c8abb366d9c39450513335f550b56da13b30aztenghui                        newPath = new VFullPath((VFullPath) copyChild);
1073a95c8abb366d9c39450513335f550b56da13b30aztenghui                    } else if (copyChild instanceof VClipPath) {
1074a95c8abb366d9c39450513335f550b56da13b30aztenghui                        newPath = new VClipPath((VClipPath) copyChild);
1075a95c8abb366d9c39450513335f550b56da13b30aztenghui                    } else {
1076a95c8abb366d9c39450513335f550b56da13b30aztenghui                        throw new IllegalStateException("Unknown object in the tree!");
1077a95c8abb366d9c39450513335f550b56da13b30aztenghui                    }
10788d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    mChildren.add(newPath);
10798d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    if (newPath.mPathName != null) {
10808d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                        targetsMap.put(newPath.mPathName, newPath);
10818d0d24f7a2b1a59060aa18926984491691d3c667ztenghui                    }
108255f765441c6cec6b2990de12f68d1c4ac444d35cztenghui                }
108355f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            }
108455f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        }
108555f765441c6cec6b2990de12f68d1c4ac444d35cztenghui
108655f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        public VGroup() {
108755f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        }
108855f765441c6cec6b2990de12f68d1c4ac444d35cztenghui
1089a95c8abb366d9c39450513335f550b56da13b30aztenghui        public String getGroupName() {
1090a95c8abb366d9c39450513335f550b56da13b30aztenghui            return mGroupName;
1091a95c8abb366d9c39450513335f550b56da13b30aztenghui        }
1092a95c8abb366d9c39450513335f550b56da13b30aztenghui
1093a95c8abb366d9c39450513335f550b56da13b30aztenghui        public Matrix getLocalMatrix() {
1094a95c8abb366d9c39450513335f550b56da13b30aztenghui            return mLocalMatrix;
1095a95c8abb366d9c39450513335f550b56da13b30aztenghui        }
1096a95c8abb366d9c39450513335f550b56da13b30aztenghui
1097a95c8abb366d9c39450513335f550b56da13b30aztenghui        public void inflate(Resources res, AttributeSet attrs, Theme theme) {
1098a95c8abb366d9c39450513335f550b56da13b30aztenghui            final TypedArray a = obtainAttributes(res, theme, attrs,
1099a95c8abb366d9c39450513335f550b56da13b30aztenghui                    R.styleable.VectorDrawableGroup);
1100a95c8abb366d9c39450513335f550b56da13b30aztenghui            updateStateFromTypedArray(a);
1101a95c8abb366d9c39450513335f550b56da13b30aztenghui            a.recycle();
1102a95c8abb366d9c39450513335f550b56da13b30aztenghui        }
1103a95c8abb366d9c39450513335f550b56da13b30aztenghui
1104a95c8abb366d9c39450513335f550b56da13b30aztenghui        private void updateStateFromTypedArray(TypedArray a) {
1105a95c8abb366d9c39450513335f550b56da13b30aztenghui            // Account for any configuration changes.
1106a95c8abb366d9c39450513335f550b56da13b30aztenghui            mChangingConfigurations |= a.getChangingConfigurations();
1107a95c8abb366d9c39450513335f550b56da13b30aztenghui
1108a95c8abb366d9c39450513335f550b56da13b30aztenghui            // Extract the theme attributes, if any.
1109a95c8abb366d9c39450513335f550b56da13b30aztenghui            mThemeAttrs = a.extractThemeAttrs();
1110a95c8abb366d9c39450513335f550b56da13b30aztenghui
1111a95c8abb366d9c39450513335f550b56da13b30aztenghui            mRotate = a.getFloat(R.styleable.VectorDrawableGroup_rotation, mRotate);
1112a95c8abb366d9c39450513335f550b56da13b30aztenghui            mPivotX = a.getFloat(R.styleable.VectorDrawableGroup_pivotX, mPivotX);
1113a95c8abb366d9c39450513335f550b56da13b30aztenghui            mPivotY = a.getFloat(R.styleable.VectorDrawableGroup_pivotY, mPivotY);
1114a95c8abb366d9c39450513335f550b56da13b30aztenghui            mScaleX = a.getFloat(R.styleable.VectorDrawableGroup_scaleX, mScaleX);
1115a95c8abb366d9c39450513335f550b56da13b30aztenghui            mScaleY = a.getFloat(R.styleable.VectorDrawableGroup_scaleY, mScaleY);
1116a95c8abb366d9c39450513335f550b56da13b30aztenghui            mTranslateX = a.getFloat(R.styleable.VectorDrawableGroup_translateX, mTranslateX);
1117a95c8abb366d9c39450513335f550b56da13b30aztenghui            mTranslateY = a.getFloat(R.styleable.VectorDrawableGroup_translateY, mTranslateY);
1118a95c8abb366d9c39450513335f550b56da13b30aztenghui
1119a95c8abb366d9c39450513335f550b56da13b30aztenghui            final String groupName = a.getString(R.styleable.VectorDrawableGroup_name);
1120a95c8abb366d9c39450513335f550b56da13b30aztenghui            if (groupName != null) {
1121a95c8abb366d9c39450513335f550b56da13b30aztenghui                mGroupName = groupName;
1122a95c8abb366d9c39450513335f550b56da13b30aztenghui            }
1123a95c8abb366d9c39450513335f550b56da13b30aztenghui
1124a95c8abb366d9c39450513335f550b56da13b30aztenghui            updateLocalMatrix();
1125a95c8abb366d9c39450513335f550b56da13b30aztenghui        }
1126a95c8abb366d9c39450513335f550b56da13b30aztenghui
1127607bd848269fb802550e63aa61945790616f97a7Alan Viverette        public boolean canApplyTheme() {
1128607bd848269fb802550e63aa61945790616f97a7Alan Viverette            return mThemeAttrs != null;
1129607bd848269fb802550e63aa61945790616f97a7Alan Viverette        }
1130607bd848269fb802550e63aa61945790616f97a7Alan Viverette
1131a95c8abb366d9c39450513335f550b56da13b30aztenghui        public void applyTheme(Theme t) {
1132a95c8abb366d9c39450513335f550b56da13b30aztenghui            if (mThemeAttrs == null) {
1133a95c8abb366d9c39450513335f550b56da13b30aztenghui                return;
1134a95c8abb366d9c39450513335f550b56da13b30aztenghui            }
1135a95c8abb366d9c39450513335f550b56da13b30aztenghui
1136607bd848269fb802550e63aa61945790616f97a7Alan Viverette            final TypedArray a = t.resolveAttributes(mThemeAttrs, R.styleable.VectorDrawableGroup);
1137a95c8abb366d9c39450513335f550b56da13b30aztenghui            updateStateFromTypedArray(a);
1138a95c8abb366d9c39450513335f550b56da13b30aztenghui            a.recycle();
1139a95c8abb366d9c39450513335f550b56da13b30aztenghui        }
1140a95c8abb366d9c39450513335f550b56da13b30aztenghui
1141a95c8abb366d9c39450513335f550b56da13b30aztenghui        private void updateLocalMatrix() {
1142a95c8abb366d9c39450513335f550b56da13b30aztenghui            // The order we apply is the same as the
1143a95c8abb366d9c39450513335f550b56da13b30aztenghui            // RenderNode.cpp::applyViewPropertyTransforms().
1144a95c8abb366d9c39450513335f550b56da13b30aztenghui            mLocalMatrix.reset();
1145a95c8abb366d9c39450513335f550b56da13b30aztenghui            mLocalMatrix.postTranslate(-mPivotX, -mPivotY);
1146a95c8abb366d9c39450513335f550b56da13b30aztenghui            mLocalMatrix.postScale(mScaleX, mScaleY);
1147a95c8abb366d9c39450513335f550b56da13b30aztenghui            mLocalMatrix.postRotate(mRotate, 0, 0);
1148a95c8abb366d9c39450513335f550b56da13b30aztenghui            mLocalMatrix.postTranslate(mTranslateX + mPivotX, mTranslateY + mPivotY);
1149a95c8abb366d9c39450513335f550b56da13b30aztenghui        }
1150a95c8abb366d9c39450513335f550b56da13b30aztenghui
1151a95c8abb366d9c39450513335f550b56da13b30aztenghui        /* Setters and Getters, used by animator from AnimatedVectorDrawable. */
1152a95c8abb366d9c39450513335f550b56da13b30aztenghui        @SuppressWarnings("unused")
1153e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        public float getRotation() {
1154e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            return mRotate;
1155e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
1156e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
1157a95c8abb366d9c39450513335f550b56da13b30aztenghui        @SuppressWarnings("unused")
1158e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        public void setRotation(float rotation) {
1159e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            if (rotation != mRotate) {
1160e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui                mRotate = rotation;
1161e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui                updateLocalMatrix();
1162e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            }
1163e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
1164e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
1165a95c8abb366d9c39450513335f550b56da13b30aztenghui        @SuppressWarnings("unused")
1166e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        public float getPivotX() {
1167e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            return mPivotX;
1168e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
1169e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
1170a95c8abb366d9c39450513335f550b56da13b30aztenghui        @SuppressWarnings("unused")
1171e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        public void setPivotX(float pivotX) {
1172e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            if (pivotX != mPivotX) {
1173e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui                mPivotX = pivotX;
1174e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui                updateLocalMatrix();
1175e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            }
1176e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
1177e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
1178a95c8abb366d9c39450513335f550b56da13b30aztenghui        @SuppressWarnings("unused")
1179e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        public float getPivotY() {
1180e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            return mPivotY;
1181e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
1182e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
1183a95c8abb366d9c39450513335f550b56da13b30aztenghui        @SuppressWarnings("unused")
1184e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        public void setPivotY(float pivotY) {
1185e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            if (pivotY != mPivotY) {
1186e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui                mPivotY = pivotY;
1187e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui                updateLocalMatrix();
1188e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            }
1189e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
1190e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
1191a95c8abb366d9c39450513335f550b56da13b30aztenghui        @SuppressWarnings("unused")
1192e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        public float getScaleX() {
1193e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            return mScaleX;
1194e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
11954d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui
1196a95c8abb366d9c39450513335f550b56da13b30aztenghui        @SuppressWarnings("unused")
1197e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        public void setScaleX(float scaleX) {
1198e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            if (scaleX != mScaleX) {
1199e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui                mScaleX = scaleX;
1200e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui                updateLocalMatrix();
1201e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            }
1202e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
1203e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
1204a95c8abb366d9c39450513335f550b56da13b30aztenghui        @SuppressWarnings("unused")
1205e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        public float getScaleY() {
1206e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            return mScaleY;
1207e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
1208e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
1209a95c8abb366d9c39450513335f550b56da13b30aztenghui        @SuppressWarnings("unused")
1210e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        public void setScaleY(float scaleY) {
1211e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            if (scaleY != mScaleY) {
1212e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui                mScaleY = scaleY;
1213e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui                updateLocalMatrix();
1214e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            }
1215e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
1216e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
1217a95c8abb366d9c39450513335f550b56da13b30aztenghui        @SuppressWarnings("unused")
1218e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        public float getTranslateX() {
1219e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            return mTranslateX;
1220e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
1221e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
1222a95c8abb366d9c39450513335f550b56da13b30aztenghui        @SuppressWarnings("unused")
1223e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        public void setTranslateX(float translateX) {
1224e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            if (translateX != mTranslateX) {
1225e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui                mTranslateX = translateX;
1226e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui                updateLocalMatrix();
1227e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            }
1228e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
1229e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
1230a95c8abb366d9c39450513335f550b56da13b30aztenghui        @SuppressWarnings("unused")
1231e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        public float getTranslateY() {
1232e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            return mTranslateY;
1233e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
1234e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
1235a95c8abb366d9c39450513335f550b56da13b30aztenghui        @SuppressWarnings("unused")
1236e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        public void setTranslateY(float translateY) {
1237e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            if (translateY != mTranslateY) {
1238e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui                mTranslateY = translateY;
1239e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui                updateLocalMatrix();
1240e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            }
1241e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
1242a95c8abb366d9c39450513335f550b56da13b30aztenghui    }
1243e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
1244a95c8abb366d9c39450513335f550b56da13b30aztenghui    /**
1245a95c8abb366d9c39450513335f550b56da13b30aztenghui     * Common Path information for clip path and normal path.
1246a95c8abb366d9c39450513335f550b56da13b30aztenghui     */
1247a95c8abb366d9c39450513335f550b56da13b30aztenghui    private static class VPath {
1248a95c8abb366d9c39450513335f550b56da13b30aztenghui        protected PathParser.PathDataNode[] mNodes = null;
1249a95c8abb366d9c39450513335f550b56da13b30aztenghui        String mPathName;
1250a95c8abb366d9c39450513335f550b56da13b30aztenghui        int mChangingConfigurations;
1251a95c8abb366d9c39450513335f550b56da13b30aztenghui
1252a95c8abb366d9c39450513335f550b56da13b30aztenghui        public VPath() {
1253a95c8abb366d9c39450513335f550b56da13b30aztenghui            // Empty constructor.
12544d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui        }
12554d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui
1256a95c8abb366d9c39450513335f550b56da13b30aztenghui        public VPath(VPath copy) {
1257a95c8abb366d9c39450513335f550b56da13b30aztenghui            mPathName = copy.mPathName;
1258a95c8abb366d9c39450513335f550b56da13b30aztenghui            mChangingConfigurations = copy.mChangingConfigurations;
1259a95c8abb366d9c39450513335f550b56da13b30aztenghui            mNodes = PathParser.deepCopyNodes(copy.mNodes);
12604d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui        }
12614d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui
1262a95c8abb366d9c39450513335f550b56da13b30aztenghui        public void toPath(Path path) {
1263a95c8abb366d9c39450513335f550b56da13b30aztenghui            path.reset();
1264a95c8abb366d9c39450513335f550b56da13b30aztenghui            if (mNodes != null) {
1265a95c8abb366d9c39450513335f550b56da13b30aztenghui                PathParser.PathDataNode.nodesToPath(mNodes, path);
1266a95c8abb366d9c39450513335f550b56da13b30aztenghui            }
12674d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui        }
12684d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui
1269a95c8abb366d9c39450513335f550b56da13b30aztenghui        public String getPathName() {
1270a95c8abb366d9c39450513335f550b56da13b30aztenghui            return mPathName;
12718e5e11b99fac942122ee2d6cdd30af51564861aeAlan Viverette        }
12728e5e11b99fac942122ee2d6cdd30af51564861aeAlan Viverette
1273a95c8abb366d9c39450513335f550b56da13b30aztenghui        public boolean canApplyTheme() {
1274a95c8abb366d9c39450513335f550b56da13b30aztenghui            return false;
1275a95c8abb366d9c39450513335f550b56da13b30aztenghui        }
127663cfd85bcce488a3f3952bd1db523a727d30ca39ztenghui
1277a95c8abb366d9c39450513335f550b56da13b30aztenghui        public void applyTheme(Theme t) {
1278a95c8abb366d9c39450513335f550b56da13b30aztenghui        }
127963cfd85bcce488a3f3952bd1db523a727d30ca39ztenghui
1280a95c8abb366d9c39450513335f550b56da13b30aztenghui        public boolean isClipPath() {
1281a95c8abb366d9c39450513335f550b56da13b30aztenghui            return false;
1282a95c8abb366d9c39450513335f550b56da13b30aztenghui        }
128363cfd85bcce488a3f3952bd1db523a727d30ca39ztenghui
1284a95c8abb366d9c39450513335f550b56da13b30aztenghui        /* Setters and Getters, used by animator from AnimatedVectorDrawable. */
1285a95c8abb366d9c39450513335f550b56da13b30aztenghui        @SuppressWarnings("unused")
1286a95c8abb366d9c39450513335f550b56da13b30aztenghui        public PathParser.PathDataNode[] getPathData() {
1287a95c8abb366d9c39450513335f550b56da13b30aztenghui            return mNodes;
1288a95c8abb366d9c39450513335f550b56da13b30aztenghui        }
1289a95c8abb366d9c39450513335f550b56da13b30aztenghui
1290a95c8abb366d9c39450513335f550b56da13b30aztenghui        @SuppressWarnings("unused")
1291a95c8abb366d9c39450513335f550b56da13b30aztenghui        public void setPathData(PathParser.PathDataNode[] nodes) {
1292a95c8abb366d9c39450513335f550b56da13b30aztenghui            if (!PathParser.canMorph(mNodes, nodes)) {
1293a95c8abb366d9c39450513335f550b56da13b30aztenghui                // This should not happen in the middle of animation.
1294a95c8abb366d9c39450513335f550b56da13b30aztenghui                mNodes = PathParser.deepCopyNodes(nodes);
1295a95c8abb366d9c39450513335f550b56da13b30aztenghui            } else {
1296a95c8abb366d9c39450513335f550b56da13b30aztenghui                PathParser.updateNodes(mNodes, nodes);
129763cfd85bcce488a3f3952bd1db523a727d30ca39ztenghui            }
1298a95c8abb366d9c39450513335f550b56da13b30aztenghui        }
1299a95c8abb366d9c39450513335f550b56da13b30aztenghui    }
130063cfd85bcce488a3f3952bd1db523a727d30ca39ztenghui
1301a95c8abb366d9c39450513335f550b56da13b30aztenghui    /**
1302a95c8abb366d9c39450513335f550b56da13b30aztenghui     * Clip path, which only has name and pathData.
1303a95c8abb366d9c39450513335f550b56da13b30aztenghui     */
13048490354dc651dd8f25036ef0dda7917fb33013edztenghui    private static class VClipPath extends VPath {
1305a95c8abb366d9c39450513335f550b56da13b30aztenghui        public VClipPath() {
1306a95c8abb366d9c39450513335f550b56da13b30aztenghui            // Empty constructor.
13078e5e11b99fac942122ee2d6cdd30af51564861aeAlan Viverette        }
13084d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui
1309a95c8abb366d9c39450513335f550b56da13b30aztenghui        public VClipPath(VClipPath copy) {
1310a95c8abb366d9c39450513335f550b56da13b30aztenghui            super(copy);
1311a95c8abb366d9c39450513335f550b56da13b30aztenghui        }
13124d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui
1313a95c8abb366d9c39450513335f550b56da13b30aztenghui        public void inflate(Resources r, AttributeSet attrs, Theme theme) {
1314a95c8abb366d9c39450513335f550b56da13b30aztenghui            final TypedArray a = obtainAttributes(r, theme, attrs,
1315a95c8abb366d9c39450513335f550b56da13b30aztenghui                    R.styleable.VectorDrawableClipPath);
13168e5e11b99fac942122ee2d6cdd30af51564861aeAlan Viverette            updateStateFromTypedArray(a);
131763cfd85bcce488a3f3952bd1db523a727d30ca39ztenghui            a.recycle();
131863cfd85bcce488a3f3952bd1db523a727d30ca39ztenghui        }
131963cfd85bcce488a3f3952bd1db523a727d30ca39ztenghui
1320a95c8abb366d9c39450513335f550b56da13b30aztenghui        private void updateStateFromTypedArray(TypedArray a) {
1321a95c8abb366d9c39450513335f550b56da13b30aztenghui            // Account for any configuration changes.
1322a95c8abb366d9c39450513335f550b56da13b30aztenghui            mChangingConfigurations |= a.getChangingConfigurations();
1323a95c8abb366d9c39450513335f550b56da13b30aztenghui
13245004032ebc2aee97c5884b7f91cc33d2f98ae8b5Alan Viverette            final String pathName = a.getString(R.styleable.VectorDrawableClipPath_name);
13255004032ebc2aee97c5884b7f91cc33d2f98ae8b5Alan Viverette            if (pathName != null) {
13265004032ebc2aee97c5884b7f91cc33d2f98ae8b5Alan Viverette                mPathName = pathName;
13275004032ebc2aee97c5884b7f91cc33d2f98ae8b5Alan Viverette            }
13285004032ebc2aee97c5884b7f91cc33d2f98ae8b5Alan Viverette
13295004032ebc2aee97c5884b7f91cc33d2f98ae8b5Alan Viverette            final String pathData = a.getString(R.styleable.VectorDrawableClipPath_pathData);
13305004032ebc2aee97c5884b7f91cc33d2f98ae8b5Alan Viverette            if (pathData != null) {
13315004032ebc2aee97c5884b7f91cc33d2f98ae8b5Alan Viverette                mNodes = PathParser.createNodesFromPathData(pathData);
13325004032ebc2aee97c5884b7f91cc33d2f98ae8b5Alan Viverette            }
1333a95c8abb366d9c39450513335f550b56da13b30aztenghui        }
1334a95c8abb366d9c39450513335f550b56da13b30aztenghui
1335a95c8abb366d9c39450513335f550b56da13b30aztenghui        @Override
1336a95c8abb366d9c39450513335f550b56da13b30aztenghui        public boolean isClipPath() {
1337a95c8abb366d9c39450513335f550b56da13b30aztenghui            return true;
13384d24caf1dec2babf273b18c99638fe2a0635ced4ztenghui        }
1339abb7d134c02ac60091108c491dafb00877093170John Hoford    }
1340abb7d134c02ac60091108c491dafb00877093170John Hoford
1341a95c8abb366d9c39450513335f550b56da13b30aztenghui    /**
1342a95c8abb366d9c39450513335f550b56da13b30aztenghui     * Normal path, which contains all the fill / paint information.
1343a95c8abb366d9c39450513335f550b56da13b30aztenghui     */
1344a95c8abb366d9c39450513335f550b56da13b30aztenghui    private static class VFullPath extends VPath {
134555f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        /////////////////////////////////////////////////////
134655f765441c6cec6b2990de12f68d1c4ac444d35cztenghui        // Variables below need to be copied (deep copy if applicable) for mutation.
13479453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette        private int[] mThemeAttrs;
13489453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette
1349a70b4742a7a0b2edc57cbbbf83d2c6d43ebc300dztenghui        int mStrokeColor = Color.TRANSPARENT;
1350abb7d134c02ac60091108c491dafb00877093170John Hoford        float mStrokeWidth = 0;
13512e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui
1352a70b4742a7a0b2edc57cbbbf83d2c6d43ebc300dztenghui        int mFillColor = Color.TRANSPARENT;
13532e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui        float mStrokeAlpha = 1.0f;
1354abb7d134c02ac60091108c491dafb00877093170John Hoford        int mFillRule;
13552e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui        float mFillAlpha = 1.0f;
1356abb7d134c02ac60091108c491dafb00877093170John Hoford        float mTrimPathStart = 0;
1357abb7d134c02ac60091108c491dafb00877093170John Hoford        float mTrimPathEnd = 1;
1358abb7d134c02ac60091108c491dafb00877093170John Hoford        float mTrimPathOffset = 0;
13599453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette
13604b3988d159e1c9faa2a7e16c9aca9951264bb429Alan Viverette        Paint.Cap mStrokeLineCap = Paint.Cap.BUTT;
13614b3988d159e1c9faa2a7e16c9aca9951264bb429Alan Viverette        Paint.Join mStrokeLineJoin = Paint.Join.MITER;
1362abb7d134c02ac60091108c491dafb00877093170John Hoford        float mStrokeMiterlimit = 4;
13639453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette
1364a95c8abb366d9c39450513335f550b56da13b30aztenghui        public VFullPath() {
13659453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette            // Empty constructor.
13669453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette        }
13679453b7cfd4bbb35467bd7f947f850bd154982fceAlan Viverette
1368a95c8abb366d9c39450513335f550b56da13b30aztenghui        public VFullPath(VFullPath copy) {
1369a95c8abb366d9c39450513335f550b56da13b30aztenghui            super(copy);
137055f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            mThemeAttrs = copy.mThemeAttrs;
137155f765441c6cec6b2990de12f68d1c4ac444d35cztenghui
137255f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            mStrokeColor = copy.mStrokeColor;
137355f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            mStrokeWidth = copy.mStrokeWidth;
13742e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui            mStrokeAlpha = copy.mStrokeAlpha;
137555f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            mFillColor = copy.mFillColor;
137655f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            mFillRule = copy.mFillRule;
13772e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui            mFillAlpha = copy.mFillAlpha;
137855f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            mTrimPathStart = copy.mTrimPathStart;
137955f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            mTrimPathEnd = copy.mTrimPathEnd;
138055f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            mTrimPathOffset = copy.mTrimPathOffset;
138155f765441c6cec6b2990de12f68d1c4ac444d35cztenghui
138255f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            mStrokeLineCap = copy.mStrokeLineCap;
138355f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            mStrokeLineJoin = copy.mStrokeLineJoin;
138455f765441c6cec6b2990de12f68d1c4ac444d35cztenghui            mStrokeMiterlimit = copy.mStrokeMiterlimit;
1385abb7d134c02ac60091108c491dafb00877093170John Hoford        }
1386abb7d134c02ac60091108c491dafb00877093170John Hoford
13877f1ab7a43fd7e65bbd7460334014ecc73dbb1b8dAlan Viverette        private Paint.Cap getStrokeLineCap(int id, Paint.Cap defValue) {
13887f1ab7a43fd7e65bbd7460334014ecc73dbb1b8dAlan Viverette            switch (id) {
13897f1ab7a43fd7e65bbd7460334014ecc73dbb1b8dAlan Viverette                case LINECAP_BUTT:
13907f1ab7a43fd7e65bbd7460334014ecc73dbb1b8dAlan Viverette                    return Paint.Cap.BUTT;
13917f1ab7a43fd7e65bbd7460334014ecc73dbb1b8dAlan Viverette                case LINECAP_ROUND:
13927f1ab7a43fd7e65bbd7460334014ecc73dbb1b8dAlan Viverette                    return Paint.Cap.ROUND;
13937f1ab7a43fd7e65bbd7460334014ecc73dbb1b8dAlan Viverette                case LINECAP_SQUARE:
13947f1ab7a43fd7e65bbd7460334014ecc73dbb1b8dAlan Viverette                    return Paint.Cap.SQUARE;
13957f1ab7a43fd7e65bbd7460334014ecc73dbb1b8dAlan Viverette                default:
13967f1ab7a43fd7e65bbd7460334014ecc73dbb1b8dAlan Viverette                    return defValue;
13977f1ab7a43fd7e65bbd7460334014ecc73dbb1b8dAlan Viverette            }
13987f1ab7a43fd7e65bbd7460334014ecc73dbb1b8dAlan Viverette        }
13997f1ab7a43fd7e65bbd7460334014ecc73dbb1b8dAlan Viverette
14007f1ab7a43fd7e65bbd7460334014ecc73dbb1b8dAlan Viverette        private Paint.Join getStrokeLineJoin(int id, Paint.Join defValue) {
14017f1ab7a43fd7e65bbd7460334014ecc73dbb1b8dAlan Viverette            switch (id) {
14027f1ab7a43fd7e65bbd7460334014ecc73dbb1b8dAlan Viverette                case LINEJOIN_MITER:
14037f1ab7a43fd7e65bbd7460334014ecc73dbb1b8dAlan Viverette                    return Paint.Join.MITER;
14047f1ab7a43fd7e65bbd7460334014ecc73dbb1b8dAlan Viverette                case LINEJOIN_ROUND:
14057f1ab7a43fd7e65bbd7460334014ecc73dbb1b8dAlan Viverette                    return Paint.Join.ROUND;
14067f1ab7a43fd7e65bbd7460334014ecc73dbb1b8dAlan Viverette                case LINEJOIN_BEVEL:
14077f1ab7a43fd7e65bbd7460334014ecc73dbb1b8dAlan Viverette                    return Paint.Join.BEVEL;
14087f1ab7a43fd7e65bbd7460334014ecc73dbb1b8dAlan Viverette                default:
14097f1ab7a43fd7e65bbd7460334014ecc73dbb1b8dAlan Viverette                    return defValue;
14107f1ab7a43fd7e65bbd7460334014ecc73dbb1b8dAlan Viverette            }
14117f1ab7a43fd7e65bbd7460334014ecc73dbb1b8dAlan Viverette        }
14127f1ab7a43fd7e65bbd7460334014ecc73dbb1b8dAlan Viverette
1413a95c8abb366d9c39450513335f550b56da13b30aztenghui        @Override
1414a95c8abb366d9c39450513335f550b56da13b30aztenghui        public boolean canApplyTheme() {
1415a95c8abb366d9c39450513335f550b56da13b30aztenghui            return mThemeAttrs != null;
1416eb034fbca40006c55db143047eb628c4b657730aztenghui        }
1417eb034fbca40006c55db143047eb628c4b657730aztenghui
1418a95c8abb366d9c39450513335f550b56da13b30aztenghui        public void inflate(Resources r, AttributeSet attrs, Theme theme) {
1419a95c8abb366d9c39450513335f550b56da13b30aztenghui            final TypedArray a = obtainAttributes(r, theme, attrs,
1420a95c8abb366d9c39450513335f550b56da13b30aztenghui                    R.styleable.VectorDrawablePath);
1421a95c8abb366d9c39450513335f550b56da13b30aztenghui            updateStateFromTypedArray(a);
1422a95c8abb366d9c39450513335f550b56da13b30aztenghui            a.recycle();
1423a95c8abb366d9c39450513335f550b56da13b30aztenghui        }
1424a95c8abb366d9c39450513335f550b56da13b30aztenghui
1425a95c8abb366d9c39450513335f550b56da13b30aztenghui        private void updateStateFromTypedArray(TypedArray a) {
1426a95c8abb366d9c39450513335f550b56da13b30aztenghui            // Account for any configuration changes.
1427a95c8abb366d9c39450513335f550b56da13b30aztenghui            mChangingConfigurations |= a.getChangingConfigurations();
1428a95c8abb366d9c39450513335f550b56da13b30aztenghui
1429a95c8abb366d9c39450513335f550b56da13b30aztenghui            // Extract the theme attributes, if any.
1430a95c8abb366d9c39450513335f550b56da13b30aztenghui            mThemeAttrs = a.extractThemeAttrs();
1431a95c8abb366d9c39450513335f550b56da13b30aztenghui
14325004032ebc2aee97c5884b7f91cc33d2f98ae8b5Alan Viverette            final String pathName = a.getString(R.styleable.VectorDrawablePath_name);
14335004032ebc2aee97c5884b7f91cc33d2f98ae8b5Alan Viverette            if (pathName != null) {
14345004032ebc2aee97c5884b7f91cc33d2f98ae8b5Alan Viverette                mPathName = pathName;
14355004032ebc2aee97c5884b7f91cc33d2f98ae8b5Alan Viverette            }
14365004032ebc2aee97c5884b7f91cc33d2f98ae8b5Alan Viverette
14375004032ebc2aee97c5884b7f91cc33d2f98ae8b5Alan Viverette            final String pathData = a.getString(R.styleable.VectorDrawablePath_pathData);
14385004032ebc2aee97c5884b7f91cc33d2f98ae8b5Alan Viverette            if (pathData != null) {
14395004032ebc2aee97c5884b7f91cc33d2f98ae8b5Alan Viverette                mNodes = PathParser.createNodesFromPathData(pathData);
14405004032ebc2aee97c5884b7f91cc33d2f98ae8b5Alan Viverette            }
1441a95c8abb366d9c39450513335f550b56da13b30aztenghui
1442a95c8abb366d9c39450513335f550b56da13b30aztenghui            mFillColor = a.getColor(R.styleable.VectorDrawablePath_fillColor,
1443a95c8abb366d9c39450513335f550b56da13b30aztenghui                    mFillColor);
14442e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui            mFillAlpha = a.getFloat(R.styleable.VectorDrawablePath_fillAlpha,
14452e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui                    mFillAlpha);
1446a95c8abb366d9c39450513335f550b56da13b30aztenghui            mStrokeLineCap = getStrokeLineCap(a.getInt(
1447a95c8abb366d9c39450513335f550b56da13b30aztenghui                    R.styleable.VectorDrawablePath_strokeLineCap, -1), mStrokeLineCap);
1448a95c8abb366d9c39450513335f550b56da13b30aztenghui            mStrokeLineJoin = getStrokeLineJoin(a.getInt(
1449a95c8abb366d9c39450513335f550b56da13b30aztenghui                    R.styleable.VectorDrawablePath_strokeLineJoin, -1), mStrokeLineJoin);
1450a95c8abb366d9c39450513335f550b56da13b30aztenghui            mStrokeMiterlimit = a.getFloat(
1451a95c8abb366d9c39450513335f550b56da13b30aztenghui                    R.styleable.VectorDrawablePath_strokeMiterLimit, mStrokeMiterlimit);
1452a95c8abb366d9c39450513335f550b56da13b30aztenghui            mStrokeColor = a.getColor(R.styleable.VectorDrawablePath_strokeColor,
1453a95c8abb366d9c39450513335f550b56da13b30aztenghui                    mStrokeColor);
14542e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui            mStrokeAlpha = a.getFloat(R.styleable.VectorDrawablePath_strokeAlpha,
14552e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui                    mStrokeAlpha);
1456a95c8abb366d9c39450513335f550b56da13b30aztenghui            mStrokeWidth = a.getFloat(R.styleable.VectorDrawablePath_strokeWidth,
1457a95c8abb366d9c39450513335f550b56da13b30aztenghui                    mStrokeWidth);
1458a95c8abb366d9c39450513335f550b56da13b30aztenghui            mTrimPathEnd = a.getFloat(R.styleable.VectorDrawablePath_trimPathEnd,
1459a95c8abb366d9c39450513335f550b56da13b30aztenghui                    mTrimPathEnd);
1460a95c8abb366d9c39450513335f550b56da13b30aztenghui            mTrimPathOffset = a.getFloat(
1461a95c8abb366d9c39450513335f550b56da13b30aztenghui                    R.styleable.VectorDrawablePath_trimPathOffset, mTrimPathOffset);
1462a95c8abb366d9c39450513335f550b56da13b30aztenghui            mTrimPathStart = a.getFloat(
1463a95c8abb366d9c39450513335f550b56da13b30aztenghui                    R.styleable.VectorDrawablePath_trimPathStart, mTrimPathStart);
1464a95c8abb366d9c39450513335f550b56da13b30aztenghui        }
1465a95c8abb366d9c39450513335f550b56da13b30aztenghui
1466a95c8abb366d9c39450513335f550b56da13b30aztenghui        @Override
1467a95c8abb366d9c39450513335f550b56da13b30aztenghui        public void applyTheme(Theme t) {
1468a95c8abb366d9c39450513335f550b56da13b30aztenghui            if (mThemeAttrs == null) {
1469a95c8abb366d9c39450513335f550b56da13b30aztenghui                return;
1470a95c8abb366d9c39450513335f550b56da13b30aztenghui            }
1471a95c8abb366d9c39450513335f550b56da13b30aztenghui
1472607bd848269fb802550e63aa61945790616f97a7Alan Viverette            final TypedArray a = t.resolveAttributes(mThemeAttrs, R.styleable.VectorDrawablePath);
1473a95c8abb366d9c39450513335f550b56da13b30aztenghui            updateStateFromTypedArray(a);
1474a95c8abb366d9c39450513335f550b56da13b30aztenghui            a.recycle();
1475a95c8abb366d9c39450513335f550b56da13b30aztenghui        }
1476a95c8abb366d9c39450513335f550b56da13b30aztenghui
1477a95c8abb366d9c39450513335f550b56da13b30aztenghui        /* Setters and Getters, used by animator from AnimatedVectorDrawable. */
1478eb034fbca40006c55db143047eb628c4b657730aztenghui        @SuppressWarnings("unused")
1479dbcccffc3760f702887faa95b2748125bac88a89ztenghui        int getStrokeColor() {
1480e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            return mStrokeColor;
1481e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
1482e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
1483eb034fbca40006c55db143047eb628c4b657730aztenghui        @SuppressWarnings("unused")
1484dbcccffc3760f702887faa95b2748125bac88a89ztenghui        void setStrokeColor(int strokeColor) {
1485e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            mStrokeColor = strokeColor;
1486e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
1487e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
1488eb034fbca40006c55db143047eb628c4b657730aztenghui        @SuppressWarnings("unused")
1489e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        float getStrokeWidth() {
1490e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            return mStrokeWidth;
1491e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
1492e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
1493eb034fbca40006c55db143047eb628c4b657730aztenghui        @SuppressWarnings("unused")
1494e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        void setStrokeWidth(float strokeWidth) {
1495e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            mStrokeWidth = strokeWidth;
1496e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
1497e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
1498eb034fbca40006c55db143047eb628c4b657730aztenghui        @SuppressWarnings("unused")
1499dbcccffc3760f702887faa95b2748125bac88a89ztenghui        float getStrokeAlpha() {
15002e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui            return mStrokeAlpha;
15012e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui        }
15022e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui
15032e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui        @SuppressWarnings("unused")
1504dbcccffc3760f702887faa95b2748125bac88a89ztenghui        void setStrokeAlpha(float strokeAlpha) {
15052e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui            mStrokeAlpha = strokeAlpha;
15062e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui        }
15072e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui
15082e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui        @SuppressWarnings("unused")
1509dbcccffc3760f702887faa95b2748125bac88a89ztenghui        int getFillColor() {
1510e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            return mFillColor;
1511e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
1512e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
1513eb034fbca40006c55db143047eb628c4b657730aztenghui        @SuppressWarnings("unused")
1514dbcccffc3760f702887faa95b2748125bac88a89ztenghui        void setFillColor(int fillColor) {
1515e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            mFillColor = fillColor;
1516e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
1517e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
1518eb034fbca40006c55db143047eb628c4b657730aztenghui        @SuppressWarnings("unused")
1519dbcccffc3760f702887faa95b2748125bac88a89ztenghui        float getFillAlpha() {
15202e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui            return mFillAlpha;
15212e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui        }
15222e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui
15232e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui        @SuppressWarnings("unused")
1524dbcccffc3760f702887faa95b2748125bac88a89ztenghui        void setFillAlpha(float fillAlpha) {
15252e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui            mFillAlpha = fillAlpha;
15262e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui        }
15272e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui
15282e17d2b232e11b3ec246c704d8c4707c8fd863faztenghui        @SuppressWarnings("unused")
1529e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        float getTrimPathStart() {
1530e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            return mTrimPathStart;
1531e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
1532e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
1533eb034fbca40006c55db143047eb628c4b657730aztenghui        @SuppressWarnings("unused")
1534e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        void setTrimPathStart(float trimPathStart) {
1535e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            mTrimPathStart = trimPathStart;
1536e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
1537e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
1538eb034fbca40006c55db143047eb628c4b657730aztenghui        @SuppressWarnings("unused")
1539e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        float getTrimPathEnd() {
1540e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            return mTrimPathEnd;
1541e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
1542e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
1543eb034fbca40006c55db143047eb628c4b657730aztenghui        @SuppressWarnings("unused")
1544e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        void setTrimPathEnd(float trimPathEnd) {
1545e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            mTrimPathEnd = trimPathEnd;
1546e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
1547e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
1548eb034fbca40006c55db143047eb628c4b657730aztenghui        @SuppressWarnings("unused")
1549e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        float getTrimPathOffset() {
1550e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            return mTrimPathOffset;
1551e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
1552e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui
1553eb034fbca40006c55db143047eb628c4b657730aztenghui        @SuppressWarnings("unused")
1554e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        void setTrimPathOffset(float trimPathOffset) {
1555e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui            mTrimPathOffset = trimPathOffset;
1556e5e92602a41a4ddc7b42cd1c171a0edfbd09b8daztenghui        }
1557abb7d134c02ac60091108c491dafb00877093170John Hoford    }
1558abb7d134c02ac60091108c491dafb00877093170John Hoford}
1559