Drawable.java revision bd3bfc5285dcacff0a69fecf3baeeeb90d887a58
1/* 2 * Copyright (C) 2006 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package android.graphics.drawable; 18 19import android.annotation.NonNull; 20import android.annotation.Nullable; 21import android.content.res.ColorStateList; 22import android.content.res.Resources; 23import android.content.res.Resources.Theme; 24import android.content.res.TypedArray; 25import android.graphics.Bitmap; 26import android.graphics.BitmapFactory; 27import android.graphics.Canvas; 28import android.graphics.Color; 29import android.graphics.ColorFilter; 30import android.graphics.Insets; 31import android.graphics.NinePatch; 32import android.graphics.Outline; 33import android.graphics.PixelFormat; 34import android.graphics.PorterDuff; 35import android.graphics.PorterDuff.Mode; 36import android.graphics.PorterDuffColorFilter; 37import android.graphics.Rect; 38import android.graphics.Region; 39import android.graphics.Xfermode; 40import android.os.Trace; 41import android.util.AttributeSet; 42import android.util.DisplayMetrics; 43import android.util.StateSet; 44import android.util.TypedValue; 45import android.util.Xml; 46import android.view.View; 47 48import org.xmlpull.v1.XmlPullParser; 49import org.xmlpull.v1.XmlPullParserException; 50 51import java.io.IOException; 52import java.io.InputStream; 53import java.lang.ref.WeakReference; 54import java.util.Arrays; 55import java.util.Collection; 56 57/** 58 * A Drawable is a general abstraction for "something that can be drawn." Most 59 * often you will deal with Drawable as the type of resource retrieved for 60 * drawing things to the screen; the Drawable class provides a generic API for 61 * dealing with an underlying visual resource that may take a variety of forms. 62 * Unlike a {@link android.view.View}, a Drawable does not have any facility to 63 * receive events or otherwise interact with the user. 64 * 65 * <p>In addition to simple drawing, Drawable provides a number of generic 66 * mechanisms for its client to interact with what is being drawn: 67 * 68 * <ul> 69 * <li> The {@link #setBounds} method <var>must</var> be called to tell the 70 * Drawable where it is drawn and how large it should be. All Drawables 71 * should respect the requested size, often simply by scaling their 72 * imagery. A client can find the preferred size for some Drawables with 73 * the {@link #getIntrinsicHeight} and {@link #getIntrinsicWidth} methods. 74 * 75 * <li> The {@link #getPadding} method can return from some Drawables 76 * information about how to frame content that is placed inside of them. 77 * For example, a Drawable that is intended to be the frame for a button 78 * widget would need to return padding that correctly places the label 79 * inside of itself. 80 * 81 * <li> The {@link #setState} method allows the client to tell the Drawable 82 * in which state it is to be drawn, such as "focused", "selected", etc. 83 * Some drawables may modify their imagery based on the selected state. 84 * 85 * <li> The {@link #setLevel} method allows the client to supply a single 86 * continuous controller that can modify the Drawable is displayed, such as 87 * a battery level or progress level. Some drawables may modify their 88 * imagery based on the current level. 89 * 90 * <li> A Drawable can perform animations by calling back to its client 91 * through the {@link Callback} interface. All clients should support this 92 * interface (via {@link #setCallback}) so that animations will work. A 93 * simple way to do this is through the system facilities such as 94 * {@link android.view.View#setBackground(Drawable)} and 95 * {@link android.widget.ImageView}. 96 * </ul> 97 * 98 * Though usually not visible to the application, Drawables may take a variety 99 * of forms: 100 * 101 * <ul> 102 * <li> <b>Bitmap</b>: the simplest Drawable, a PNG or JPEG image. 103 * <li> <b>Nine Patch</b>: an extension to the PNG format allows it to 104 * specify information about how to stretch it and place things inside of 105 * it. 106 * <li> <b>Shape</b>: contains simple drawing commands instead of a raw 107 * bitmap, allowing it to resize better in some cases. 108 * <li> <b>Layers</b>: a compound drawable, which draws multiple underlying 109 * drawables on top of each other. 110 * <li> <b>States</b>: a compound drawable that selects one of a set of 111 * drawables based on its state. 112 * <li> <b>Levels</b>: a compound drawable that selects one of a set of 113 * drawables based on its level. 114 * <li> <b>Scale</b>: a compound drawable with a single child drawable, 115 * whose overall size is modified based on the current level. 116 * </ul> 117 * 118 * <div class="special reference"> 119 * <h3>Developer Guides</h3> 120 * <p>For more information about how to use drawables, read the 121 * <a href="{@docRoot}guide/topics/graphics/2d-graphics.html">Canvas and Drawables</a> developer 122 * guide. For information and examples of creating drawable resources (XML or bitmap files that 123 * can be loaded in code), read the 124 * <a href="{@docRoot}guide/topics/resources/drawable-resource.html">Drawable Resources</a> 125 * document.</p></div> 126 */ 127public abstract class Drawable { 128 private static final Rect ZERO_BOUNDS_RECT = new Rect(); 129 130 static final PorterDuff.Mode DEFAULT_TINT_MODE = PorterDuff.Mode.SRC_IN; 131 132 private int[] mStateSet = StateSet.WILD_CARD; 133 private int mLevel = 0; 134 private int mChangingConfigurations = 0; 135 private Rect mBounds = ZERO_BOUNDS_RECT; // lazily becomes a new Rect() 136 private WeakReference<Callback> mCallback = null; 137 private boolean mVisible = true; 138 139 private int mLayoutDirection; 140 141 /** 142 * Draw in its bounds (set via setBounds) respecting optional effects such 143 * as alpha (set via setAlpha) and color filter (set via setColorFilter). 144 * 145 * @param canvas The canvas to draw into 146 */ 147 public abstract void draw(Canvas canvas); 148 149 /** 150 * Specify a bounding rectangle for the Drawable. This is where the drawable 151 * will draw when its draw() method is called. 152 */ 153 public void setBounds(int left, int top, int right, int bottom) { 154 Rect oldBounds = mBounds; 155 156 if (oldBounds == ZERO_BOUNDS_RECT) { 157 oldBounds = mBounds = new Rect(); 158 } 159 160 if (oldBounds.left != left || oldBounds.top != top || 161 oldBounds.right != right || oldBounds.bottom != bottom) { 162 if (!oldBounds.isEmpty()) { 163 // first invalidate the previous bounds 164 invalidateSelf(); 165 } 166 mBounds.set(left, top, right, bottom); 167 onBoundsChange(mBounds); 168 } 169 } 170 171 /** 172 * Specify a bounding rectangle for the Drawable. This is where the drawable 173 * will draw when its draw() method is called. 174 */ 175 public void setBounds(Rect bounds) { 176 setBounds(bounds.left, bounds.top, bounds.right, bounds.bottom); 177 } 178 179 /** 180 * Return a copy of the drawable's bounds in the specified Rect (allocated 181 * by the caller). The bounds specify where this will draw when its draw() 182 * method is called. 183 * 184 * @param bounds Rect to receive the drawable's bounds (allocated by the 185 * caller). 186 */ 187 public final void copyBounds(Rect bounds) { 188 bounds.set(mBounds); 189 } 190 191 /** 192 * Return a copy of the drawable's bounds in a new Rect. This returns the 193 * same values as getBounds(), but the returned object is guaranteed to not 194 * be changed later by the drawable (i.e. it retains no reference to this 195 * rect). If the caller already has a Rect allocated, call copyBounds(rect). 196 * 197 * @return A copy of the drawable's bounds 198 */ 199 public final Rect copyBounds() { 200 return new Rect(mBounds); 201 } 202 203 /** 204 * Return the drawable's bounds Rect. Note: for efficiency, the returned 205 * object may be the same object stored in the drawable (though this is not 206 * guaranteed), so if a persistent copy of the bounds is needed, call 207 * copyBounds(rect) instead. 208 * You should also not change the object returned by this method as it may 209 * be the same object stored in the drawable. 210 * 211 * @return The bounds of the drawable (which may change later, so caller 212 * beware). DO NOT ALTER the returned object as it may change the 213 * stored bounds of this drawable. 214 * 215 * @see #copyBounds() 216 * @see #copyBounds(android.graphics.Rect) 217 */ 218 public final Rect getBounds() { 219 if (mBounds == ZERO_BOUNDS_RECT) { 220 mBounds = new Rect(); 221 } 222 223 return mBounds; 224 } 225 226 /** 227 * Return the drawable's dirty bounds Rect. Note: for efficiency, the 228 * returned object may be the same object stored in the drawable (though 229 * this is not guaranteed). 230 * <p> 231 * By default, this returns the full drawable bounds. Custom drawables may 232 * override this method to perform more precise invalidation. 233 * 234 * @return The dirty bounds of this drawable 235 */ 236 public Rect getDirtyBounds() { 237 return getBounds(); 238 } 239 240 /** 241 * Set a mask of the configuration parameters for which this drawable 242 * may change, requiring that it be re-created. 243 * 244 * @param configs A mask of the changing configuration parameters, as 245 * defined by {@link android.content.pm.ActivityInfo}. 246 * 247 * @see android.content.pm.ActivityInfo 248 */ 249 public void setChangingConfigurations(int configs) { 250 mChangingConfigurations = configs; 251 } 252 253 /** 254 * Return a mask of the configuration parameters for which this drawable 255 * may change, requiring that it be re-created. The default implementation 256 * returns whatever was provided through 257 * {@link #setChangingConfigurations(int)} or 0 by default. Subclasses 258 * may extend this to or in the changing configurations of any other 259 * drawables they hold. 260 * 261 * @return Returns a mask of the changing configuration parameters, as 262 * defined by {@link android.content.pm.ActivityInfo}. 263 * 264 * @see android.content.pm.ActivityInfo 265 */ 266 public int getChangingConfigurations() { 267 return mChangingConfigurations; 268 } 269 270 /** 271 * Set to true to have the drawable dither its colors when drawn to a device 272 * with fewer than 8-bits per color component. This can improve the look on 273 * those devices, but can also slow down the drawing a little. 274 */ 275 public void setDither(boolean dither) {} 276 277 /** 278 * @return whether this drawable dither its colors 279 * @see #setDither(boolean) 280 */ 281 public boolean getDither() { 282 return false; 283 } 284 285 /** 286 * Set to true to have the drawable filter its bitmap when scaled or rotated 287 * (for drawables that use bitmaps). If the drawable does not use bitmaps, 288 * this call is ignored. This can improve the look when scaled or rotated, 289 * but also slows down the drawing. 290 */ 291 public void setFilterBitmap(boolean filter) {} 292 293 /** 294 * @return whether this drawable filters its bitmap 295 * @see #setFilterBitmap(boolean) 296 */ 297 public boolean getFilterBitmap() { 298 return false; 299 } 300 301 /** 302 * Implement this interface if you want to create an animated drawable that 303 * extends {@link android.graphics.drawable.Drawable Drawable}. 304 * Upon retrieving a drawable, use 305 * {@link Drawable#setCallback(android.graphics.drawable.Drawable.Callback)} 306 * to supply your implementation of the interface to the drawable; it uses 307 * this interface to schedule and execute animation changes. 308 */ 309 public static interface Callback { 310 /** 311 * Called when the drawable needs to be redrawn. A view at this point 312 * should invalidate itself (or at least the part of itself where the 313 * drawable appears). 314 * 315 * @param who The drawable that is requesting the update. 316 */ 317 public void invalidateDrawable(Drawable who); 318 319 /** 320 * A Drawable can call this to schedule the next frame of its 321 * animation. An implementation can generally simply call 322 * {@link android.os.Handler#postAtTime(Runnable, Object, long)} with 323 * the parameters <var>(what, who, when)</var> to perform the 324 * scheduling. 325 * 326 * @param who The drawable being scheduled. 327 * @param what The action to execute. 328 * @param when The time (in milliseconds) to run. The timebase is 329 * {@link android.os.SystemClock#uptimeMillis} 330 */ 331 public void scheduleDrawable(Drawable who, Runnable what, long when); 332 333 /** 334 * A Drawable can call this to unschedule an action previously 335 * scheduled with {@link #scheduleDrawable}. An implementation can 336 * generally simply call 337 * {@link android.os.Handler#removeCallbacks(Runnable, Object)} with 338 * the parameters <var>(what, who)</var> to unschedule the drawable. 339 * 340 * @param who The drawable being unscheduled. 341 * @param what The action being unscheduled. 342 */ 343 public void unscheduleDrawable(Drawable who, Runnable what); 344 } 345 346 /** 347 * Bind a {@link Callback} object to this Drawable. Required for clients 348 * that want to support animated drawables. 349 * 350 * @param cb The client's Callback implementation. 351 * 352 * @see #getCallback() 353 */ 354 public final void setCallback(Callback cb) { 355 mCallback = new WeakReference<Callback>(cb); 356 } 357 358 /** 359 * Return the current {@link Callback} implementation attached to this 360 * Drawable. 361 * 362 * @return A {@link Callback} instance or null if no callback was set. 363 * 364 * @see #setCallback(android.graphics.drawable.Drawable.Callback) 365 */ 366 public Callback getCallback() { 367 if (mCallback != null) { 368 return mCallback.get(); 369 } 370 return null; 371 } 372 373 /** 374 * Use the current {@link Callback} implementation to have this Drawable 375 * redrawn. Does nothing if there is no Callback attached to the 376 * Drawable. 377 * 378 * @see Callback#invalidateDrawable 379 * @see #getCallback() 380 * @see #setCallback(android.graphics.drawable.Drawable.Callback) 381 */ 382 public void invalidateSelf() { 383 final Callback callback = getCallback(); 384 if (callback != null) { 385 callback.invalidateDrawable(this); 386 } 387 } 388 389 /** 390 * Use the current {@link Callback} implementation to have this Drawable 391 * scheduled. Does nothing if there is no Callback attached to the 392 * Drawable. 393 * 394 * @param what The action being scheduled. 395 * @param when The time (in milliseconds) to run. 396 * 397 * @see Callback#scheduleDrawable 398 */ 399 public void scheduleSelf(Runnable what, long when) { 400 final Callback callback = getCallback(); 401 if (callback != null) { 402 callback.scheduleDrawable(this, what, when); 403 } 404 } 405 406 /** 407 * Use the current {@link Callback} implementation to have this Drawable 408 * unscheduled. Does nothing if there is no Callback attached to the 409 * Drawable. 410 * 411 * @param what The runnable that you no longer want called. 412 * 413 * @see Callback#unscheduleDrawable 414 */ 415 public void unscheduleSelf(Runnable what) { 416 final Callback callback = getCallback(); 417 if (callback != null) { 418 callback.unscheduleDrawable(this, what); 419 } 420 } 421 422 /** 423 * Returns the resolved layout direction for this Drawable. 424 * 425 * @return One of {@link android.view.View#LAYOUT_DIRECTION_LTR}, 426 * {@link android.view.View#LAYOUT_DIRECTION_RTL} 427 * @see #setLayoutDirection(int) 428 */ 429 public int getLayoutDirection() { 430 return mLayoutDirection; 431 } 432 433 /** 434 * Set the layout direction for this drawable. Should be a resolved 435 * layout direction, as the Drawable as no capacity to do the resolution on 436 * its own. 437 * 438 * @param layoutDirection the resolved layout direction for the drawable, 439 * either {@link android.view.View#LAYOUT_DIRECTION_LTR} 440 * or {@link android.view.View#LAYOUT_DIRECTION_RTL} 441 * @see #getLayoutDirection() 442 */ 443 public final boolean setLayoutDirection(@View.ResolvedLayoutDir int layoutDirection) { 444 if (mLayoutDirection != layoutDirection) { 445 mLayoutDirection = layoutDirection; 446 return onLayoutDirectionChange(layoutDirection); 447 } 448 return false; 449 } 450 451 /** 452 * Called when the drawable's resolved layout direction changes. 453 * 454 * @param layoutDirection the new resolved layout direction 455 * @return true if the layout direction change has caused the appearance of 456 * the drawable to change and it needs to be re-drawn 457 * @see #setLayoutDirection(int) 458 */ 459 public boolean onLayoutDirectionChange(@View.ResolvedLayoutDir int layoutDirection) { 460 return false; 461 } 462 463 /** 464 * Specify an alpha value for the drawable. 0 means fully transparent, and 465 * 255 means fully opaque. 466 */ 467 public abstract void setAlpha(int alpha); 468 469 /** 470 * Gets the current alpha value for the drawable. 0 means fully transparent, 471 * 255 means fully opaque. This method is implemented by 472 * Drawable subclasses and the value returned is specific to how that class treats alpha. 473 * The default return value is 255 if the class does not override this method to return a value 474 * specific to its use of alpha. 475 */ 476 public int getAlpha() { 477 return 0xFF; 478 } 479 480 /** 481 * @hide 482 * 483 * Internal-only method for setting xfermode on certain supported drawables. 484 * 485 * Should not be made public since the layers and drawing area with which 486 * Drawables draw is private implementation detail, and not something apps 487 * should rely upon. 488 */ 489 public void setXfermode(Xfermode mode) { 490 // Base implementation drops it on the floor for compatibility. Whee! 491 } 492 493 /** 494 * Specify an optional color filter for the drawable. 495 * <p> 496 * If a Drawable has a ColorFilter, each output pixel of the Drawable's 497 * drawing contents will be modified by the color filter before it is 498 * blended onto the render target of a Canvas. 499 * </p> 500 * <p> 501 * Pass {@code null} to remove any existing color filter. 502 * </p> 503 * <p class="note"><strong>Note:</strong> Setting a non-{@code null} color 504 * filter disables {@link #setTintList(ColorStateList) tint}. 505 * </p> 506 * 507 * @param colorFilter The color filter to apply, or {@code null} to remove the 508 * existing color filter 509 */ 510 public abstract void setColorFilter(@Nullable ColorFilter colorFilter); 511 512 /** 513 * Specify a color and Porter-Duff mode to be the color filter for this 514 * drawable. 515 * <p> 516 * Convenience for {@link #setColorFilter(ColorFilter)} which constructs a 517 * {@link PorterDuffColorFilter}. 518 * </p> 519 * <p class="note"><strong>Note:</strong> Setting a color filter disables 520 * {@link #setTintList(ColorStateList) tint}. 521 * </p> 522 */ 523 public void setColorFilter(int color, @NonNull PorterDuff.Mode mode) { 524 setColorFilter(new PorterDuffColorFilter(color, mode)); 525 } 526 527 /** 528 * Specifies tint color for this drawable. 529 * <p> 530 * A Drawable's drawing content will be blended together with its tint 531 * before it is drawn to the screen. This functions similarly to 532 * {@link #setColorFilter(int, PorterDuff.Mode)}. 533 * </p> 534 * <p> 535 * To clear the tint, pass {@code null} to 536 * {@link #setTintList(ColorStateList)}. 537 * </p> 538 * <p class="note"><strong>Note:</strong> Setting a color filter via 539 * {@link #setColorFilter(ColorFilter)} or 540 * {@link #setColorFilter(int, PorterDuff.Mode)} overrides tint. 541 * </p> 542 * 543 * @param tintColor Color to use for tinting this drawable 544 * @see #setTintList(ColorStateList) 545 * @see #setTintMode(PorterDuff.Mode) 546 */ 547 public void setTint(int tintColor) { 548 setTintList(ColorStateList.valueOf(tintColor)); 549 } 550 551 /** 552 * Specifies tint color for this drawable as a color state list. 553 * <p> 554 * A Drawable's drawing content will be blended together with its tint 555 * before it is drawn to the screen. This functions similarly to 556 * {@link #setColorFilter(int, PorterDuff.Mode)}. 557 * </p> 558 * <p class="note"><strong>Note:</strong> Setting a color filter via 559 * {@link #setColorFilter(ColorFilter)} or 560 * {@link #setColorFilter(int, PorterDuff.Mode)} overrides tint. 561 * </p> 562 * 563 * @param tint Color state list to use for tinting this drawable, or 564 * {@code null} to clear the tint 565 * @see #setTint(int) 566 * @see #setTintMode(PorterDuff.Mode) 567 */ 568 public void setTintList(@Nullable ColorStateList tint) {} 569 570 /** 571 * Specifies a tint blending mode for this drawable. 572 * <p> 573 * Defines how this drawable's tint color should be blended into the drawable 574 * before it is drawn to screen. Default tint mode is {@link PorterDuff.Mode#MULTIPLY}. 575 * </p> 576 * <p class="note"><strong>Note:</strong> Setting a color filter via 577 * {@link #setColorFilter(ColorFilter)} or 578 * {@link #setColorFilter(int, PorterDuff.Mode)} overrides tint. 579 * </p> 580 * 581 * @param tintMode A Porter-Duff blending mode 582 * @see #setTint(int) 583 * @see #setTintList(ColorStateList) 584 */ 585 public void setTintMode(@NonNull PorterDuff.Mode tintMode) {} 586 587 /** 588 * Returns the current color filter, or {@code null} if none set. 589 * 590 * @return the current color filter, or {@code null} if none set 591 */ 592 public ColorFilter getColorFilter() { 593 return null; 594 } 595 596 /** 597 * Removes the color filter for this drawable. 598 */ 599 public void clearColorFilter() { 600 setColorFilter(null); 601 } 602 603 /** 604 * Specifies the hotspot's location within the drawable. 605 * 606 * @param x The X coordinate of the center of the hotspot 607 * @param y The Y coordinate of the center of the hotspot 608 */ 609 public void setHotspot(float x, float y) {} 610 611 /** 612 * Sets the bounds to which the hotspot is constrained, if they should be 613 * different from the drawable bounds. 614 * 615 * @param left position in pixels of the left bound 616 * @param top position in pixels of the top bound 617 * @param right position in pixels of the right bound 618 * @param bottom position in pixels of the bottom bound 619 * @see #getHotspotBounds(android.graphics.Rect) 620 */ 621 public void setHotspotBounds(int left, int top, int right, int bottom) {} 622 623 /** 624 * Populates {@code outRect} with the hotspot bounds. 625 * 626 * @param outRect the rect to populate with the hotspot bounds 627 * @see #setHotspotBounds(int, int, int, int) 628 */ 629 public void getHotspotBounds(Rect outRect) { 630 outRect.set(getBounds()); 631 } 632 633 /** 634 * Whether this drawable requests projection. 635 * 636 * @hide magic! 637 */ 638 public boolean isProjected() { 639 return false; 640 } 641 642 /** 643 * Indicates whether this drawable will change its appearance based on 644 * state. Clients can use this to determine whether it is necessary to 645 * calculate their state and call setState. 646 * 647 * @return True if this drawable changes its appearance based on state, 648 * false otherwise. 649 * @see #setState(int[]) 650 */ 651 public boolean isStateful() { 652 return false; 653 } 654 655 /** 656 * Specify a set of states for the drawable. These are use-case specific, 657 * so see the relevant documentation. As an example, the background for 658 * widgets like Button understand the following states: 659 * [{@link android.R.attr#state_focused}, 660 * {@link android.R.attr#state_pressed}]. 661 * 662 * <p>If the new state you are supplying causes the appearance of the 663 * Drawable to change, then it is responsible for calling 664 * {@link #invalidateSelf} in order to have itself redrawn, <em>and</em> 665 * true will be returned from this function. 666 * 667 * <p>Note: The Drawable holds a reference on to <var>stateSet</var> 668 * until a new state array is given to it, so you must not modify this 669 * array during that time.</p> 670 * 671 * @param stateSet The new set of states to be displayed. 672 * 673 * @return Returns true if this change in state has caused the appearance 674 * of the Drawable to change (hence requiring an invalidate), otherwise 675 * returns false. 676 */ 677 public boolean setState(final int[] stateSet) { 678 if (!Arrays.equals(mStateSet, stateSet)) { 679 mStateSet = stateSet; 680 return onStateChange(stateSet); 681 } 682 return false; 683 } 684 685 /** 686 * Describes the current state, as a union of primitve states, such as 687 * {@link android.R.attr#state_focused}, 688 * {@link android.R.attr#state_selected}, etc. 689 * Some drawables may modify their imagery based on the selected state. 690 * @return An array of resource Ids describing the current state. 691 */ 692 public int[] getState() { 693 return mStateSet; 694 } 695 696 /** 697 * If this Drawable does transition animations between states, ask that 698 * it immediately jump to the current state and skip any active animations. 699 */ 700 public void jumpToCurrentState() { 701 } 702 703 /** 704 * @return The current drawable that will be used by this drawable. For simple drawables, this 705 * is just the drawable itself. For drawables that change state like 706 * {@link StateListDrawable} and {@link LevelListDrawable} this will be the child drawable 707 * currently in use. 708 */ 709 public Drawable getCurrent() { 710 return this; 711 } 712 713 /** 714 * Specify the level for the drawable. This allows a drawable to vary its 715 * imagery based on a continuous controller, for example to show progress 716 * or volume level. 717 * 718 * <p>If the new level you are supplying causes the appearance of the 719 * Drawable to change, then it is responsible for calling 720 * {@link #invalidateSelf} in order to have itself redrawn, <em>and</em> 721 * true will be returned from this function. 722 * 723 * @param level The new level, from 0 (minimum) to 10000 (maximum). 724 * 725 * @return Returns true if this change in level has caused the appearance 726 * of the Drawable to change (hence requiring an invalidate), otherwise 727 * returns false. 728 */ 729 public final boolean setLevel(int level) { 730 if (mLevel != level) { 731 mLevel = level; 732 return onLevelChange(level); 733 } 734 return false; 735 } 736 737 /** 738 * Retrieve the current level. 739 * 740 * @return int Current level, from 0 (minimum) to 10000 (maximum). 741 */ 742 public final int getLevel() { 743 return mLevel; 744 } 745 746 /** 747 * Set whether this Drawable is visible. This generally does not impact 748 * the Drawable's behavior, but is a hint that can be used by some 749 * Drawables, for example, to decide whether run animations. 750 * 751 * @param visible Set to true if visible, false if not. 752 * @param restart You can supply true here to force the drawable to behave 753 * as if it has just become visible, even if it had last 754 * been set visible. Used for example to force animations 755 * to restart. 756 * 757 * @return boolean Returns true if the new visibility is different than 758 * its previous state. 759 */ 760 public boolean setVisible(boolean visible, boolean restart) { 761 boolean changed = mVisible != visible; 762 if (changed) { 763 mVisible = visible; 764 invalidateSelf(); 765 } 766 return changed; 767 } 768 769 public final boolean isVisible() { 770 return mVisible; 771 } 772 773 /** 774 * Set whether this Drawable is automatically mirrored when its layout direction is RTL 775 * (right-to left). See {@link android.util.LayoutDirection}. 776 * 777 * @param mirrored Set to true if the Drawable should be mirrored, false if not. 778 */ 779 public void setAutoMirrored(boolean mirrored) { 780 } 781 782 /** 783 * Tells if this Drawable will be automatically mirrored when its layout direction is RTL 784 * right-to-left. See {@link android.util.LayoutDirection}. 785 * 786 * @return boolean Returns true if this Drawable will be automatically mirrored. 787 */ 788 public boolean isAutoMirrored() { 789 return false; 790 } 791 792 /** 793 * Applies the specified theme to this Drawable and its children. 794 */ 795 public void applyTheme(@SuppressWarnings("unused") Theme t) { 796 } 797 798 public boolean canApplyTheme() { 799 return false; 800 } 801 802 /** 803 * Return the opacity/transparency of this Drawable. The returned value is 804 * one of the abstract format constants in 805 * {@link android.graphics.PixelFormat}: 806 * {@link android.graphics.PixelFormat#UNKNOWN}, 807 * {@link android.graphics.PixelFormat#TRANSLUCENT}, 808 * {@link android.graphics.PixelFormat#TRANSPARENT}, or 809 * {@link android.graphics.PixelFormat#OPAQUE}. 810 * 811 * <p>Generally a Drawable should be as conservative as possible with the 812 * value it returns. For example, if it contains multiple child drawables 813 * and only shows one of them at a time, if only one of the children is 814 * TRANSLUCENT and the others are OPAQUE then TRANSLUCENT should be 815 * returned. You can use the method {@link #resolveOpacity} to perform a 816 * standard reduction of two opacities to the appropriate single output. 817 * 818 * <p>Note that the returned value does <em>not</em> take into account a 819 * custom alpha or color filter that has been applied by the client through 820 * the {@link #setAlpha} or {@link #setColorFilter} methods. 821 * 822 * @return int The opacity class of the Drawable. 823 * 824 * @see android.graphics.PixelFormat 825 */ 826 public abstract int getOpacity(); 827 828 /** 829 * Return the appropriate opacity value for two source opacities. If 830 * either is UNKNOWN, that is returned; else, if either is TRANSLUCENT, 831 * that is returned; else, if either is TRANSPARENT, that is returned; 832 * else, OPAQUE is returned. 833 * 834 * <p>This is to help in implementing {@link #getOpacity}. 835 * 836 * @param op1 One opacity value. 837 * @param op2 Another opacity value. 838 * 839 * @return int The combined opacity value. 840 * 841 * @see #getOpacity 842 */ 843 public static int resolveOpacity(int op1, int op2) { 844 if (op1 == op2) { 845 return op1; 846 } 847 if (op1 == PixelFormat.UNKNOWN || op2 == PixelFormat.UNKNOWN) { 848 return PixelFormat.UNKNOWN; 849 } 850 if (op1 == PixelFormat.TRANSLUCENT || op2 == PixelFormat.TRANSLUCENT) { 851 return PixelFormat.TRANSLUCENT; 852 } 853 if (op1 == PixelFormat.TRANSPARENT || op2 == PixelFormat.TRANSPARENT) { 854 return PixelFormat.TRANSPARENT; 855 } 856 return PixelFormat.OPAQUE; 857 } 858 859 /** 860 * Returns a Region representing the part of the Drawable that is completely 861 * transparent. This can be used to perform drawing operations, identifying 862 * which parts of the target will not change when rendering the Drawable. 863 * The default implementation returns null, indicating no transparent 864 * region; subclasses can optionally override this to return an actual 865 * Region if they want to supply this optimization information, but it is 866 * not required that they do so. 867 * 868 * @return Returns null if the Drawables has no transparent region to 869 * report, else a Region holding the parts of the Drawable's bounds that 870 * are transparent. 871 */ 872 public Region getTransparentRegion() { 873 return null; 874 } 875 876 /** 877 * Override this in your subclass to change appearance if you recognize the 878 * specified state. 879 * 880 * @return Returns true if the state change has caused the appearance of 881 * the Drawable to change (that is, it needs to be drawn), else false 882 * if it looks the same and there is no need to redraw it since its 883 * last state. 884 */ 885 protected boolean onStateChange(int[] state) { return false; } 886 /** Override this in your subclass to change appearance if you vary based 887 * on level. 888 * @return Returns true if the level change has caused the appearance of 889 * the Drawable to change (that is, it needs to be drawn), else false 890 * if it looks the same and there is no need to redraw it since its 891 * last level. 892 */ 893 protected boolean onLevelChange(int level) { return false; } 894 /** 895 * Override this in your subclass to change appearance if you vary based on 896 * the bounds. 897 */ 898 protected void onBoundsChange(Rect bounds) {} 899 900 /** 901 * Return the intrinsic width of the underlying drawable object. Returns 902 * -1 if it has no intrinsic width, such as with a solid color. 903 */ 904 public int getIntrinsicWidth() { 905 return -1; 906 } 907 908 /** 909 * Return the intrinsic height of the underlying drawable object. Returns 910 * -1 if it has no intrinsic height, such as with a solid color. 911 */ 912 public int getIntrinsicHeight() { 913 return -1; 914 } 915 916 /** 917 * Returns the minimum width suggested by this Drawable. If a View uses this 918 * Drawable as a background, it is suggested that the View use at least this 919 * value for its width. (There will be some scenarios where this will not be 920 * possible.) This value should INCLUDE any padding. 921 * 922 * @return The minimum width suggested by this Drawable. If this Drawable 923 * doesn't have a suggested minimum width, 0 is returned. 924 */ 925 public int getMinimumWidth() { 926 final int intrinsicWidth = getIntrinsicWidth(); 927 return intrinsicWidth > 0 ? intrinsicWidth : 0; 928 } 929 930 /** 931 * Returns the minimum height suggested by this Drawable. If a View uses this 932 * Drawable as a background, it is suggested that the View use at least this 933 * value for its height. (There will be some scenarios where this will not be 934 * possible.) This value should INCLUDE any padding. 935 * 936 * @return The minimum height suggested by this Drawable. If this Drawable 937 * doesn't have a suggested minimum height, 0 is returned. 938 */ 939 public int getMinimumHeight() { 940 final int intrinsicHeight = getIntrinsicHeight(); 941 return intrinsicHeight > 0 ? intrinsicHeight : 0; 942 } 943 944 /** 945 * Return in padding the insets suggested by this Drawable for placing 946 * content inside the drawable's bounds. Positive values move toward the 947 * center of the Drawable (set Rect.inset). 948 * 949 * @return true if this drawable actually has a padding, else false. When false is returned, 950 * the padding is always set to 0. 951 */ 952 public boolean getPadding(@NonNull Rect padding) { 953 padding.set(0, 0, 0, 0); 954 return false; 955 } 956 957 /** 958 * Return in insets the layout insets suggested by this Drawable for use with alignment 959 * operations during layout. 960 * 961 * @hide 962 */ 963 public Insets getOpticalInsets() { 964 return Insets.NONE; 965 } 966 967 /** 968 * Called to get the drawable to populate the Outline that defines its drawing area. 969 * <p> 970 * This method is called by the default {@link android.view.ViewOutlineProvider} to define 971 * the outline of the View. 972 * <p> 973 * The default behavior defines the outline to be the bounding rectangle of 0 alpha. 974 * Subclasses that wish to convey a different shape or alpha value must override this method. 975 * 976 * @see android.view.View#setOutlineProvider(android.view.ViewOutlineProvider) 977 */ 978 public void getOutline(@NonNull Outline outline) { 979 outline.setRect(getBounds()); 980 outline.setAlpha(0); 981 } 982 983 /** 984 * Make this drawable mutable. This operation cannot be reversed. A mutable 985 * drawable is guaranteed to not share its state with any other drawable. 986 * This is especially useful when you need to modify properties of drawables 987 * loaded from resources. By default, all drawables instances loaded from 988 * the same resource share a common state; if you modify the state of one 989 * instance, all the other instances will receive the same modification. 990 * 991 * Calling this method on a mutable Drawable will have no effect. 992 * 993 * @return This drawable. 994 * @see ConstantState 995 * @see #getConstantState() 996 */ 997 public Drawable mutate() { 998 return this; 999 } 1000 1001 /** 1002 * Clears the mutated state, allowing this drawable to be cached and 1003 * mutated again. 1004 * <p> 1005 * This is hidden because only framework drawables can be cached, so 1006 * custom drawables don't need to support constant state, mutate(), or 1007 * clearMutated(). 1008 * 1009 * @hide 1010 */ 1011 public void clearMutated() { 1012 // Default implementation is no-op. 1013 } 1014 1015 /** 1016 * Create a drawable from an inputstream 1017 */ 1018 public static Drawable createFromStream(InputStream is, String srcName) { 1019 Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, srcName != null ? srcName : "Unknown drawable"); 1020 try { 1021 return createFromResourceStream(null, null, is, srcName); 1022 } finally { 1023 Trace.traceEnd(Trace.TRACE_TAG_RESOURCES); 1024 } 1025 } 1026 1027 /** 1028 * Create a drawable from an inputstream, using the given resources and 1029 * value to determine density information. 1030 */ 1031 public static Drawable createFromResourceStream(Resources res, TypedValue value, 1032 InputStream is, String srcName) { 1033 Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, srcName != null ? srcName : "Unknown drawable"); 1034 try { 1035 return createFromResourceStream(res, value, is, srcName, null); 1036 } finally { 1037 Trace.traceEnd(Trace.TRACE_TAG_RESOURCES); 1038 } 1039 } 1040 1041 /** 1042 * Create a drawable from an inputstream, using the given resources and 1043 * value to determine density information. 1044 */ 1045 public static Drawable createFromResourceStream(Resources res, TypedValue value, 1046 InputStream is, String srcName, BitmapFactory.Options opts) { 1047 if (is == null) { 1048 return null; 1049 } 1050 1051 /* ugh. The decodeStream contract is that we have already allocated 1052 the pad rect, but if the bitmap does not had a ninepatch chunk, 1053 then the pad will be ignored. If we could change this to lazily 1054 alloc/assign the rect, we could avoid the GC churn of making new 1055 Rects only to drop them on the floor. 1056 */ 1057 Rect pad = new Rect(); 1058 1059 // Special stuff for compatibility mode: if the target density is not 1060 // the same as the display density, but the resource -is- the same as 1061 // the display density, then don't scale it down to the target density. 1062 // This allows us to load the system's density-correct resources into 1063 // an application in compatibility mode, without scaling those down 1064 // to the compatibility density only to have them scaled back up when 1065 // drawn to the screen. 1066 if (opts == null) opts = new BitmapFactory.Options(); 1067 opts.inScreenDensity = res != null 1068 ? res.getDisplayMetrics().noncompatDensityDpi : DisplayMetrics.DENSITY_DEVICE; 1069 Bitmap bm = BitmapFactory.decodeResourceStream(res, value, is, pad, opts); 1070 if (bm != null) { 1071 byte[] np = bm.getNinePatchChunk(); 1072 if (np == null || !NinePatch.isNinePatchChunk(np)) { 1073 np = null; 1074 pad = null; 1075 } 1076 1077 final Rect opticalInsets = new Rect(); 1078 bm.getOpticalInsets(opticalInsets); 1079 return drawableFromBitmap(res, bm, np, pad, opticalInsets, srcName); 1080 } 1081 return null; 1082 } 1083 1084 /** 1085 * Create a drawable from an XML document. For more information on how to 1086 * create resources in XML, see 1087 * <a href="{@docRoot}guide/topics/resources/drawable-resource.html">Drawable Resources</a>. 1088 */ 1089 public static Drawable createFromXml(Resources r, XmlPullParser parser) 1090 throws XmlPullParserException, IOException { 1091 return createFromXml(r, parser, null); 1092 } 1093 1094 /** 1095 * Create a drawable from an XML document using an optional {@link Theme}. 1096 * For more information on how to create resources in XML, see 1097 * <a href="{@docRoot}guide/topics/resources/drawable-resource.html">Drawable Resources</a>. 1098 */ 1099 public static Drawable createFromXml(Resources r, XmlPullParser parser, Theme theme) 1100 throws XmlPullParserException, IOException { 1101 AttributeSet attrs = Xml.asAttributeSet(parser); 1102 1103 int type; 1104 while ((type=parser.next()) != XmlPullParser.START_TAG && 1105 type != XmlPullParser.END_DOCUMENT) { 1106 // Empty loop 1107 } 1108 1109 if (type != XmlPullParser.START_TAG) { 1110 throw new XmlPullParserException("No start tag found"); 1111 } 1112 1113 Drawable drawable = createFromXmlInner(r, parser, attrs, theme); 1114 1115 if (drawable == null) { 1116 throw new RuntimeException("Unknown initial tag: " + parser.getName()); 1117 } 1118 1119 return drawable; 1120 } 1121 1122 /** 1123 * Create from inside an XML document. Called on a parser positioned at 1124 * a tag in an XML document, tries to create a Drawable from that tag. 1125 * Returns null if the tag is not a valid drawable. 1126 */ 1127 public static Drawable createFromXmlInner(Resources r, XmlPullParser parser, AttributeSet attrs) 1128 throws XmlPullParserException, IOException { 1129 return createFromXmlInner(r, parser, attrs, null); 1130 } 1131 1132 /** 1133 * Create a drawable from inside an XML document using an optional 1134 * {@link Theme}. Called on a parser positioned at a tag in an XML 1135 * document, tries to create a Drawable from that tag. Returns {@code null} 1136 * if the tag is not a valid drawable. 1137 */ 1138 public static Drawable createFromXmlInner(Resources r, XmlPullParser parser, AttributeSet attrs, 1139 Theme theme) throws XmlPullParserException, IOException { 1140 final Drawable drawable; 1141 1142 final String name = parser.getName(); 1143 switch (name) { 1144 case "selector": 1145 drawable = new StateListDrawable(); 1146 break; 1147 case "animated-selector": 1148 drawable = new AnimatedStateListDrawable(); 1149 break; 1150 case "level-list": 1151 drawable = new LevelListDrawable(); 1152 break; 1153 case "layer-list": 1154 drawable = new LayerDrawable(); 1155 break; 1156 case "transition": 1157 drawable = new TransitionDrawable(); 1158 break; 1159 case "ripple": 1160 drawable = new RippleDrawable(); 1161 break; 1162 case "color": 1163 drawable = new ColorDrawable(); 1164 break; 1165 case "shape": 1166 drawable = new GradientDrawable(); 1167 break; 1168 case "vector": 1169 drawable = new VectorDrawable(); 1170 break; 1171 case "animated-vector": 1172 drawable = new AnimatedVectorDrawable(); 1173 break; 1174 case "scale": 1175 drawable = new ScaleDrawable(); 1176 break; 1177 case "clip": 1178 drawable = new ClipDrawable(); 1179 break; 1180 case "rotate": 1181 drawable = new RotateDrawable(); 1182 break; 1183 case "animated-rotate": 1184 drawable = new AnimatedRotateDrawable(); 1185 break; 1186 case "animation-list": 1187 drawable = new AnimationDrawable(); 1188 break; 1189 case "inset": 1190 drawable = new InsetDrawable(); 1191 break; 1192 case "bitmap": 1193 drawable = new BitmapDrawable(r); 1194 if (r != null) { 1195 ((BitmapDrawable) drawable).setTargetDensity(r.getDisplayMetrics()); 1196 } 1197 break; 1198 case "nine-patch": 1199 drawable = new NinePatchDrawable(); 1200 if (r != null) { 1201 ((NinePatchDrawable) drawable).setTargetDensity(r.getDisplayMetrics()); 1202 } 1203 break; 1204 default: 1205 throw new XmlPullParserException(parser.getPositionDescription() + 1206 ": invalid drawable tag " + name); 1207 1208 } 1209 drawable.inflate(r, parser, attrs, theme); 1210 return drawable; 1211 } 1212 1213 1214 /** 1215 * Create a drawable from file path name. 1216 */ 1217 public static Drawable createFromPath(String pathName) { 1218 if (pathName == null) { 1219 return null; 1220 } 1221 1222 Trace.traceBegin(Trace.TRACE_TAG_RESOURCES, pathName); 1223 try { 1224 Bitmap bm = BitmapFactory.decodeFile(pathName); 1225 if (bm != null) { 1226 return drawableFromBitmap(null, bm, null, null, null, pathName); 1227 } 1228 } finally { 1229 Trace.traceEnd(Trace.TRACE_TAG_RESOURCES); 1230 } 1231 1232 return null; 1233 } 1234 1235 /** 1236 * Inflate this Drawable from an XML resource. Does not apply a theme. 1237 * 1238 * @see #inflate(Resources, XmlPullParser, AttributeSet, Theme) 1239 */ 1240 public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs) 1241 throws XmlPullParserException, IOException { 1242 inflate(r, parser, attrs, null); 1243 } 1244 1245 /** 1246 * Inflate this Drawable from an XML resource optionally styled by a theme. 1247 * 1248 * @param r Resources used to resolve attribute values 1249 * @param parser XML parser from which to inflate this Drawable 1250 * @param attrs Base set of attribute values 1251 * @param theme Theme to apply, may be null 1252 * @throws XmlPullParserException 1253 * @throws IOException 1254 */ 1255 public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Theme theme) 1256 throws XmlPullParserException, IOException { 1257 final TypedArray a; 1258 if (theme != null) { 1259 a = theme.obtainStyledAttributes( 1260 attrs, com.android.internal.R.styleable.Drawable, 0, 0); 1261 } else { 1262 a = r.obtainAttributes(attrs, com.android.internal.R.styleable.Drawable); 1263 } 1264 1265 inflateWithAttributes(r, parser, a, com.android.internal.R.styleable.Drawable_visible); 1266 a.recycle(); 1267 } 1268 1269 /** 1270 * Inflate a Drawable from an XML resource. 1271 * 1272 * @throws XmlPullParserException 1273 * @throws IOException 1274 */ 1275 void inflateWithAttributes(Resources r, XmlPullParser parser, TypedArray attrs, int visibleAttr) 1276 throws XmlPullParserException, IOException { 1277 mVisible = attrs.getBoolean(visibleAttr, mVisible); 1278 } 1279 1280 /** 1281 * This abstract class is used by {@link Drawable}s to store shared constant state and data 1282 * between Drawables. {@link BitmapDrawable}s created from the same resource will for instance 1283 * share a unique bitmap stored in their ConstantState. 1284 * 1285 * <p> 1286 * {@link #newDrawable(Resources)} can be used as a factory to create new Drawable instances 1287 * from this ConstantState. 1288 * </p> 1289 * 1290 * Use {@link Drawable#getConstantState()} to retrieve the ConstantState of a Drawable. Calling 1291 * {@link Drawable#mutate()} on a Drawable should typically create a new ConstantState for that 1292 * Drawable. 1293 */ 1294 public static abstract class ConstantState { 1295 /** 1296 * Create a new drawable without supplying resources the caller 1297 * is running in. Note that using this means the density-dependent 1298 * drawables (like bitmaps) will not be able to update their target 1299 * density correctly. One should use {@link #newDrawable(Resources)} 1300 * instead to provide a resource. 1301 */ 1302 public abstract Drawable newDrawable(); 1303 1304 /** 1305 * Create a new Drawable instance from its constant state. This 1306 * must be implemented for drawables that change based on the target 1307 * density of their caller (that is depending on whether it is 1308 * in compatibility mode). 1309 */ 1310 public Drawable newDrawable(Resources res) { 1311 return newDrawable(); 1312 } 1313 1314 /** 1315 * Create a new Drawable instance from its constant state. This must be 1316 * implemented for drawables that can have a theme applied. 1317 */ 1318 public Drawable newDrawable(Resources res, Theme theme) { 1319 return newDrawable(null); 1320 } 1321 1322 /** 1323 * Return a bit mask of configuration changes that will impact 1324 * this drawable (and thus require completely reloading it). 1325 */ 1326 public abstract int getChangingConfigurations(); 1327 1328 /** 1329 * @return Total pixel count 1330 * @hide 1331 */ 1332 public int addAtlasableBitmaps(Collection<Bitmap> atlasList) { 1333 return 0; 1334 } 1335 1336 /** @hide */ 1337 protected final boolean isAtlasable(Bitmap bitmap) { 1338 return bitmap != null && bitmap.getConfig() == Bitmap.Config.ARGB_8888; 1339 } 1340 1341 /** 1342 * Return whether this constant state can have a theme applied. 1343 */ 1344 public boolean canApplyTheme() { 1345 return false; 1346 } 1347 } 1348 1349 /** 1350 * Return a {@link ConstantState} instance that holds the shared state of this Drawable. 1351 * 1352 * @return The ConstantState associated to that Drawable. 1353 * @see ConstantState 1354 * @see Drawable#mutate() 1355 */ 1356 public ConstantState getConstantState() { 1357 return null; 1358 } 1359 1360 private static Drawable drawableFromBitmap(Resources res, Bitmap bm, byte[] np, 1361 Rect pad, Rect layoutBounds, String srcName) { 1362 1363 if (np != null) { 1364 return new NinePatchDrawable(res, bm, np, pad, layoutBounds, srcName); 1365 } 1366 1367 return new BitmapDrawable(res, bm); 1368 } 1369 1370 /** 1371 * Ensures the tint filter is consistent with the current tint color and 1372 * mode. 1373 */ 1374 PorterDuffColorFilter updateTintFilter(PorterDuffColorFilter tintFilter, ColorStateList tint, 1375 PorterDuff.Mode tintMode) { 1376 if (tint == null || tintMode == null) { 1377 return null; 1378 } 1379 1380 final int color = tint.getColorForState(getState(), Color.TRANSPARENT); 1381 if (tintFilter == null) { 1382 return new PorterDuffColorFilter(color, tintMode); 1383 } 1384 1385 tintFilter.setColor(color); 1386 tintFilter.setMode(tintMode); 1387 return tintFilter; 1388 } 1389 1390 /** 1391 * Obtains styled attributes from the theme, if available, or unstyled 1392 * resources if the theme is null. 1393 */ 1394 static TypedArray obtainAttributes( 1395 Resources res, Theme theme, AttributeSet set, int[] attrs) { 1396 if (theme == null) { 1397 return res.obtainAttributes(set, attrs); 1398 } 1399 return theme.obtainStyledAttributes(set, attrs, 0, 0); 1400 } 1401 1402 /** 1403 * Parses a {@link android.graphics.PorterDuff.Mode} from a tintMode 1404 * attribute's enum value. 1405 * 1406 * @hide 1407 */ 1408 public static PorterDuff.Mode parseTintMode(int value, Mode defaultMode) { 1409 switch (value) { 1410 case 3: return Mode.SRC_OVER; 1411 case 5: return Mode.SRC_IN; 1412 case 9: return Mode.SRC_ATOP; 1413 case 14: return Mode.MULTIPLY; 1414 case 15: return Mode.SCREEN; 1415 case 16: return Mode.ADD; 1416 default: return defaultMode; 1417 } 1418 } 1419} 1420 1421