VectorDrawable.java revision fd3c4744f265c5277e6e2641a18d5ec3dff19f6b
1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15package android.graphics.drawable; 16 17import android.content.res.ColorStateList; 18import android.content.res.Resources; 19import android.content.res.Resources.Theme; 20import android.content.res.TypedArray; 21import android.graphics.Bitmap; 22import android.graphics.Canvas; 23import android.graphics.Color; 24import android.graphics.ColorFilter; 25import android.graphics.Matrix; 26import android.graphics.Paint; 27import android.graphics.Path; 28import android.graphics.PathMeasure; 29import android.graphics.PixelFormat; 30import android.graphics.PorterDuffColorFilter; 31import android.graphics.Rect; 32import android.graphics.Region; 33import android.graphics.PorterDuff.Mode; 34import android.util.ArrayMap; 35import android.util.AttributeSet; 36import android.util.LayoutDirection; 37import android.util.Log; 38import android.util.PathParser; 39import android.util.Xml; 40 41import com.android.internal.R; 42 43import org.xmlpull.v1.XmlPullParser; 44import org.xmlpull.v1.XmlPullParserException; 45import org.xmlpull.v1.XmlPullParserFactory; 46 47import java.io.IOException; 48import java.util.ArrayList; 49import java.util.Stack; 50 51/** 52 * This lets you create a drawable based on an XML vector graphic. It can be 53 * defined in an XML file with the <code><vector></code> element. 54 * <p/> 55 * The vector drawable has the following elements: 56 * <p/> 57 * <dt><code><vector></code></dt> 58 * <dl> 59 * <dd>Used to defined a vector drawable 60 * <dl> 61 * <dt><code>android:width</code></dt> 62 * <dd>Used to defined the intrinsic width of the drawable. 63 * This support all the dimension units, normally specified with dp.</dd> 64 * <dt><code>android:height</code></dt> 65 * <dd>Used to defined the intrinsic height the drawable. 66 * This support all the dimension units, normally specified with dp.</dd> 67 * <dt><code>android:viewportWidth</code></dt> 68 * <dd>Used to defined the width of the viewport space. Viewport is basically 69 * the virtual canvas where the paths are drawn on.</dd> 70 * <dt><code>android:viewportHeight</code></dt> 71 * <dd>Used to defined the height of the viewport space. Viewport is basically 72 * the virtual canvas where the paths are drawn on.</dd> 73 * <dt><code>android:tint</code></dt> 74 * <dd>The color to apply to the drawable as a tint. By default, no tint is applied.</dd> 75 * <dt><code>android:tintMode</code></dt> 76 * <dd>The Porter-Duff blending mode for the tint color. The default value is src_in.</dd> 77 * <dt><code>android:autoMirrored</code></dt> 78 * <dd>Indicates if the drawable needs to be mirrored when its layout direction is 79 * RTL (right-to-left).</dd> 80 * </dl></dd> 81 * </dl> 82 * 83 * <dl> 84 * <dt><code><group></code></dt> 85 * <dd>Defines a group of paths or subgroups, plus transformation information. 86 * The transformations are defined in the same coordinates as the viewport. 87 * And the transformations are applied in the order of scale, rotate then translate. 88 * <dl> 89 * <dt><code>android:rotation</code></dt> 90 * <dd>The degrees of rotation of the group.</dd> 91 * <dt><code>android:pivotX</code></dt> 92 * <dd>The X coordinate of the pivot for the scale and rotation of the group. 93 * This is defined in the viewport space.</dd> 94 * <dt><code>android:pivotY</code></dt> 95 * <dd>The Y coordinate of the pivot for the scale and rotation of the group. 96 * This is defined in the viewport space.</dd> 97 * <dt><code>android:scaleX</code></dt> 98 * <dd>The amount of scale on the X Coordinate.</dd> 99 * <dt><code>android:scaleY</code></dt> 100 * <dd>The amount of scale on the Y coordinate.</dd> 101 * <dt><code>android:translateX</code></dt> 102 * <dd>The amount of translation on the X coordinate. 103 * This is defined in the viewport space.</dd> 104 * <dt><code>android:translateY</code></dt> 105 * <dd>The amount of translation on the Y coordinate. 106 * This is defined in the viewport space.</dd> 107 * <dt><code>android:alpha</code></dt> 108 * <dd>The amount of transparency.</dd> 109 * </dl></dd> 110 * </dl> 111 * 112 * <dl> 113 * <dt><code><path></code></dt> 114 * <dd>Defines paths to be drawn. 115 * <dl> 116 * <dt><code>android:name</code></dt> 117 * <dd>Defines the name of the path.</dd> 118 * <dt><code>android:pathData</code></dt> 119 * <dd>Defines path string. This is using exactly same format as "d" attribute 120 * in the SVG's path data. This is defined in the viewport space.</dd> 121 * <dt><code>android:fillColor</code></dt> 122 * <dd>Defines the color to fill the path (none if not present).</dd> 123 * <dt><code>android:strokeColor</code></dt> 124 * <dd>Defines the color to draw the path outline (none if not present).</dd> 125 * <dt><code>android:strokeWidth</code></dt> 126 * <dd>The width a path stroke.</dd> 127 * <dt><code>android:trimPathStart</code></dt> 128 * <dd>The fraction of the path to trim from the start, in the range from 0 to 1.</dd> 129 * <dt><code>android:trimPathEnd</code></dt> 130 * <dd>The fraction of the path to trim from the end, in the range from 0 to 1.</dd> 131 * <dt><code>android:trimPathOffset</code></dt> 132 * <dd>Shift trim region (allows showed region to include the start and end), in the range 133 * from 0 to 1.</dd> 134 * <dt><code>android:strokeLineCap</code></dt> 135 * <dd>Sets the linecap for a stroked path: butt, round, square.</dd> 136 * <dt><code>android:strokeLineJoin</code></dt> 137 * <dd>Sets the lineJoin for a stroked path: miter,round,bevel.</dd> 138 * <dt><code>android:strokeMiterLimit</code></dt> 139 * <dd>Sets the Miter limit for a stroked path.</dd> 140 * </dl></dd> 141 * </dl> 142 * 143 * <dl> 144 * <dt><code><clip-path></code></dt> 145 * <dd>Defines path to be the current clip. 146 * <dl> 147 * <dt><code>android:name</code></dt> 148 * <dd>Defines the name of the clip path.</dd> 149 * <dt><code>android:pathData</code></dt> 150 * <dd>Defines clip path string. This is using exactly same format as "d" attribute 151 * in the SVG's path data.</dd> 152 * </dl></dd> 153 * </dl> 154 * <li>Here is a simple VectorDrawable in this vectordrawable.xml file. 155 * <pre> 156 * <vector xmlns:android="http://schemas.android.com/apk/res/android" 157 * android:height="64dp" 158 * android:width="64dp" 159 * android:viewportHeight="600" 160 * android:viewportWidth="600" > 161 * <group 162 * android:name="rotationGroup" 163 * android:pivotX="300.0" 164 * android:pivotY="300.0" 165 * android:rotation="45.0" > 166 * <path 167 * android:name="v" 168 * android:fillColor="#000000" 169 * android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" /> 170 * </group> 171 * </vector> 172 * </pre></li> 173 */ 174 175public class VectorDrawable extends Drawable { 176 private static final String LOGTAG = VectorDrawable.class.getSimpleName(); 177 178 private static final String SHAPE_CLIP_PATH = "clip-path"; 179 private static final String SHAPE_GROUP = "group"; 180 private static final String SHAPE_PATH = "path"; 181 private static final String SHAPE_VECTOR = "vector"; 182 183 private static final int LINECAP_BUTT = 0; 184 private static final int LINECAP_ROUND = 1; 185 private static final int LINECAP_SQUARE = 2; 186 187 private static final int LINEJOIN_MITER = 0; 188 private static final int LINEJOIN_ROUND = 1; 189 private static final int LINEJOIN_BEVEL = 2; 190 191 private static final boolean DBG_VECTOR_DRAWABLE = false; 192 193 private VectorDrawableState mVectorState; 194 195 private PorterDuffColorFilter mTintFilter; 196 197 private boolean mMutated; 198 199 // AnimatedVectorDrawable needs to turn off the cache all the time, otherwise, 200 // caching the bitmap by default is allowed. 201 private boolean mAllowCaching = true; 202 203 public VectorDrawable() { 204 mVectorState = new VectorDrawableState(); 205 } 206 207 private VectorDrawable(VectorDrawableState state, Resources res, Theme theme) { 208 if (theme != null && state.canApplyTheme()) { 209 // If we need to apply a theme, implicitly mutate. 210 mVectorState = new VectorDrawableState(state); 211 applyTheme(theme); 212 } else { 213 mVectorState = state; 214 } 215 216 mTintFilter = updateTintFilter(mTintFilter, state.mTint, state.mTintMode); 217 mVectorState.mVPathRenderer.setColorFilter(mTintFilter); 218 } 219 220 @Override 221 public Drawable mutate() { 222 if (!mMutated && super.mutate() == this) { 223 mVectorState = new VectorDrawableState(mVectorState); 224 mMutated = true; 225 } 226 return this; 227 } 228 229 Object getTargetByName(String name) { 230 return mVectorState.mVPathRenderer.mVGTargetsMap.get(name); 231 } 232 233 @Override 234 public ConstantState getConstantState() { 235 mVectorState.mChangingConfigurations = getChangingConfigurations(); 236 return mVectorState; 237 } 238 239 @Override 240 public void draw(Canvas canvas) { 241 final Rect bounds = getBounds(); 242 if (bounds.width() == 0 || bounds.height() == 0) { 243 // too small to draw 244 return; 245 } 246 247 final int saveCount = canvas.save(); 248 final boolean needMirroring = needMirroring(); 249 250 canvas.translate(bounds.left, bounds.top); 251 if (needMirroring) { 252 canvas.translate(bounds.width(), 0); 253 canvas.scale(-1.0f, 1.0f); 254 } 255 256 if (!mAllowCaching) { 257 mVectorState.mVPathRenderer.draw(canvas, bounds.width(), bounds.height()); 258 } else { 259 Bitmap bitmap = mVectorState.mCachedBitmap; 260 if (bitmap == null || !mVectorState.canReuseCache(bounds.width(), 261 bounds.height())) { 262 bitmap = Bitmap.createBitmap(bounds.width(), bounds.height(), 263 Bitmap.Config.ARGB_8888); 264 Canvas tmpCanvas = new Canvas(bitmap); 265 mVectorState.mVPathRenderer.draw(tmpCanvas, bounds.width(), bounds.height()); 266 mVectorState.mCachedBitmap = bitmap; 267 268 mVectorState.updateCacheStates(); 269 } 270 // The bitmap's size is the same as the bounds. 271 canvas.drawBitmap(bitmap, 0, 0, null); 272 } 273 274 canvas.restoreToCount(saveCount); 275 } 276 277 @Override 278 public int getAlpha() { 279 return mVectorState.mVPathRenderer.getRootAlpha(); 280 } 281 282 @Override 283 public void setAlpha(int alpha) { 284 if (mVectorState.mVPathRenderer.getRootAlpha() != alpha) { 285 mVectorState.mVPathRenderer.setRootAlpha(alpha); 286 invalidateSelf(); 287 } 288 } 289 290 @Override 291 public void setColorFilter(ColorFilter colorFilter) { 292 final VectorDrawableState state = mVectorState; 293 if (colorFilter != null) { 294 // Color filter overrides tint. 295 mTintFilter = null; 296 } else if (state.mTint != null && state.mTintMode != null) { 297 // Restore the tint filter, if we need one. 298 final int color = state.mTint.getColorForState(getState(), Color.TRANSPARENT); 299 mTintFilter = new PorterDuffColorFilter(color, state.mTintMode); 300 colorFilter = mTintFilter; 301 } 302 303 state.mVPathRenderer.setColorFilter(colorFilter); 304 invalidateSelf(); 305 } 306 307 @Override 308 public void setTintList(ColorStateList tint) { 309 final VectorDrawableState state = mVectorState; 310 if (state.mTint != tint) { 311 state.mTint = tint; 312 mTintFilter = updateTintFilter(mTintFilter, tint, state.mTintMode); 313 state.mVPathRenderer.setColorFilter(mTintFilter); 314 invalidateSelf(); 315 } 316 } 317 318 @Override 319 public void setTintMode(Mode tintMode) { 320 final VectorDrawableState state = mVectorState; 321 if (state.mTintMode != tintMode) { 322 state.mTintMode = tintMode; 323 mTintFilter = updateTintFilter(mTintFilter, state.mTint, tintMode); 324 state.mVPathRenderer.setColorFilter(mTintFilter); 325 invalidateSelf(); 326 } 327 } 328 329 @Override 330 protected boolean onStateChange(int[] stateSet) { 331 final VectorDrawableState state = mVectorState; 332 if (state.mTint != null && state.mTintMode != null) { 333 mTintFilter = updateTintFilter(mTintFilter, state.mTint, state.mTintMode); 334 mVectorState.mVPathRenderer.setColorFilter(mTintFilter); 335 return true; 336 } 337 return false; 338 } 339 340 @Override 341 public int getOpacity() { 342 return PixelFormat.TRANSLUCENT; 343 } 344 345 @Override 346 public int getIntrinsicWidth() { 347 return (int) mVectorState.mVPathRenderer.mBaseWidth; 348 } 349 350 @Override 351 public int getIntrinsicHeight() { 352 return (int) mVectorState.mVPathRenderer.mBaseHeight; 353 } 354 355 @Override 356 public boolean canApplyTheme() { 357 return super.canApplyTheme() || mVectorState != null && mVectorState.canApplyTheme(); 358 } 359 360 @Override 361 public void applyTheme(Theme t) { 362 super.applyTheme(t); 363 364 final VectorDrawableState state = mVectorState; 365 final VPathRenderer path = state.mVPathRenderer; 366 if (path != null && path.canApplyTheme()) { 367 path.applyTheme(t); 368 } 369 } 370 371 /** 372 * The size of a pixel when scaled from the intrinsic dimension to the viewport dimension. 373 * This is used to calculate the path animation accuracy. 374 * 375 * @hide 376 */ 377 public float getPixelSize() { 378 if (mVectorState == null && mVectorState.mVPathRenderer == null || 379 mVectorState.mVPathRenderer.mBaseWidth == 0 || 380 mVectorState.mVPathRenderer.mBaseHeight == 0 || 381 mVectorState.mVPathRenderer.mViewportHeight == 0 || 382 mVectorState.mVPathRenderer.mViewportWidth == 0) { 383 return 1; // fall back to 1:1 pixel mapping. 384 } 385 float intrinsicWidth = mVectorState.mVPathRenderer.mBaseWidth; 386 float intrinsicHeight = mVectorState.mVPathRenderer.mBaseHeight; 387 float viewportWidth = mVectorState.mVPathRenderer.mViewportWidth; 388 float viewportHeight = mVectorState.mVPathRenderer.mViewportHeight; 389 float scaleX = viewportWidth / intrinsicWidth; 390 float scaleY = viewportHeight / intrinsicHeight; 391 return Math.min(scaleX, scaleY); 392 } 393 394 /** @hide */ 395 public static VectorDrawable create(Resources resources, int rid) { 396 try { 397 final XmlPullParser parser = resources.getXml(rid); 398 final AttributeSet attrs = Xml.asAttributeSet(parser); 399 int type; 400 while ((type=parser.next()) != XmlPullParser.START_TAG && 401 type != XmlPullParser.END_DOCUMENT) { 402 // Empty loop 403 } 404 if (type != XmlPullParser.START_TAG) { 405 throw new XmlPullParserException("No start tag found"); 406 } 407 408 final VectorDrawable drawable = new VectorDrawable(); 409 drawable.inflate(resources, parser, attrs); 410 411 return drawable; 412 } catch (XmlPullParserException e) { 413 Log.e(LOGTAG, "parser error", e); 414 } catch (IOException e) { 415 Log.e(LOGTAG, "parser error", e); 416 } 417 return null; 418 } 419 420 private static int applyAlpha(int color, float alpha) { 421 int alphaBytes = Color.alpha(color); 422 color &= 0x00FFFFFF; 423 color |= ((int) (alphaBytes * alpha)) << 24; 424 return color; 425 } 426 427 @Override 428 public void inflate(Resources res, XmlPullParser parser, AttributeSet attrs, Theme theme) 429 throws XmlPullParserException, IOException { 430 final VectorDrawableState state = mVectorState; 431 final VPathRenderer pathRenderer = new VPathRenderer(); 432 state.mVPathRenderer = pathRenderer; 433 434 TypedArray a = obtainAttributes(res, theme, attrs, R.styleable.VectorDrawable); 435 updateStateFromTypedArray(a); 436 a.recycle(); 437 438 state.mCacheDirty = true; 439 inflateInternal(res, parser, attrs, theme); 440 441 mTintFilter = updateTintFilter(mTintFilter, state.mTint, state.mTintMode); 442 state.mVPathRenderer.setColorFilter(mTintFilter); 443 } 444 445 private void updateStateFromTypedArray(TypedArray a) throws XmlPullParserException { 446 final VectorDrawableState state = mVectorState; 447 final VPathRenderer pathRenderer = state.mVPathRenderer; 448 449 // Account for any configuration changes. 450 state.mChangingConfigurations |= a.getChangingConfigurations(); 451 452 // Extract the theme attributes, if any. 453 state.mThemeAttrs = a.extractThemeAttrs(); 454 455 final int tintMode = a.getInt(R.styleable.VectorDrawable_tintMode, -1); 456 if (tintMode != -1) { 457 state.mTintMode = Drawable.parseTintMode(tintMode, Mode.SRC_IN); 458 } 459 460 final ColorStateList tint = a.getColorStateList(R.styleable.VectorDrawable_tint); 461 if (tint != null) { 462 state.mTint = tint; 463 } 464 465 state.mAutoMirrored = a.getBoolean( 466 R.styleable.VectorDrawable_autoMirrored, state.mAutoMirrored); 467 468 pathRenderer.mViewportWidth = a.getFloat( 469 R.styleable.VectorDrawable_viewportWidth, pathRenderer.mViewportWidth); 470 pathRenderer.mViewportHeight = a.getFloat( 471 R.styleable.VectorDrawable_viewportHeight, pathRenderer.mViewportHeight); 472 473 if (pathRenderer.mViewportWidth <= 0) { 474 throw new XmlPullParserException(a.getPositionDescription() + 475 "<vector> tag requires viewportWidth > 0"); 476 } else if (pathRenderer.mViewportHeight <= 0) { 477 throw new XmlPullParserException(a.getPositionDescription() + 478 "<vector> tag requires viewportHeight > 0"); 479 } 480 481 pathRenderer.mBaseWidth = a.getDimension( 482 R.styleable.VectorDrawable_width, pathRenderer.mBaseWidth); 483 pathRenderer.mBaseHeight = a.getDimension( 484 R.styleable.VectorDrawable_height, pathRenderer.mBaseHeight); 485 486 if (pathRenderer.mBaseWidth <= 0) { 487 throw new XmlPullParserException(a.getPositionDescription() + 488 "<vector> tag requires width > 0"); 489 } else if (pathRenderer.mBaseHeight <= 0) { 490 throw new XmlPullParserException(a.getPositionDescription() + 491 "<vector> tag requires height > 0"); 492 } 493 } 494 495 private void inflateInternal(Resources res, XmlPullParser parser, AttributeSet attrs, 496 Theme theme) throws XmlPullParserException, IOException { 497 final VectorDrawableState state = mVectorState; 498 final VPathRenderer pathRenderer = state.mVPathRenderer; 499 boolean noPathTag = true; 500 501 // Use a stack to help to build the group tree. 502 // The top of the stack is always the current group. 503 final Stack<VGroup> groupStack = new Stack<VGroup>(); 504 groupStack.push(pathRenderer.mRootGroup); 505 506 int eventType = parser.getEventType(); 507 while (eventType != XmlPullParser.END_DOCUMENT) { 508 if (eventType == XmlPullParser.START_TAG) { 509 final String tagName = parser.getName(); 510 final VGroup currentGroup = groupStack.peek(); 511 512 if (SHAPE_PATH.equals(tagName)) { 513 final VFullPath path = new VFullPath(); 514 path.inflate(res, attrs, theme); 515 currentGroup.mChildren.add(path); 516 if (path.getPathName() != null) { 517 pathRenderer.mVGTargetsMap.put(path.getPathName(), path); 518 } 519 noPathTag = false; 520 state.mChangingConfigurations |= path.mChangingConfigurations; 521 } else if (SHAPE_CLIP_PATH.equals(tagName)) { 522 final VClipPath path = new VClipPath(); 523 path.inflate(res, attrs, theme); 524 currentGroup.mChildren.add(path); 525 if (path.getPathName() != null) { 526 pathRenderer.mVGTargetsMap.put(path.getPathName(), path); 527 } 528 state.mChangingConfigurations |= path.mChangingConfigurations; 529 } else if (SHAPE_GROUP.equals(tagName)) { 530 VGroup newChildGroup = new VGroup(); 531 newChildGroup.inflate(res, attrs, theme); 532 currentGroup.mChildren.add(newChildGroup); 533 groupStack.push(newChildGroup); 534 if (newChildGroup.getGroupName() != null) { 535 pathRenderer.mVGTargetsMap.put(newChildGroup.getGroupName(), 536 newChildGroup); 537 } 538 state.mChangingConfigurations |= newChildGroup.mChangingConfigurations; 539 } 540 } else if (eventType == XmlPullParser.END_TAG) { 541 final String tagName = parser.getName(); 542 if (SHAPE_GROUP.equals(tagName)) { 543 groupStack.pop(); 544 } 545 } 546 eventType = parser.next(); 547 } 548 549 // Print the tree out for debug. 550 if (DBG_VECTOR_DRAWABLE) { 551 printGroupTree(pathRenderer.mRootGroup, 0); 552 } 553 554 if (noPathTag) { 555 final StringBuffer tag = new StringBuffer(); 556 557 if (tag.length() > 0) { 558 tag.append(" or "); 559 } 560 tag.append(SHAPE_PATH); 561 562 throw new XmlPullParserException("no " + tag + " defined"); 563 } 564 } 565 566 private void printGroupTree(VGroup currentGroup, int level) { 567 String indent = ""; 568 for (int i = 0; i < level; i++) { 569 indent += " "; 570 } 571 // Print the current node 572 Log.v(LOGTAG, indent + "current group is :" + currentGroup.getGroupName() 573 + " rotation is " + currentGroup.mRotate); 574 Log.v(LOGTAG, indent + "matrix is :" + currentGroup.getLocalMatrix().toString()); 575 // Then print all the children groups 576 for (int i = 0; i < currentGroup.mChildren.size(); i++) { 577 Object child = currentGroup.mChildren.get(i); 578 if (child instanceof VGroup) { 579 printGroupTree((VGroup) child, level + 1); 580 } 581 } 582 } 583 584 @Override 585 public int getChangingConfigurations() { 586 return super.getChangingConfigurations() | mVectorState.mChangingConfigurations; 587 } 588 589 void setAllowCaching(boolean allowCaching) { 590 mAllowCaching = allowCaching; 591 } 592 593 private boolean needMirroring() { 594 return isAutoMirrored() && getLayoutDirection() == LayoutDirection.RTL; 595 } 596 597 @Override 598 public void setAutoMirrored(boolean mirrored) { 599 if (mVectorState.mAutoMirrored != mirrored) { 600 mVectorState.mAutoMirrored = mirrored; 601 invalidateSelf(); 602 } 603 } 604 605 @Override 606 public boolean isAutoMirrored() { 607 return mVectorState.mAutoMirrored; 608 } 609 610 private static class VectorDrawableState extends ConstantState { 611 int[] mThemeAttrs; 612 int mChangingConfigurations; 613 VPathRenderer mVPathRenderer; 614 ColorStateList mTint; 615 Mode mTintMode; 616 boolean mAutoMirrored; 617 618 Bitmap mCachedBitmap; 619 int[] mCachedThemeAttrs; 620 ColorStateList mCachedTint; 621 Mode mCachedTintMode; 622 int mCachedRootAlpha; 623 boolean mCachedAutoMirrored; 624 boolean mCacheDirty; 625 626 // Deep copy for mutate() or implicitly mutate. 627 public VectorDrawableState(VectorDrawableState copy) { 628 if (copy != null) { 629 mThemeAttrs = copy.mThemeAttrs; 630 mChangingConfigurations = copy.mChangingConfigurations; 631 mVPathRenderer = new VPathRenderer(copy.mVPathRenderer); 632 mTint = copy.mTint; 633 mTintMode = copy.mTintMode; 634 mAutoMirrored = copy.mAutoMirrored; 635 } 636 } 637 638 public boolean canReuseCache(int width, int height) { 639 if (!mCacheDirty 640 && mCachedThemeAttrs == mThemeAttrs 641 && mCachedTint == mTint 642 && mCachedTintMode == mTintMode 643 && mCachedAutoMirrored == mAutoMirrored 644 && width == mCachedBitmap.getWidth() 645 && height == mCachedBitmap.getHeight() 646 && mCachedRootAlpha == mVPathRenderer.getRootAlpha()) { 647 return true; 648 } 649 return false; 650 } 651 652 public void updateCacheStates() { 653 // Use shallow copy here and shallow comparison in canReuseCache(), 654 // likely hit cache miss more, but practically not much difference. 655 mCachedThemeAttrs = mThemeAttrs; 656 mCachedTint = mTint; 657 mCachedTintMode = mTintMode; 658 mCachedRootAlpha = mVPathRenderer.getRootAlpha(); 659 mCachedAutoMirrored = mAutoMirrored; 660 mCacheDirty = false; 661 } 662 663 public VectorDrawableState() { 664 mVPathRenderer = new VPathRenderer(); 665 } 666 667 @Override 668 public Drawable newDrawable() { 669 return new VectorDrawable(this, null, null); 670 } 671 672 @Override 673 public Drawable newDrawable(Resources res) { 674 return new VectorDrawable(this, res, null); 675 } 676 677 @Override 678 public Drawable newDrawable(Resources res, Theme theme) { 679 return new VectorDrawable(this, res, theme); 680 } 681 682 @Override 683 public int getChangingConfigurations() { 684 return mChangingConfigurations; 685 } 686 } 687 688 private static class VPathRenderer { 689 /* Right now the internal data structure is organized as a tree. 690 * Each node can be a group node, or a path. 691 * A group node can have groups or paths as children, but a path node has 692 * no children. 693 * One example can be: 694 * Root Group 695 * / | \ 696 * Group Path Group 697 * / \ | 698 * Path Path Path 699 * 700 */ 701 // Variables that only used temporarily inside the draw() call, so there 702 // is no need for deep copying. 703 private final Path mPath = new Path(); 704 private final Path mRenderPath = new Path(); 705 private static final Matrix IDENTITY_MATRIX = new Matrix(); 706 private final Matrix mFinalPathMatrix = new Matrix(); 707 708 private Paint mStrokePaint; 709 private Paint mFillPaint; 710 private ColorFilter mColorFilter; 711 private PathMeasure mPathMeasure; 712 713 ///////////////////////////////////////////////////// 714 // Variables below need to be copied (deep copy if applicable) for mutation. 715 private int mChangingConfigurations; 716 private final VGroup mRootGroup; 717 float mBaseWidth = 0; 718 float mBaseHeight = 0; 719 float mViewportWidth = 0; 720 float mViewportHeight = 0; 721 private int mRootAlpha = 0xFF; 722 723 final ArrayMap<String, Object> mVGTargetsMap = new ArrayMap<String, Object>(); 724 725 public VPathRenderer() { 726 mRootGroup = new VGroup(); 727 } 728 729 public void setRootAlpha(int alpha) { 730 mRootAlpha = alpha; 731 } 732 733 public int getRootAlpha() { 734 return mRootAlpha; 735 } 736 737 public VPathRenderer(VPathRenderer copy) { 738 mRootGroup = new VGroup(copy.mRootGroup, mVGTargetsMap); 739 mBaseWidth = copy.mBaseWidth; 740 mBaseHeight = copy.mBaseHeight; 741 mViewportWidth = copy.mViewportWidth; 742 mViewportHeight = copy.mViewportHeight; 743 mChangingConfigurations = copy.mChangingConfigurations; 744 mRootAlpha = copy.mRootAlpha; 745 } 746 747 public boolean canApplyTheme() { 748 // If one of the paths can apply theme, then return true; 749 return recursiveCanApplyTheme(mRootGroup); 750 } 751 752 private boolean recursiveCanApplyTheme(VGroup currentGroup) { 753 // We can do a tree traverse here, if there is one path return true, 754 // then we return true for the whole tree. 755 final ArrayList<Object> children = currentGroup.mChildren; 756 757 for (int i = 0; i < children.size(); i++) { 758 Object child = children.get(i); 759 if (child instanceof VGroup) { 760 VGroup childGroup = (VGroup) child; 761 if (childGroup.canApplyTheme() 762 || recursiveCanApplyTheme(childGroup)) { 763 return true; 764 } 765 } else if (child instanceof VPath) { 766 VPath childPath = (VPath) child; 767 if (childPath.canApplyTheme()) { 768 return true; 769 } 770 } 771 } 772 return false; 773 } 774 775 public void applyTheme(Theme t) { 776 // Apply theme to every path of the tree. 777 recursiveApplyTheme(mRootGroup, t); 778 } 779 780 private void recursiveApplyTheme(VGroup currentGroup, Theme t) { 781 // We can do a tree traverse here, apply theme to all paths which 782 // can apply theme. 783 final ArrayList<Object> children = currentGroup.mChildren; 784 for (int i = 0; i < children.size(); i++) { 785 Object child = children.get(i); 786 if (child instanceof VGroup) { 787 VGroup childGroup = (VGroup) child; 788 if (childGroup.canApplyTheme()) { 789 childGroup.applyTheme(t); 790 } 791 recursiveApplyTheme(childGroup, t); 792 } else if (child instanceof VPath) { 793 VPath childPath = (VPath) child; 794 if (childPath.canApplyTheme()) { 795 childPath.applyTheme(t); 796 } 797 } 798 } 799 } 800 801 public void setColorFilter(ColorFilter colorFilter) { 802 mColorFilter = colorFilter; 803 804 if (mFillPaint != null) { 805 mFillPaint.setColorFilter(colorFilter); 806 } 807 808 if (mStrokePaint != null) { 809 mStrokePaint.setColorFilter(colorFilter); 810 } 811 812 } 813 814 private void drawGroupTree(VGroup currentGroup, Matrix currentMatrix, 815 float currentAlpha, Canvas canvas, int w, int h) { 816 // Calculate current group's matrix by preConcat the parent's and 817 // and the current one on the top of the stack. 818 // Basically the Mfinal = Mviewport * M0 * M1 * M2; 819 // Mi the local matrix at level i of the group tree. 820 currentGroup.mStackedMatrix.set(currentMatrix); 821 822 currentGroup.mStackedMatrix.preConcat(currentGroup.mLocalMatrix); 823 824 float stackedAlpha = currentAlpha * currentGroup.mGroupAlpha; 825 826 // Draw the group tree in the same order as the XML file. 827 for (int i = 0; i < currentGroup.mChildren.size(); i++) { 828 Object child = currentGroup.mChildren.get(i); 829 if (child instanceof VGroup) { 830 VGroup childGroup = (VGroup) child; 831 drawGroupTree(childGroup, currentGroup.mStackedMatrix, 832 stackedAlpha, canvas, w, h); 833 } else if (child instanceof VPath) { 834 VPath childPath = (VPath) child; 835 drawPath(currentGroup, childPath, stackedAlpha, canvas, w, h); 836 } 837 } 838 } 839 840 public void draw(Canvas canvas, int w, int h) { 841 // Travese the tree in pre-order to draw. 842 drawGroupTree(mRootGroup, IDENTITY_MATRIX, ((float) mRootAlpha) / 0xFF, 843 canvas, w, h); 844 } 845 846 private void drawPath(VGroup vGroup, VPath vPath, float stackedAlpha, 847 Canvas canvas, int w, int h) { 848 final float scaleX = w / mViewportWidth; 849 final float scaleY = h / mViewportHeight; 850 final float minScale = Math.min(scaleX, scaleY); 851 852 mFinalPathMatrix.set(vGroup.mStackedMatrix); 853 mFinalPathMatrix.postScale(scaleX, scaleY); 854 855 vPath.toPath(mPath); 856 final Path path = mPath; 857 858 mRenderPath.reset(); 859 860 if (vPath.isClipPath()) { 861 mRenderPath.addPath(path, mFinalPathMatrix); 862 canvas.clipPath(mRenderPath, Region.Op.REPLACE); 863 } else { 864 VFullPath fullPath = (VFullPath) vPath; 865 if (fullPath.mTrimPathStart != 0.0f || fullPath.mTrimPathEnd != 1.0f) { 866 float start = (fullPath.mTrimPathStart + fullPath.mTrimPathOffset) % 1.0f; 867 float end = (fullPath.mTrimPathEnd + fullPath.mTrimPathOffset) % 1.0f; 868 869 if (mPathMeasure == null) { 870 mPathMeasure = new PathMeasure(); 871 } 872 mPathMeasure.setPath(mPath, false); 873 874 float len = mPathMeasure.getLength(); 875 start = start * len; 876 end = end * len; 877 path.reset(); 878 if (start > end) { 879 mPathMeasure.getSegment(start, len, path, true); 880 mPathMeasure.getSegment(0f, end, path, true); 881 } else { 882 mPathMeasure.getSegment(start, end, path, true); 883 } 884 path.rLineTo(0, 0); // fix bug in measure 885 } 886 mRenderPath.addPath(path, mFinalPathMatrix); 887 888 if (fullPath.mFillColor != Color.TRANSPARENT) { 889 if (mFillPaint == null) { 890 mFillPaint = new Paint(); 891 mFillPaint.setColorFilter(mColorFilter); 892 mFillPaint.setStyle(Paint.Style.FILL); 893 mFillPaint.setAntiAlias(true); 894 } 895 mFillPaint.setColor(applyAlpha(fullPath.mFillColor, stackedAlpha)); 896 canvas.drawPath(mRenderPath, mFillPaint); 897 } 898 899 if (fullPath.mStrokeColor != Color.TRANSPARENT) { 900 if (mStrokePaint == null) { 901 mStrokePaint = new Paint(); 902 mStrokePaint.setColorFilter(mColorFilter); 903 mStrokePaint.setStyle(Paint.Style.STROKE); 904 mStrokePaint.setAntiAlias(true); 905 } 906 907 final Paint strokePaint = mStrokePaint; 908 if (fullPath.mStrokeLineJoin != null) { 909 strokePaint.setStrokeJoin(fullPath.mStrokeLineJoin); 910 } 911 912 if (fullPath.mStrokeLineCap != null) { 913 strokePaint.setStrokeCap(fullPath.mStrokeLineCap); 914 } 915 916 strokePaint.setStrokeMiter(fullPath.mStrokeMiterlimit); 917 918 strokePaint.setColor(applyAlpha(fullPath.mStrokeColor, stackedAlpha)); 919 strokePaint.setStrokeWidth(fullPath.mStrokeWidth * minScale); 920 canvas.drawPath(mRenderPath, strokePaint); 921 } 922 } 923 } 924 } 925 926 private static class VGroup { 927 // mStackedMatrix is only used temporarily when drawing, it combines all 928 // the parents' local matrices with the current one. 929 private final Matrix mStackedMatrix = new Matrix(); 930 931 ///////////////////////////////////////////////////// 932 // Variables below need to be copied (deep copy if applicable) for mutation. 933 final ArrayList<Object> mChildren = new ArrayList<Object>(); 934 935 private float mRotate = 0; 936 private float mPivotX = 0; 937 private float mPivotY = 0; 938 private float mScaleX = 1; 939 private float mScaleY = 1; 940 private float mTranslateX = 0; 941 private float mTranslateY = 0; 942 private float mGroupAlpha = 1; 943 944 // mLocalMatrix is updated based on the update of transformation information, 945 // either parsed from the XML or by animation. 946 private final Matrix mLocalMatrix = new Matrix(); 947 private int mChangingConfigurations; 948 private int[] mThemeAttrs; 949 private String mGroupName = null; 950 951 public VGroup(VGroup copy, ArrayMap<String, Object> targetsMap) { 952 mRotate = copy.mRotate; 953 mPivotX = copy.mPivotX; 954 mPivotY = copy.mPivotY; 955 mScaleX = copy.mScaleX; 956 mScaleY = copy.mScaleY; 957 mTranslateX = copy.mTranslateX; 958 mTranslateY = copy.mTranslateY; 959 mGroupAlpha = copy.mGroupAlpha; 960 mThemeAttrs = copy.mThemeAttrs; 961 mGroupName = copy.mGroupName; 962 mChangingConfigurations = copy.mChangingConfigurations; 963 if (mGroupName != null) { 964 targetsMap.put(mGroupName, this); 965 } 966 967 mLocalMatrix.set(copy.mLocalMatrix); 968 969 final ArrayList<Object> children = copy.mChildren; 970 for (int i = 0; i < children.size(); i++) { 971 Object copyChild = children.get(i); 972 if (copyChild instanceof VGroup) { 973 VGroup copyGroup = (VGroup) copyChild; 974 mChildren.add(new VGroup(copyGroup, targetsMap)); 975 } else { 976 VPath newPath = null; 977 if (copyChild instanceof VFullPath) { 978 newPath = new VFullPath((VFullPath) copyChild); 979 } else if (copyChild instanceof VClipPath) { 980 newPath = new VClipPath((VClipPath) copyChild); 981 } else { 982 throw new IllegalStateException("Unknown object in the tree!"); 983 } 984 mChildren.add(newPath); 985 if (newPath.mPathName != null) { 986 targetsMap.put(newPath.mPathName, newPath); 987 } 988 } 989 } 990 } 991 992 public VGroup() { 993 } 994 995 public String getGroupName() { 996 return mGroupName; 997 } 998 999 public Matrix getLocalMatrix() { 1000 return mLocalMatrix; 1001 } 1002 1003 public boolean canApplyTheme() { 1004 return mThemeAttrs != null; 1005 } 1006 1007 public void inflate(Resources res, AttributeSet attrs, Theme theme) { 1008 final TypedArray a = obtainAttributes(res, theme, attrs, 1009 R.styleable.VectorDrawableGroup); 1010 updateStateFromTypedArray(a); 1011 a.recycle(); 1012 } 1013 1014 private void updateStateFromTypedArray(TypedArray a) { 1015 // Account for any configuration changes. 1016 mChangingConfigurations |= a.getChangingConfigurations(); 1017 1018 // Extract the theme attributes, if any. 1019 mThemeAttrs = a.extractThemeAttrs(); 1020 1021 mRotate = a.getFloat(R.styleable.VectorDrawableGroup_rotation, mRotate); 1022 mPivotX = a.getFloat(R.styleable.VectorDrawableGroup_pivotX, mPivotX); 1023 mPivotY = a.getFloat(R.styleable.VectorDrawableGroup_pivotY, mPivotY); 1024 mScaleX = a.getFloat(R.styleable.VectorDrawableGroup_scaleX, mScaleX); 1025 mScaleY = a.getFloat(R.styleable.VectorDrawableGroup_scaleY, mScaleY); 1026 mTranslateX = a.getFloat(R.styleable.VectorDrawableGroup_translateX, mTranslateX); 1027 mTranslateY = a.getFloat(R.styleable.VectorDrawableGroup_translateY, mTranslateY); 1028 mGroupAlpha = a.getFloat(R.styleable.VectorDrawableGroup_alpha, mGroupAlpha); 1029 1030 final String groupName = a.getString(R.styleable.VectorDrawableGroup_name); 1031 if (groupName != null) { 1032 mGroupName = groupName; 1033 } 1034 1035 updateLocalMatrix(); 1036 } 1037 1038 public void applyTheme(Theme t) { 1039 if (mThemeAttrs == null) { 1040 return; 1041 } 1042 1043 final TypedArray a = t.resolveAttributes(mThemeAttrs, 1044 R.styleable.VectorDrawableGroup); 1045 updateStateFromTypedArray(a); 1046 a.recycle(); 1047 } 1048 1049 private void updateLocalMatrix() { 1050 // The order we apply is the same as the 1051 // RenderNode.cpp::applyViewPropertyTransforms(). 1052 mLocalMatrix.reset(); 1053 mLocalMatrix.postTranslate(-mPivotX, -mPivotY); 1054 mLocalMatrix.postScale(mScaleX, mScaleY); 1055 mLocalMatrix.postRotate(mRotate, 0, 0); 1056 mLocalMatrix.postTranslate(mTranslateX + mPivotX, mTranslateY + mPivotY); 1057 } 1058 1059 /* Setters and Getters, used by animator from AnimatedVectorDrawable. */ 1060 @SuppressWarnings("unused") 1061 public float getRotation() { 1062 return mRotate; 1063 } 1064 1065 @SuppressWarnings("unused") 1066 public void setRotation(float rotation) { 1067 if (rotation != mRotate) { 1068 mRotate = rotation; 1069 updateLocalMatrix(); 1070 } 1071 } 1072 1073 @SuppressWarnings("unused") 1074 public float getPivotX() { 1075 return mPivotX; 1076 } 1077 1078 @SuppressWarnings("unused") 1079 public void setPivotX(float pivotX) { 1080 if (pivotX != mPivotX) { 1081 mPivotX = pivotX; 1082 updateLocalMatrix(); 1083 } 1084 } 1085 1086 @SuppressWarnings("unused") 1087 public float getPivotY() { 1088 return mPivotY; 1089 } 1090 1091 @SuppressWarnings("unused") 1092 public void setPivotY(float pivotY) { 1093 if (pivotY != mPivotY) { 1094 mPivotY = pivotY; 1095 updateLocalMatrix(); 1096 } 1097 } 1098 1099 @SuppressWarnings("unused") 1100 public float getScaleX() { 1101 return mScaleX; 1102 } 1103 1104 @SuppressWarnings("unused") 1105 public void setScaleX(float scaleX) { 1106 if (scaleX != mScaleX) { 1107 mScaleX = scaleX; 1108 updateLocalMatrix(); 1109 } 1110 } 1111 1112 @SuppressWarnings("unused") 1113 public float getScaleY() { 1114 return mScaleY; 1115 } 1116 1117 @SuppressWarnings("unused") 1118 public void setScaleY(float scaleY) { 1119 if (scaleY != mScaleY) { 1120 mScaleY = scaleY; 1121 updateLocalMatrix(); 1122 } 1123 } 1124 1125 @SuppressWarnings("unused") 1126 public float getTranslateX() { 1127 return mTranslateX; 1128 } 1129 1130 @SuppressWarnings("unused") 1131 public void setTranslateX(float translateX) { 1132 if (translateX != mTranslateX) { 1133 mTranslateX = translateX; 1134 updateLocalMatrix(); 1135 } 1136 } 1137 1138 @SuppressWarnings("unused") 1139 public float getTranslateY() { 1140 return mTranslateY; 1141 } 1142 1143 @SuppressWarnings("unused") 1144 public void setTranslateY(float translateY) { 1145 if (translateY != mTranslateY) { 1146 mTranslateY = translateY; 1147 updateLocalMatrix(); 1148 } 1149 } 1150 1151 @SuppressWarnings("unused") 1152 public float getAlpha() { 1153 return mGroupAlpha; 1154 } 1155 1156 @SuppressWarnings("unused") 1157 public void setAlpha(float groupAlpha) { 1158 if (groupAlpha != mGroupAlpha) { 1159 mGroupAlpha = groupAlpha; 1160 } 1161 } 1162 } 1163 1164 /** 1165 * Common Path information for clip path and normal path. 1166 */ 1167 private static class VPath { 1168 protected PathParser.PathDataNode[] mNodes = null; 1169 String mPathName; 1170 int mChangingConfigurations; 1171 1172 public VPath() { 1173 // Empty constructor. 1174 } 1175 1176 public VPath(VPath copy) { 1177 mPathName = copy.mPathName; 1178 mChangingConfigurations = copy.mChangingConfigurations; 1179 mNodes = PathParser.deepCopyNodes(copy.mNodes); 1180 } 1181 1182 public void toPath(Path path) { 1183 path.reset(); 1184 if (mNodes != null) { 1185 PathParser.PathDataNode.nodesToPath(mNodes, path); 1186 } 1187 } 1188 1189 public String getPathName() { 1190 return mPathName; 1191 } 1192 1193 public boolean canApplyTheme() { 1194 return false; 1195 } 1196 1197 public void applyTheme(Theme t) { 1198 } 1199 1200 public boolean isClipPath() { 1201 return false; 1202 } 1203 1204 /* Setters and Getters, used by animator from AnimatedVectorDrawable. */ 1205 @SuppressWarnings("unused") 1206 public PathParser.PathDataNode[] getPathData() { 1207 return mNodes; 1208 } 1209 1210 @SuppressWarnings("unused") 1211 public void setPathData(PathParser.PathDataNode[] nodes) { 1212 if (!PathParser.canMorph(mNodes, nodes)) { 1213 // This should not happen in the middle of animation. 1214 mNodes = PathParser.deepCopyNodes(nodes); 1215 } else { 1216 PathParser.updateNodes(mNodes, nodes); 1217 } 1218 } 1219 } 1220 1221 /** 1222 * Clip path, which only has name and pathData. 1223 */ 1224 private static class VClipPath extends VPath{ 1225 public VClipPath() { 1226 // Empty constructor. 1227 } 1228 1229 public VClipPath(VClipPath copy) { 1230 super(copy); 1231 } 1232 1233 public void inflate(Resources r, AttributeSet attrs, Theme theme) { 1234 final TypedArray a = obtainAttributes(r, theme, attrs, 1235 R.styleable.VectorDrawableClipPath); 1236 updateStateFromTypedArray(a); 1237 a.recycle(); 1238 } 1239 1240 private void updateStateFromTypedArray(TypedArray a) { 1241 // Account for any configuration changes. 1242 mChangingConfigurations |= a.getChangingConfigurations(); 1243 1244 mPathName = a.getString(R.styleable.VectorDrawableClipPath_name); 1245 mNodes = PathParser.createNodesFromPathData(a.getString( 1246 R.styleable.VectorDrawableClipPath_pathData)); 1247 } 1248 1249 @Override 1250 public boolean isClipPath() { 1251 return true; 1252 } 1253 } 1254 1255 /** 1256 * Normal path, which contains all the fill / paint information. 1257 */ 1258 private static class VFullPath extends VPath { 1259 ///////////////////////////////////////////////////// 1260 // Variables below need to be copied (deep copy if applicable) for mutation. 1261 private int[] mThemeAttrs; 1262 1263 int mStrokeColor = Color.TRANSPARENT; 1264 float mStrokeWidth = 0; 1265 int mFillColor = Color.TRANSPARENT; 1266 int mFillRule; 1267 float mTrimPathStart = 0; 1268 float mTrimPathEnd = 1; 1269 float mTrimPathOffset = 0; 1270 1271 Paint.Cap mStrokeLineCap = Paint.Cap.BUTT; 1272 Paint.Join mStrokeLineJoin = Paint.Join.MITER; 1273 float mStrokeMiterlimit = 4; 1274 1275 public VFullPath() { 1276 // Empty constructor. 1277 } 1278 1279 public VFullPath(VFullPath copy) { 1280 super(copy); 1281 mThemeAttrs = copy.mThemeAttrs; 1282 1283 mStrokeColor = copy.mStrokeColor; 1284 mStrokeWidth = copy.mStrokeWidth; 1285 mFillColor = copy.mFillColor; 1286 mFillRule = copy.mFillRule; 1287 mTrimPathStart = copy.mTrimPathStart; 1288 mTrimPathEnd = copy.mTrimPathEnd; 1289 mTrimPathOffset = copy.mTrimPathOffset; 1290 1291 mStrokeLineCap = copy.mStrokeLineCap; 1292 mStrokeLineJoin = copy.mStrokeLineJoin; 1293 mStrokeMiterlimit = copy.mStrokeMiterlimit; 1294 } 1295 1296 private Paint.Cap getStrokeLineCap(int id, Paint.Cap defValue) { 1297 switch (id) { 1298 case LINECAP_BUTT: 1299 return Paint.Cap.BUTT; 1300 case LINECAP_ROUND: 1301 return Paint.Cap.ROUND; 1302 case LINECAP_SQUARE: 1303 return Paint.Cap.SQUARE; 1304 default: 1305 return defValue; 1306 } 1307 } 1308 1309 private Paint.Join getStrokeLineJoin(int id, Paint.Join defValue) { 1310 switch (id) { 1311 case LINEJOIN_MITER: 1312 return Paint.Join.MITER; 1313 case LINEJOIN_ROUND: 1314 return Paint.Join.ROUND; 1315 case LINEJOIN_BEVEL: 1316 return Paint.Join.BEVEL; 1317 default: 1318 return defValue; 1319 } 1320 } 1321 1322 @Override 1323 public boolean canApplyTheme() { 1324 return mThemeAttrs != null; 1325 } 1326 1327 public void inflate(Resources r, AttributeSet attrs, Theme theme) { 1328 final TypedArray a = obtainAttributes(r, theme, attrs, 1329 R.styleable.VectorDrawablePath); 1330 updateStateFromTypedArray(a); 1331 a.recycle(); 1332 } 1333 1334 private void updateStateFromTypedArray(TypedArray a) { 1335 // Account for any configuration changes. 1336 mChangingConfigurations |= a.getChangingConfigurations(); 1337 1338 // Extract the theme attributes, if any. 1339 mThemeAttrs = a.extractThemeAttrs(); 1340 1341 mPathName = a.getString(R.styleable.VectorDrawablePath_name); 1342 mNodes = PathParser.createNodesFromPathData(a.getString( 1343 R.styleable.VectorDrawablePath_pathData)); 1344 1345 mFillColor = a.getColor(R.styleable.VectorDrawablePath_fillColor, 1346 mFillColor); 1347 mStrokeLineCap = getStrokeLineCap(a.getInt( 1348 R.styleable.VectorDrawablePath_strokeLineCap, -1), mStrokeLineCap); 1349 mStrokeLineJoin = getStrokeLineJoin(a.getInt( 1350 R.styleable.VectorDrawablePath_strokeLineJoin, -1), mStrokeLineJoin); 1351 mStrokeMiterlimit = a.getFloat( 1352 R.styleable.VectorDrawablePath_strokeMiterLimit, mStrokeMiterlimit); 1353 mStrokeColor = a.getColor(R.styleable.VectorDrawablePath_strokeColor, 1354 mStrokeColor); 1355 mStrokeWidth = a.getFloat(R.styleable.VectorDrawablePath_strokeWidth, 1356 mStrokeWidth); 1357 mTrimPathEnd = a.getFloat(R.styleable.VectorDrawablePath_trimPathEnd, 1358 mTrimPathEnd); 1359 mTrimPathOffset = a.getFloat( 1360 R.styleable.VectorDrawablePath_trimPathOffset, mTrimPathOffset); 1361 mTrimPathStart = a.getFloat( 1362 R.styleable.VectorDrawablePath_trimPathStart, mTrimPathStart); 1363 } 1364 1365 @Override 1366 public void applyTheme(Theme t) { 1367 if (mThemeAttrs == null) { 1368 return; 1369 } 1370 1371 final TypedArray a = t.resolveAttributes(mThemeAttrs, 1372 R.styleable.VectorDrawablePath); 1373 updateStateFromTypedArray(a); 1374 a.recycle(); 1375 } 1376 1377 /* Setters and Getters, used by animator from AnimatedVectorDrawable. */ 1378 @SuppressWarnings("unused") 1379 int getStroke() { 1380 return mStrokeColor; 1381 } 1382 1383 @SuppressWarnings("unused") 1384 void setStroke(int strokeColor) { 1385 mStrokeColor = strokeColor; 1386 } 1387 1388 @SuppressWarnings("unused") 1389 float getStrokeWidth() { 1390 return mStrokeWidth; 1391 } 1392 1393 @SuppressWarnings("unused") 1394 void setStrokeWidth(float strokeWidth) { 1395 mStrokeWidth = strokeWidth; 1396 } 1397 1398 @SuppressWarnings("unused") 1399 int getFill() { 1400 return mFillColor; 1401 } 1402 1403 @SuppressWarnings("unused") 1404 void setFill(int fillColor) { 1405 mFillColor = fillColor; 1406 } 1407 1408 @SuppressWarnings("unused") 1409 float getTrimPathStart() { 1410 return mTrimPathStart; 1411 } 1412 1413 @SuppressWarnings("unused") 1414 void setTrimPathStart(float trimPathStart) { 1415 mTrimPathStart = trimPathStart; 1416 } 1417 1418 @SuppressWarnings("unused") 1419 float getTrimPathEnd() { 1420 return mTrimPathEnd; 1421 } 1422 1423 @SuppressWarnings("unused") 1424 void setTrimPathEnd(float trimPathEnd) { 1425 mTrimPathEnd = trimPathEnd; 1426 } 1427 1428 @SuppressWarnings("unused") 1429 float getTrimPathOffset() { 1430 return mTrimPathOffset; 1431 } 1432 1433 @SuppressWarnings("unused") 1434 void setTrimPathOffset(float trimPathOffset) { 1435 mTrimPathOffset = trimPathOffset; 1436 } 1437 } 1438} 1439