Drawable.java revision 54b6cfa9a9e5b861a9930af873580d6dc20f773c
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 java.io.InputStream; 20import java.io.IOException; 21import java.util.Arrays; 22 23import org.xmlpull.v1.XmlPullParser; 24import org.xmlpull.v1.XmlPullParserException; 25 26import android.content.res.Resources; 27import android.content.res.TypedArray; 28import android.graphics.*; 29import android.util.AttributeSet; 30import android.util.StateSet; 31import android.util.Xml; 32 33/** 34 * A Drawable is a general abstraction for "something that can be drawn." Most 35 * often you will deal with Drawable as the type of resource retrieved for 36 * drawing things to the screen; the Drawable class provides a generic API for 37 * dealing with an underlying visual resource that may take a variety of forms. 38 * Unlike a {@link android.view.View}, a Drawable does not have any facility to 39 * receive events or otherwise interact with the user. 40 * 41 * <p>In addition to simple drawing, Drawable provides a number of generic 42 * mechanisms for its client to interact with what is being drawn: 43 * 44 * <ul> 45 * <li> The {@link #setBounds} method <var>must</var> be called to tell the 46 * Drawable where it is drawn and how large it should be. All Drawables 47 * should respect the requested size, often simply by scaling their 48 * imagery. A client can find the preferred size for some Drawables with 49 * the {@link #getIntrinsicHeight} and {@link #getIntrinsicWidth} methods. 50 * 51 * <li> The {@link #getPadding} method can return from some Drawables 52 * information about how to frame content that is placed inside of them. 53 * For example, a Drawable that is intended to be the frame for a button 54 * widget would need to return padding that correctly places the label 55 * inside of itself. 56 * 57 * <li> The {@link #setState} method allows the client to tell the Drawable 58 * in which state it is to be drawn, such as "focused", "selected", etc. 59 * Some drawables may modify their imagery based on the selected state. 60 * 61 * <li> The {@link #setLevel} method allows the client to supply a single 62 * continuous controller that can modify the Drawable is displayed, such as 63 * a battery level or progress level. Some drawables may modify their 64 * imagery based on the current level. 65 * 66 * <li> A Drawable can perform animations by calling back to its client 67 * through the {@link Callback} interface. All clients should support this 68 * interface (via {@link #setCallback}) so that animations will work. A 69 * simple way to do this is through the system facilities such as 70 * {@link android.view.View#setBackgroundDrawable(Drawable)} and 71 * {@link android.widget.ImageView}. 72 * </ul> 73 * 74 * Though usually not visible to the application, Drawables may take a variety 75 * of forms: 76 * 77 * <ul> 78 * <li> <b>Bitmap</b>: the simplest Drawable, a PNG or JPEG image. 79 * <li> <b>Nine Patch</b>: an extension to the PNG format allows it to 80 * specify information about how to stretch it and place things inside of 81 * it. 82 * <li> <b>Shape</b>: contains simple drawing commands instead of a raw 83 * bitmap, allowing it to resize better in some cases. 84 * <li> <b>Layers</b>: a compound drawable, which draws multiple underlying 85 * drawables on top of each other. 86 * <li> <b>States</b>: a compound drawable that selects one of a set of 87 * drawables based on its state. 88 * <li> <b>Levels</b>: a compound drawable that selects one of a set of 89 * drawables based on its level. 90 * <li> <b>Scale</b>: a compound drawable with a single child drawable, 91 * whose overall size is modified based on the current level. 92 * </ul> 93 * <p>For information and examples of creating drawable resources (XML or bitmap files that 94 * can be loaded in code), see <a href="{@docRoot}devel/resources-i18n.html">Resources 95 * and Internationalization</a>. 96 */ 97public abstract class Drawable { 98 99 private int[] mStateSet = StateSet.WILD_CARD; 100 private int mLevel = 0; 101 private int mChangingConfigurations = 0; 102 private Rect mBounds = new Rect(); 103 /*package*/ Callback mCallback = null; 104 private boolean mVisible = true; 105 106 /** 107 * Draw in its bounds (set via setBounds) respecting optional effects such 108 * as alpha (set via setAlpha) and color filter (set via setColorFilter). 109 * 110 * @param canvas The canvas to draw into 111 */ 112 public abstract void draw(Canvas canvas); 113 114 /** 115 * Specify a bounding rectangle for the Drawable. This is where the drawable 116 * will draw when its draw() method is called. 117 */ 118 public void setBounds(int left, int top, int right, int bottom) { 119 Rect oldBounds = mBounds; 120 121 if (oldBounds.left != left || oldBounds.top != top || 122 oldBounds.right != right || oldBounds.bottom != bottom) { 123 mBounds.set(left, top, right, bottom); 124 onBoundsChange(mBounds); 125 } 126 } 127 128 /** 129 * Specify a bounding rectangle for the Drawable. This is where the drawable 130 * will draw when its draw() method is called. 131 */ 132 public void setBounds(Rect bounds) { 133 setBounds(bounds.left, bounds.top, bounds.right, bounds.bottom); 134 } 135 136 /** 137 * Return a copy of the drawable's bounds in the specified Rect (allocated 138 * by the caller). The bounds specify where this will draw when its draw() 139 * method is called. 140 * 141 * @param bounds Rect to receive the drawable's bounds (allocated by the 142 * caller). 143 */ 144 public final void copyBounds(Rect bounds) { 145 bounds.set(mBounds); 146 } 147 148 /** 149 * Return a copy of the drawable's bounds in a new Rect. This returns the 150 * same values as getBounds(), but the returned object is guaranteed to not 151 * be changed later by the drawable (i.e. it retains no reference to this 152 * rect). If the caller already has a Rect allocated, call copyBounds(rect) 153 * 154 * @return A copy of the drawable's bounds 155 */ 156 public final Rect copyBounds() { 157 return new Rect(mBounds); 158 } 159 160 /** 161 * Return the drawable's bounds Rect. Note: for efficiency, the returned 162 * object may be the same object stored in the drawable (though this is not 163 * guaranteed), so if a persistent copy of the bounds is needed, call 164 * copyBounds(rect) instead. 165 * 166 * @return The bounds of the drawable (which may change later, so caller 167 * beware). 168 */ 169 public final Rect getBounds() { 170 return mBounds; 171 } 172 173 /** 174 * Set a mask of the configuration parameters for which this drawable 175 * may change, requiring that it be re-created. 176 * 177 * @param configs A mask of the changing configuration parameters, as 178 * defined by {@link android.content.res.Configuration}. 179 * 180 * @see android.content.res.Configuration 181 */ 182 public void setChangingConfigurations(int configs) { 183 mChangingConfigurations = configs; 184 } 185 186 /** 187 * Return a mask of the configuration parameters for which this drawable 188 * mau change, requiring that it be re-created. The default implementation 189 * returns whatever was provided through 190 * {@link #setChangingConfigurations(int)} or 0 by default. Subclasses 191 * may extend this to or in the changing configurations of any other 192 * drawables they hold. 193 * 194 * @return Returns a mask of the changing configuration parameters, as 195 * defined by {@link android.content.res.Configuration}. 196 * 197 * @see android.content.res.Configuration 198 */ 199 public int getChangingConfigurations() { 200 return mChangingConfigurations; 201 } 202 203 /** 204 * Set to true to have the drawable dither its colors when drawn to a device 205 * with fewer than 8-bits per color component. This can improve the look on 206 * those devices, but can also slow down the drawing a little. 207 */ 208 public void setDither(boolean dither) {} 209 210 /** 211 * Set to true to have the drawable filter its bitmap when scaled or rotated 212 * (for drawables that use bitmaps). If the drawable does not use bitmaps, 213 * this call is ignored. This can improve the look when scaled or rotated, 214 * but also slows down the drawing. 215 */ 216 public void setFilterBitmap(boolean filter) {} 217 218 /** 219 * Implement this interface if you want to create an animated drawable that 220 * extends {@link android.graphics.drawable.Drawable Drawable}. 221 * Upon retrieving a drawable, use 222 * {@link Drawable#setCallback(android.graphics.drawable.Drawable.Callback)} 223 * to supply your implementation of the interface to the drawable; it uses 224 * this interface to schedule and execute animation changes. 225 */ 226 public static interface Callback { 227 /** 228 * Called when the drawable needs to be redrawn. A view at this point 229 * should invalidate itself (or at least the part of itself where the 230 * drawable appears). 231 * 232 * @param who The drawable that is requesting the update. 233 */ 234 public void invalidateDrawable(Drawable who); 235 236 /** 237 * A Drawable can call this to schedule the next frame of its 238 * animation. An implementation can generally simply call 239 * {@link android.os.Handler#postAtTime(Runnable, Object, long)} with 240 * the parameters <var>(what, who, when)</var> to perform the 241 * scheduling. 242 * 243 * @param who The drawable being scheduled. 244 * @param what The action to execute. 245 * @param when The time (in milliseconds) to run. The timebase is 246 * {@link android.os.SystemClock#uptimeMillis} 247 */ 248 public void scheduleDrawable(Drawable who, Runnable what, long when); 249 250 /** 251 * A Drawable can call this to unschedule an action previously 252 * scheduled with {@link #scheduleDrawable}. An implementation can 253 * generally simply call 254 * {@link android.os.Handler#removeCallbacks(Runnable, Object)} with 255 * the parameters <var>(what, who)</var> to unschedule the drawable. 256 * 257 * @param who The drawable being unscheduled. 258 * @param what The action being unscheduled. 259 */ 260 public void unscheduleDrawable(Drawable who, Runnable what); 261 } 262 263 /** 264 * Bind a {@link Callback} object to this Drawable. Required for clients 265 * that want to support animated drawables. 266 * 267 * @param cb The client's Callback implementation. 268 */ 269 public final void setCallback(Callback cb) { 270 mCallback = cb; 271 } 272 273 /** 274 * Use the current {@link Callback} implementation to have this Drawable 275 * redrawn. Does nothing if there is no Callback attached to the 276 * Drawable. 277 * 278 * @see Callback#invalidateDrawable 279 */ 280 public void invalidateSelf() 281 { 282 if (mCallback != null) { 283 mCallback.invalidateDrawable(this); 284 } 285 } 286 287 /** 288 * Use the current {@link Callback} implementation to have this Drawable 289 * scheduled. Does nothing if there is no Callback attached to the 290 * Drawable. 291 * 292 * @param what The action being scheduled. 293 * @param when The time (in milliseconds) to run. 294 * 295 * @see Callback#scheduleDrawable 296 */ 297 public void scheduleSelf(Runnable what, long when) 298 { 299 if (mCallback != null) { 300 mCallback.scheduleDrawable(this, what, when); 301 } 302 } 303 304 /** 305 * Use the current {@link Callback} implementation to have this Drawable 306 * unscheduled. Does nothing if there is no Callback attached to the 307 * Drawable. 308 * 309 * @param what The runnable that you no longer want called. 310 * 311 * @see Callback#unscheduleDrawable 312 */ 313 public void unscheduleSelf(Runnable what) 314 { 315 if (mCallback != null) { 316 mCallback.unscheduleDrawable(this, what); 317 } 318 } 319 320 /** 321 * Specify an alpha value for the drawable. 0 means fully transparent, and 322 * 255 means fully opaque. 323 */ 324 public abstract void setAlpha(int alpha); 325 326 /** 327 * Specify an optional colorFilter for the drawable. Pass null to remove 328 * any filters. 329 */ 330 public abstract void setColorFilter(ColorFilter cf); 331 332 /** 333 * Specify a color and porterduff mode to be the colorfilter for this 334 * drawable. 335 */ 336 public void setColorFilter(int color, PorterDuff.Mode mode) { 337 setColorFilter(new PorterDuffColorFilter(color, mode)); 338 } 339 340 public void clearColorFilter() { 341 setColorFilter(null); 342 } 343 344 /** 345 * Indicates whether this view will change its appearance based on state. 346 * Clients can use this to determine whether it is necessary to calculate 347 * their state and call setState. 348 * 349 * @return True if this view changes its appearance based on state, false 350 * otherwise. 351 * 352 * @see #setState(int[]) 353 */ 354 public boolean isStateful() { 355 return false; 356 } 357 358 /** 359 * Specify a set of states for the drawable. These are use-case specific, 360 * so see the relevant documentation. As an example, the background for 361 * widgets like Button understand the following states: 362 * [{@link android.R.attr#state_focused}, 363 * {@link android.R.attr#state_pressed}]. 364 * 365 * <p>If the new state you are supplying causes the appearance of the 366 * Drawable to change, then it is responsible for calling 367 * {@link #invalidateSelf} in order to have itself redrawn, <em>and</em> 368 * true will be returned from this function. 369 * 370 * <p>Note: The Drawable holds a reference on to <var>stateSet</var> 371 * until a new state array is given to it, so you must not modify this 372 * array during that time.</p> 373 * 374 * @param stateSet The new set of states to be displayed. 375 * 376 * @return Returns true if this change in state has caused the appearance 377 * of the Drawable to change (hence requiring an invalidate), otherwise 378 * returns false. 379 */ 380 public boolean setState(final int[] stateSet) { 381 if (!Arrays.equals(mStateSet, stateSet)) { 382 mStateSet = stateSet; 383 return onStateChange(stateSet); 384 } 385 return false; 386 } 387 388 /** 389 * Describes the current state, as a union of primitve states, such as 390 * {@link android.R.attr#state_focused}, 391 * {@link android.R.attr#state_selected}, etc. 392 * Some drawables may modify their imagery based on the selected state. 393 * @return An array of resource Ids describing the current state. 394 */ 395 public int[] getState() { 396 return mStateSet; 397 } 398 399 /** 400 * @return The current drawable that will be used by this drawable. For simple drawables, this 401 * is just the drawable itself. For drawables that change state like 402 * {@link StateListDrawable} and {@link LevelListDrawable} this will be the child drawable 403 * currently in use. 404 */ 405 public Drawable getCurrent() { 406 return this; 407 } 408 409 /** 410 * Specify the level for the drawable. This allows a drawable to vary its 411 * imagery based on a continuous controller, for example to show progress 412 * or volume level. 413 * 414 * <p>If the new level you are supplying causes the appearance of the 415 * Drawable to change, then it is responsible for calling 416 * {@link #invalidateSelf} in order to have itself redrawn, <em>and</em> 417 * true will be returned from this function. 418 * 419 * @param level The new level, from 0 (minimum) to 10000 (maximum). 420 * 421 * @return Returns true if this change in level has caused the appearance 422 * of the Drawable to change (hence requiring an invalidate), otherwise 423 * returns false. 424 */ 425 public final boolean setLevel(int level) { 426 if (mLevel != level) { 427 mLevel = level; 428 return onLevelChange(level); 429 } 430 return false; 431 } 432 433 /** 434 * Retrieve the current level. 435 * 436 * @return int Current level, from 0 (minimum) to 10000 (maximum). 437 */ 438 public final int getLevel() { 439 return mLevel; 440 } 441 442 /** 443 * Set whether this Drawable is visible. This generally does not impact 444 * the Drawable's behavior, but is a hint that can be used by some 445 * Drawables, for example, to decide whether run animations. 446 * 447 * @param visible Set to true if visible, false if not. 448 * @param restart You can supply true here to force the drawable to behave 449 * as if it has just become visible, even if it had last 450 * been set visible. Used for example to force animations 451 * to restart. 452 * 453 * @return boolean Returns true if the new visibility is different than 454 * its previous state. 455 */ 456 public boolean setVisible(boolean visible, boolean restart) { 457 boolean changed = mVisible != visible; 458 mVisible = visible; 459 return changed; 460 } 461 462 public final boolean isVisible() { 463 return mVisible; 464 } 465 466 /** 467 * Return the opacity/transparency of this Drawable. The returned value is 468 * one of the abstract format constants in 469 * {@link android.graphics.PixelFormat}: 470 * {@link android.graphics.PixelFormat#UNKNOWN}, 471 * {@link android.graphics.PixelFormat#TRANSLUCENT}, 472 * {@link android.graphics.PixelFormat#TRANSPARENT}, or 473 * {@link android.graphics.PixelFormat#OPAQUE}. 474 * 475 * <p>Generally a Drawable should be as conservative as possible with the 476 * value it returns. For example, if it contains multiple child drawables 477 * and only shows one of them at a time, if only one of the children is 478 * TRANSLUCENT and the others are OPAQUE then TRANSLUCENT should be 479 * returned. You can use the method {@link #resolveOpacity} to perform a 480 * standard reduction of two opacities to the appropriate single output. 481 * 482 * <p>Note that the returned value does <em>not</em> take into account a 483 * custom alpha or color filter that has been applied by the client through 484 * the {@link #setAlpha} or {@link #setColorFilter} methods. 485 * 486 * @return int The opacity class of the Drawable. 487 * 488 * @see android.graphics.PixelFormat 489 */ 490 public abstract int getOpacity(); 491 492 /** 493 * Return the appropriate opacity value for two source opacities. If 494 * either is UNKNOWN, that is returned; else, if either is TRANSLUCENT, 495 * that is returned; else, if either is TRANSPARENT, that is returned; 496 * else, OPAQUE is returned. 497 * 498 * <p>This is to help in implementing {@link #getOpacity}. 499 * 500 * @param op1 One opacity value. 501 * @param op2 Another opacity value. 502 * 503 * @return int The combined opacity value. 504 * 505 * @see #getOpacity 506 */ 507 public static int resolveOpacity(int op1, int op2) { 508 if (op1 == op2) { 509 return op1; 510 } 511 if (op1 == PixelFormat.UNKNOWN || op2 == PixelFormat.UNKNOWN) { 512 return PixelFormat.UNKNOWN; 513 } 514 if (op1 == PixelFormat.TRANSLUCENT || op2 == PixelFormat.TRANSLUCENT) { 515 return PixelFormat.TRANSLUCENT; 516 } 517 if (op1 == PixelFormat.TRANSPARENT || op2 == PixelFormat.TRANSPARENT) { 518 return PixelFormat.TRANSPARENT; 519 } 520 return PixelFormat.OPAQUE; 521 } 522 523 /** 524 * Returns a Region representing the part of the Drawable that is completely 525 * transparent. This can be used to perform drawing operations, identifying 526 * which parts of the target will not change when rendering the Drawable. 527 * The default implementation returns null, indicating no transparent 528 * region; subclasses can optionally override this to return an actual 529 * Region if they want to supply this optimization information, but it is 530 * not required that they do so. 531 * 532 * @return Returns null if the Drawables has no transparent region to 533 * report, else a Region holding the parts of the Drawable's bounds that 534 * are transparent. 535 */ 536 public Region getTransparentRegion() { 537 return null; 538 } 539 540 /** 541 * Override this in your subclass to change appearance if you recognize the 542 * specified state. 543 * 544 * @return Returns true if the state change has caused the appearance of 545 * the Drawable to change (that is, it needs to be drawn), else false 546 * if it looks the same and there is no need to redraw it since its 547 * last state. 548 */ 549 protected boolean onStateChange(int[] state) { return false; } 550 /** Override this in your subclass to change appearance if you vary based 551 * on level. 552 * @return Returns true if the level change has caused the appearance of 553 * the Drawable to change (that is, it needs to be drawn), else false 554 * if it looks the same and there is no need to redraw it since its 555 * last level. 556 */ 557 protected boolean onLevelChange(int level) { return false; } 558 /** 559 * Override this in your subclass to change appearance if you recognize the 560 * specified state. 561 */ 562 protected void onBoundsChange(Rect bounds) {} 563 564 /** 565 * Return the intrinsic width of the underlying drawable object. Returns 566 * -1 if it has no intrinsic width, such as with a solid color. 567 */ 568 public int getIntrinsicWidth() { 569 return -1; 570 } 571 572 /** 573 * Return the intrinsic height of the underlying drawable object. Returns 574 * -1 if it has no intrinsic height, such as with a solid color. 575 */ 576 public int getIntrinsicHeight() { 577 return -1; 578 } 579 580 /** 581 * Returns the minimum width suggested by this Drawable. If a View uses this 582 * Drawable as a background, it is suggested that the View use at least this 583 * value for its width. (There will be some scenarios where this will not be 584 * possible.) This value should INCLUDE any padding. 585 * 586 * @return The minimum width suggested by this Drawable. If this Drawable 587 * doesn't have a suggested minimum width, 0 is returned. 588 */ 589 public int getMinimumWidth() { 590 final int intrinsicWidth = getIntrinsicWidth(); 591 return intrinsicWidth > 0 ? intrinsicWidth : 0; 592 } 593 594 /** 595 * Returns the minimum height suggested by this Drawable. If a View uses this 596 * Drawable as a background, it is suggested that the View use at least this 597 * value for its height. (There will be some scenarios where this will not be 598 * possible.) This value should INCLUDE any padding. 599 * 600 * @return The minimum height suggested by this Drawable. If this Drawable 601 * doesn't have a suggested minimum height, 0 is returned. 602 */ 603 public int getMinimumHeight() { 604 final int intrinsicHeight = getIntrinsicHeight(); 605 return intrinsicHeight > 0 ? intrinsicHeight : 0; 606 } 607 608 /** 609 * Return in padding the insets suggested by this Drawable for placing 610 * content inside the drawable's bounds. Positive values move toward the 611 * center of the Drawable (set Rect.inset). Returns true if this drawable 612 * actually has a padding, else false. When false is returned, the padding 613 * is always set to 0. 614 */ 615 public boolean getPadding(Rect padding) { 616 padding.set(0, 0, 0, 0); 617 return false; 618 } 619 620 /** 621 * Create a drawable from an inputstream 622 */ 623 public static Drawable createFromStream(InputStream is, String srcName) { 624 if (is == null) { 625 return null; 626 } 627 628 /* ugh. The decodeStream contract is that we have already allocated 629 the pad rect, but if the bitmap does not had a ninepatch chunk, 630 then the pad will be ignored. If we could change this to lazily 631 alloc/assign the rect, we could avoid the GC churn of making new 632 Rects only to drop them on the floor. 633 */ 634 Rect pad = new Rect(); 635 Bitmap bm = BitmapFactory.decodeStream(is, pad, null); 636 if (bm != null) { 637 byte[] np = bm.getNinePatchChunk(); 638 if (np == null || !NinePatch.isNinePatchChunk(np)) { 639 np = null; 640 pad = null; 641 } 642 return drawableFromBitmap(bm, np, pad, srcName); 643 } 644 return null; 645 } 646 647 /** 648 * Create a drawable from an XML document. For more information on how to 649 * create resources in XML, see 650 * <a href="{@docRoot}devel/resources-i18n.html">Resources and 651 * Internationalization</a>. 652 */ 653 public static Drawable createFromXml(Resources r, XmlPullParser parser) 654 throws XmlPullParserException, IOException { 655 AttributeSet attrs = Xml.asAttributeSet(parser); 656 657 int type; 658 while ((type=parser.next()) != XmlPullParser.START_TAG && 659 type != XmlPullParser.END_DOCUMENT) { 660 // Empty loop 661 } 662 663 if (type != XmlPullParser.START_TAG) { 664 throw new XmlPullParserException("No start tag found"); 665 } 666 667 Drawable drawable = createFromXmlInner(r, parser, attrs); 668 669 if (drawable == null) { 670 throw new RuntimeException("Unknown initial tag: " + parser.getName()); 671 } 672 673 return drawable; 674 } 675 676 /** 677 * Create from inside an XML document. Called on a parser positioned at 678 * a tag in an XML document, tries to create a Drawable from that tag. 679 * Returns null if the tag is not a valid drawable. 680 */ 681 public static Drawable createFromXmlInner(Resources r, XmlPullParser parser, AttributeSet attrs) 682 throws XmlPullParserException, IOException { 683 Drawable drawable; 684 685 final String name = parser.getName(); 686 687 if (name.equals("selector")) { 688 drawable = new StateListDrawable(); 689 } else if (name.equals("level-list")) { 690 drawable = new LevelListDrawable(); 691 } else if (name.equals("layer-list")) { 692 drawable = new LayerDrawable(); 693 } else if (name.equals("transition")) { 694 drawable = new TransitionDrawable(); 695 } else if (name.equals("color")) { 696 drawable = new ColorDrawable(); 697 } else if (name.equals("shape")) { 698 drawable = new GradientDrawable(); 699 } else if (name.equals("scale")) { 700 drawable = new ScaleDrawable(); 701 } else if (name.equals("clip")) { 702 drawable = new ClipDrawable(); 703 } else if (name.equals("rotate")) { 704 drawable = new RotateDrawable(); 705 } else if (name.equals("animation-list")) { 706 drawable = new AnimationDrawable(); 707 } else if (name.equals("inset")) { 708 drawable = new InsetDrawable(); 709 } else if (name.equals("bitmap")) { 710 drawable = new BitmapDrawable(); 711 } else { 712 throw new XmlPullParserException(parser.getPositionDescription() + 713 ": invalid drawable tag " + name); 714 } 715 716 drawable.inflate(r, parser, attrs); 717 return drawable; 718 } 719 720 721 /** 722 * Create a drawable from file path name. 723 */ 724 public static Drawable createFromPath(String pathName) { 725 if (pathName == null) { 726 return null; 727 } 728 729 Bitmap bm = BitmapFactory.decodeFile(pathName); 730 if (bm != null) { 731 return drawableFromBitmap(bm, null, null, pathName); 732 } 733 734 return null; 735 } 736 737 public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs) 738 throws XmlPullParserException, IOException { 739 740 TypedArray a = r.obtainAttributes(attrs, com.android.internal.R.styleable.Drawable); 741 inflateWithAttributes(r, parser, a, com.android.internal.R.styleable.Drawable_visible); 742 a.recycle(); 743 } 744 745 void inflateWithAttributes(Resources r, XmlPullParser parser, 746 TypedArray attrs, int visibleAttr) 747 throws XmlPullParserException, IOException { 748 749 mVisible = attrs.getBoolean(visibleAttr, mVisible); 750 } 751 752 public static abstract class ConstantState { 753 public abstract Drawable newDrawable(); 754 public abstract int getChangingConfigurations(); 755 } 756 757 public ConstantState getConstantState() { 758 return null; 759 } 760 761 private static Drawable drawableFromBitmap(Bitmap bm, byte[] np, Rect pad, String srcName) { 762 if (np != null) { 763 return new NinePatchDrawable(bm, np, pad, srcName); 764 } 765 return new BitmapDrawable(bm); 766 } 767} 768 769