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