Merge change 618 into donut

* changes:
  Fixes #1816088. Avoid initializing empty Rects when they are useless, especially in Zygote.
This commit is contained in:
Android (Google) Code Review
2009-04-28 13:45:44 -07:00

View File

@@ -38,32 +38,32 @@ import android.util.TypedValue;
* dealing with an underlying visual resource that may take a variety of forms. * dealing with an underlying visual resource that may take a variety of forms.
* Unlike a {@link android.view.View}, a Drawable does not have any facility to * Unlike a {@link android.view.View}, a Drawable does not have any facility to
* receive events or otherwise interact with the user. * receive events or otherwise interact with the user.
* *
* <p>In addition to simple drawing, Drawable provides a number of generic * <p>In addition to simple drawing, Drawable provides a number of generic
* mechanisms for its client to interact with what is being drawn: * mechanisms for its client to interact with what is being drawn:
* *
* <ul> * <ul>
* <li> The {@link #setBounds} method <var>must</var> be called to tell the * <li> The {@link #setBounds} method <var>must</var> be called to tell the
* Drawable where it is drawn and how large it should be. All Drawables * Drawable where it is drawn and how large it should be. All Drawables
* should respect the requested size, often simply by scaling their * should respect the requested size, often simply by scaling their
* imagery. A client can find the preferred size for some Drawables with * imagery. A client can find the preferred size for some Drawables with
* the {@link #getIntrinsicHeight} and {@link #getIntrinsicWidth} methods. * the {@link #getIntrinsicHeight} and {@link #getIntrinsicWidth} methods.
* *
* <li> The {@link #getPadding} method can return from some Drawables * <li> The {@link #getPadding} method can return from some Drawables
* information about how to frame content that is placed inside of them. * information about how to frame content that is placed inside of them.
* For example, a Drawable that is intended to be the frame for a button * For example, a Drawable that is intended to be the frame for a button
* widget would need to return padding that correctly places the label * widget would need to return padding that correctly places the label
* inside of itself. * inside of itself.
* *
* <li> The {@link #setState} method allows the client to tell the Drawable * <li> The {@link #setState} method allows the client to tell the Drawable
* in which state it is to be drawn, such as "focused", "selected", etc. * in which state it is to be drawn, such as "focused", "selected", etc.
* Some drawables may modify their imagery based on the selected state. * Some drawables may modify their imagery based on the selected state.
* *
* <li> The {@link #setLevel} method allows the client to supply a single * <li> The {@link #setLevel} method allows the client to supply a single
* continuous controller that can modify the Drawable is displayed, such as * continuous controller that can modify the Drawable is displayed, such as
* a battery level or progress level. Some drawables may modify their * a battery level or progress level. Some drawables may modify their
* imagery based on the current level. * imagery based on the current level.
* *
* <li> A Drawable can perform animations by calling back to its client * <li> A Drawable can perform animations by calling back to its client
* through the {@link Callback} interface. All clients should support this * through the {@link Callback} interface. All clients should support this
* interface (via {@link #setCallback}) so that animations will work. A * interface (via {@link #setCallback}) so that animations will work. A
@@ -71,7 +71,7 @@ import android.util.TypedValue;
* {@link android.view.View#setBackgroundDrawable(Drawable)} and * {@link android.view.View#setBackgroundDrawable(Drawable)} and
* {@link android.widget.ImageView}. * {@link android.widget.ImageView}.
* </ul> * </ul>
* *
* Though usually not visible to the application, Drawables may take a variety * Though usually not visible to the application, Drawables may take a variety
* of forms: * of forms:
* *
@@ -96,11 +96,12 @@ import android.util.TypedValue;
* and Internationalization</a>. * and Internationalization</a>.
*/ */
public abstract class Drawable { public abstract class Drawable {
private static final Rect ZERO_BOUNDS_RECT = new Rect();
private int[] mStateSet = StateSet.WILD_CARD; private int[] mStateSet = StateSet.WILD_CARD;
private int mLevel = 0; private int mLevel = 0;
private int mChangingConfigurations = 0; private int mChangingConfigurations = 0;
private Rect mBounds = new Rect(); private Rect mBounds = ZERO_BOUNDS_RECT;
/*package*/ Callback mCallback = null; /*package*/ Callback mCallback = null;
private boolean mVisible = true; private boolean mVisible = true;
@@ -118,14 +119,18 @@ public abstract class Drawable {
*/ */
public void setBounds(int left, int top, int right, int bottom) { public void setBounds(int left, int top, int right, int bottom) {
Rect oldBounds = mBounds; Rect oldBounds = mBounds;
if (oldBounds == ZERO_BOUNDS_RECT) {
oldBounds = mBounds = new Rect();
}
if (oldBounds.left != left || oldBounds.top != top || if (oldBounds.left != left || oldBounds.top != top ||
oldBounds.right != right || oldBounds.bottom != bottom) { oldBounds.right != right || oldBounds.bottom != bottom) {
mBounds.set(left, top, right, bottom); mBounds.set(left, top, right, bottom);
onBoundsChange(mBounds); onBoundsChange(mBounds);
} }
} }
/** /**
* Specify a bounding rectangle for the Drawable. This is where the drawable * Specify a bounding rectangle for the Drawable. This is where the drawable
* will draw when its draw() method is called. * will draw when its draw() method is called.
@@ -145,12 +150,12 @@ public abstract class Drawable {
public final void copyBounds(Rect bounds) { public final void copyBounds(Rect bounds) {
bounds.set(mBounds); bounds.set(mBounds);
} }
/** /**
* Return a copy of the drawable's bounds in a new Rect. This returns the * Return a copy of the drawable's bounds in a new Rect. This returns the
* same values as getBounds(), but the returned object is guaranteed to not * same values as getBounds(), but the returned object is guaranteed to not
* be changed later by the drawable (i.e. it retains no reference to this * be changed later by the drawable (i.e. it retains no reference to this
* rect). If the caller already has a Rect allocated, call copyBounds(rect) * rect). If the caller already has a Rect allocated, call copyBounds(rect).
* *
* @return A copy of the drawable's bounds * @return A copy of the drawable's bounds
*/ */
@@ -163,27 +168,37 @@ public abstract class Drawable {
* object may be the same object stored in the drawable (though this is not * object may be the same object stored in the drawable (though this is not
* guaranteed), so if a persistent copy of the bounds is needed, call * guaranteed), so if a persistent copy of the bounds is needed, call
* copyBounds(rect) instead. * copyBounds(rect) instead.
* You should also not change the object returned by this method as it may
* be the same object stored in the drawable.
* *
* @return The bounds of the drawable (which may change later, so caller * @return The bounds of the drawable (which may change later, so caller
* beware). * beware). DO NOT ALTER the returned object as it may change the
* stored bounds of this drawable.
*
* @see #copyBounds()
* @see #copyBounds(android.graphics.Rect)
*/ */
public final Rect getBounds() { public final Rect getBounds() {
if (mBounds == ZERO_BOUNDS_RECT) {
mBounds = new Rect();
}
return mBounds; return mBounds;
} }
/** /**
* Set a mask of the configuration parameters for which this drawable * Set a mask of the configuration parameters for which this drawable
* may change, requiring that it be re-created. * may change, requiring that it be re-created.
* *
* @param configs A mask of the changing configuration parameters, as * @param configs A mask of the changing configuration parameters, as
* defined by {@link android.content.res.Configuration}. * defined by {@link android.content.res.Configuration}.
* *
* @see android.content.res.Configuration * @see android.content.res.Configuration
*/ */
public void setChangingConfigurations(int configs) { public void setChangingConfigurations(int configs) {
mChangingConfigurations = configs; mChangingConfigurations = configs;
} }
/** /**
* Return a mask of the configuration parameters for which this drawable * Return a mask of the configuration parameters for which this drawable
* mau change, requiring that it be re-created. The default implementation * mau change, requiring that it be re-created. The default implementation
@@ -191,23 +206,23 @@ public abstract class Drawable {
* {@link #setChangingConfigurations(int)} or 0 by default. Subclasses * {@link #setChangingConfigurations(int)} or 0 by default. Subclasses
* may extend this to or in the changing configurations of any other * may extend this to or in the changing configurations of any other
* drawables they hold. * drawables they hold.
* *
* @return Returns a mask of the changing configuration parameters, as * @return Returns a mask of the changing configuration parameters, as
* defined by {@link android.content.res.Configuration}. * defined by {@link android.content.res.Configuration}.
* *
* @see android.content.res.Configuration * @see android.content.res.Configuration
*/ */
public int getChangingConfigurations() { public int getChangingConfigurations() {
return mChangingConfigurations; return mChangingConfigurations;
} }
/** /**
* Set to true to have the drawable dither its colors when drawn to a device * Set to true to have the drawable dither its colors when drawn to a device
* with fewer than 8-bits per color component. This can improve the look on * with fewer than 8-bits per color component. This can improve the look on
* those devices, but can also slow down the drawing a little. * those devices, but can also slow down the drawing a little.
*/ */
public void setDither(boolean dither) {} public void setDither(boolean dither) {}
/** /**
* Set to true to have the drawable filter its bitmap when scaled or rotated * Set to true to have the drawable filter its bitmap when scaled or rotated
* (for drawables that use bitmaps). If the drawable does not use bitmaps, * (for drawables that use bitmaps). If the drawable does not use bitmaps,
@@ -229,7 +244,7 @@ public abstract class Drawable {
* Called when the drawable needs to be redrawn. A view at this point * Called when the drawable needs to be redrawn. A view at this point
* should invalidate itself (or at least the part of itself where the * should invalidate itself (or at least the part of itself where the
* drawable appears). * drawable appears).
* *
* @param who The drawable that is requesting the update. * @param who The drawable that is requesting the update.
*/ */
public void invalidateDrawable(Drawable who); public void invalidateDrawable(Drawable who);
@@ -240,7 +255,7 @@ public abstract class Drawable {
* {@link android.os.Handler#postAtTime(Runnable, Object, long)} with * {@link android.os.Handler#postAtTime(Runnable, Object, long)} with
* the parameters <var>(what, who, when)</var> to perform the * the parameters <var>(what, who, when)</var> to perform the
* scheduling. * scheduling.
* *
* @param who The drawable being scheduled. * @param who The drawable being scheduled.
* @param what The action to execute. * @param what The action to execute.
* @param when The time (in milliseconds) to run. The timebase is * @param when The time (in milliseconds) to run. The timebase is
@@ -254,7 +269,7 @@ public abstract class Drawable {
* generally simply call * generally simply call
* {@link android.os.Handler#removeCallbacks(Runnable, Object)} with * {@link android.os.Handler#removeCallbacks(Runnable, Object)} with
* the parameters <var>(what, who)</var> to unschedule the drawable. * the parameters <var>(what, who)</var> to unschedule the drawable.
* *
* @param who The drawable being unscheduled. * @param who The drawable being unscheduled.
* @param what The action being unscheduled. * @param what The action being unscheduled.
*/ */
@@ -264,7 +279,7 @@ public abstract class Drawable {
/** /**
* Bind a {@link Callback} object to this Drawable. Required for clients * Bind a {@link Callback} object to this Drawable. Required for clients
* that want to support animated drawables. * that want to support animated drawables.
* *
* @param cb The client's Callback implementation. * @param cb The client's Callback implementation.
*/ */
public final void setCallback(Callback cb) { public final void setCallback(Callback cb) {
@@ -275,7 +290,7 @@ public abstract class Drawable {
* Use the current {@link Callback} implementation to have this Drawable * Use the current {@link Callback} implementation to have this Drawable
* redrawn. Does nothing if there is no Callback attached to the * redrawn. Does nothing if there is no Callback attached to the
* Drawable. * Drawable.
* *
* @see Callback#invalidateDrawable * @see Callback#invalidateDrawable
*/ */
public void invalidateSelf() public void invalidateSelf()
@@ -289,10 +304,10 @@ public abstract class Drawable {
* Use the current {@link Callback} implementation to have this Drawable * Use the current {@link Callback} implementation to have this Drawable
* scheduled. Does nothing if there is no Callback attached to the * scheduled. Does nothing if there is no Callback attached to the
* Drawable. * Drawable.
* *
* @param what The action being scheduled. * @param what The action being scheduled.
* @param when The time (in milliseconds) to run. * @param when The time (in milliseconds) to run.
* *
* @see Callback#scheduleDrawable * @see Callback#scheduleDrawable
*/ */
public void scheduleSelf(Runnable what, long when) public void scheduleSelf(Runnable what, long when)
@@ -306,9 +321,9 @@ public abstract class Drawable {
* Use the current {@link Callback} implementation to have this Drawable * Use the current {@link Callback} implementation to have this Drawable
* unscheduled. Does nothing if there is no Callback attached to the * unscheduled. Does nothing if there is no Callback attached to the
* Drawable. * Drawable.
* *
* @param what The runnable that you no longer want called. * @param what The runnable that you no longer want called.
* *
* @see Callback#unscheduleDrawable * @see Callback#unscheduleDrawable
*/ */
public void unscheduleSelf(Runnable what) public void unscheduleSelf(Runnable what)
@@ -323,13 +338,13 @@ public abstract class Drawable {
* 255 means fully opaque. * 255 means fully opaque.
*/ */
public abstract void setAlpha(int alpha); public abstract void setAlpha(int alpha);
/** /**
* Specify an optional colorFilter for the drawable. Pass null to remove * Specify an optional colorFilter for the drawable. Pass null to remove
* any filters. * any filters.
*/ */
public abstract void setColorFilter(ColorFilter cf); public abstract void setColorFilter(ColorFilter cf);
/** /**
* Specify a color and porterduff mode to be the colorfilter for this * Specify a color and porterduff mode to be the colorfilter for this
* drawable. * drawable.
@@ -337,7 +352,7 @@ public abstract class Drawable {
public void setColorFilter(int color, PorterDuff.Mode mode) { public void setColorFilter(int color, PorterDuff.Mode mode) {
setColorFilter(new PorterDuffColorFilter(color, mode)); setColorFilter(new PorterDuffColorFilter(color, mode));
} }
public void clearColorFilter() { public void clearColorFilter() {
setColorFilter(null); setColorFilter(null);
} }
@@ -346,34 +361,34 @@ public abstract class Drawable {
* Indicates whether this view will change its appearance based on state. * Indicates whether this view will change its appearance based on state.
* Clients can use this to determine whether it is necessary to calculate * Clients can use this to determine whether it is necessary to calculate
* their state and call setState. * their state and call setState.
* *
* @return True if this view changes its appearance based on state, false * @return True if this view changes its appearance based on state, false
* otherwise. * otherwise.
* *
* @see #setState(int[]) * @see #setState(int[])
*/ */
public boolean isStateful() { public boolean isStateful() {
return false; return false;
} }
/** /**
* Specify a set of states for the drawable. These are use-case specific, * Specify a set of states for the drawable. These are use-case specific,
* so see the relevant documentation. As an example, the background for * so see the relevant documentation. As an example, the background for
* widgets like Button understand the following states: * widgets like Button understand the following states:
* [{@link android.R.attr#state_focused}, * [{@link android.R.attr#state_focused},
* {@link android.R.attr#state_pressed}]. * {@link android.R.attr#state_pressed}].
* *
* <p>If the new state you are supplying causes the appearance of the * <p>If the new state you are supplying causes the appearance of the
* Drawable to change, then it is responsible for calling * Drawable to change, then it is responsible for calling
* {@link #invalidateSelf} in order to have itself redrawn, <em>and</em> * {@link #invalidateSelf} in order to have itself redrawn, <em>and</em>
* true will be returned from this function. * true will be returned from this function.
* *
* <p>Note: The Drawable holds a reference on to <var>stateSet</var> * <p>Note: The Drawable holds a reference on to <var>stateSet</var>
* until a new state array is given to it, so you must not modify this * until a new state array is given to it, so you must not modify this
* array during that time.</p> * array during that time.</p>
* *
* @param stateSet The new set of states to be displayed. * @param stateSet The new set of states to be displayed.
* *
* @return Returns true if this change in state has caused the appearance * @return Returns true if this change in state has caused the appearance
* of the Drawable to change (hence requiring an invalidate), otherwise * of the Drawable to change (hence requiring an invalidate), otherwise
* returns false. * returns false.
@@ -396,7 +411,7 @@ public abstract class Drawable {
public int[] getState() { public int[] getState() {
return mStateSet; return mStateSet;
} }
/** /**
* @return The current drawable that will be used by this drawable. For simple drawables, this * @return The current drawable that will be used by this drawable. For simple drawables, this
* is just the drawable itself. For drawables that change state like * is just the drawable itself. For drawables that change state like
@@ -416,9 +431,9 @@ public abstract class Drawable {
* Drawable to change, then it is responsible for calling * Drawable to change, then it is responsible for calling
* {@link #invalidateSelf} in order to have itself redrawn, <em>and</em> * {@link #invalidateSelf} in order to have itself redrawn, <em>and</em>
* true will be returned from this function. * true will be returned from this function.
* *
* @param level The new level, from 0 (minimum) to 10000 (maximum). * @param level The new level, from 0 (minimum) to 10000 (maximum).
* *
* @return Returns true if this change in level has caused the appearance * @return Returns true if this change in level has caused the appearance
* of the Drawable to change (hence requiring an invalidate), otherwise * of the Drawable to change (hence requiring an invalidate), otherwise
* returns false. * returns false.
@@ -529,7 +544,7 @@ public abstract class Drawable {
* region; subclasses can optionally override this to return an actual * region; subclasses can optionally override this to return an actual
* Region if they want to supply this optimization information, but it is * Region if they want to supply this optimization information, but it is
* not required that they do so. * not required that they do so.
* *
* @return Returns null if the Drawables has no transparent region to * @return Returns null if the Drawables has no transparent region to
* report, else a Region holding the parts of the Drawable's bounds that * report, else a Region holding the parts of the Drawable's bounds that
* are transparent. * are transparent.
@@ -537,11 +552,11 @@ public abstract class Drawable {
public Region getTransparentRegion() { public Region getTransparentRegion() {
return null; return null;
} }
/** /**
* Override this in your subclass to change appearance if you recognize the * Override this in your subclass to change appearance if you recognize the
* specified state. * specified state.
* *
* @return Returns true if the state change has caused the appearance of * @return Returns true if the state change has caused the appearance of
* the Drawable to change (that is, it needs to be drawn), else false * the Drawable to change (that is, it needs to be drawn), else false
* if it looks the same and there is no need to redraw it since its * if it looks the same and there is no need to redraw it since its
@@ -577,13 +592,13 @@ public abstract class Drawable {
public int getIntrinsicHeight() { public int getIntrinsicHeight() {
return -1; return -1;
} }
/** /**
* Returns the minimum width suggested by this Drawable. If a View uses this * Returns the minimum width suggested by this Drawable. If a View uses this
* Drawable as a background, it is suggested that the View use at least this * Drawable as a background, it is suggested that the View use at least this
* value for its width. (There will be some scenarios where this will not be * value for its width. (There will be some scenarios where this will not be
* possible.) This value should INCLUDE any padding. * possible.) This value should INCLUDE any padding.
* *
* @return The minimum width suggested by this Drawable. If this Drawable * @return The minimum width suggested by this Drawable. If this Drawable
* doesn't have a suggested minimum width, 0 is returned. * doesn't have a suggested minimum width, 0 is returned.
*/ */
@@ -597,7 +612,7 @@ public abstract class Drawable {
* Drawable as a background, it is suggested that the View use at least this * Drawable as a background, it is suggested that the View use at least this
* value for its height. (There will be some scenarios where this will not be * value for its height. (There will be some scenarios where this will not be
* possible.) This value should INCLUDE any padding. * possible.) This value should INCLUDE any padding.
* *
* @return The minimum height suggested by this Drawable. If this Drawable * @return The minimum height suggested by this Drawable. If this Drawable
* doesn't have a suggested minimum height, 0 is returned. * doesn't have a suggested minimum height, 0 is returned.
*/ */
@@ -605,7 +620,7 @@ public abstract class Drawable {
final int intrinsicHeight = getIntrinsicHeight(); final int intrinsicHeight = getIntrinsicHeight();
return intrinsicHeight > 0 ? intrinsicHeight : 0; return intrinsicHeight > 0 ? intrinsicHeight : 0;
} }
/** /**
* Return in padding the insets suggested by this Drawable for placing * Return in padding the insets suggested by this Drawable for placing
* content inside the drawable's bounds. Positive values move toward the * content inside the drawable's bounds. Positive values move toward the
@@ -643,7 +658,7 @@ public abstract class Drawable {
/** /**
* Create a drawable from an inputstream * Create a drawable from an inputstream
* *
* @hide pending API council approval * @hide pending API council approval
*/ */
public static Drawable createFromResourceStream(Resources res, TypedValue value, public static Drawable createFromResourceStream(Resources res, TypedValue value,