public class BitmapDrawable extends Drawable
Bitmap
object.
It can be defined in an XML file with the <bitmap>
element. For more
information, see the guide to Drawable Resources.
Also see the Bitmap
class, which handles the management and
transformation of raw bitmap graphics, and should be used when drawing to a
Canvas
.
Drawable.Callback, Drawable.ConstantState
Constructor and Description |
---|
BitmapDrawable()
Deprecated.
Use
BitmapDrawable(android.content.res.Resources, android.graphics.Bitmap)
instead to specify a bitmap to draw with and ensure the correct density is set. |
BitmapDrawable(Bitmap bitmap)
Deprecated.
Use
BitmapDrawable(Resources, Bitmap) to ensure
that the drawable has correctly set its target density. |
BitmapDrawable(InputStream is)
Deprecated.
Use
BitmapDrawable(Resources, java.io.InputStream) to ensure
that the drawable has correctly set its target density. |
BitmapDrawable(Resources res)
Deprecated.
Use
BitmapDrawable(android.content.res.Resources, android.graphics.Bitmap)
instead to specify a bitmap to draw with. |
BitmapDrawable(Resources res,
Bitmap bitmap)
Create drawable from a bitmap, setting initial target density based on
the display metrics of the resources.
|
BitmapDrawable(Resources res,
InputStream is)
Create a drawable by decoding a bitmap from the given input stream.
|
BitmapDrawable(Resources res,
String filepath)
Create a drawable by opening a given file path and decoding the bitmap.
|
BitmapDrawable(String filepath)
Deprecated.
Use
BitmapDrawable(Resources, String) to ensure
that the drawable has correctly set its target density. |
Modifier and Type | Method and Description |
---|---|
void |
applyTheme(Resources.Theme t)
Applies the specified theme to this Drawable and its children.
|
boolean |
canApplyTheme() |
void |
clearMutated()
Clears the mutated state, allowing this drawable to be cached and
mutated again.
|
void |
draw(Canvas canvas)
Draw in its bounds (set via setBounds) respecting optional effects such
as alpha (set via setAlpha) and color filter (set via setColorFilter).
|
int |
getAlpha()
Gets the current alpha value for the drawable. 0 means fully transparent,
255 means fully opaque.
|
Bitmap |
getBitmap()
Returns the bitmap used by this drawable to render.
|
int |
getChangingConfigurations()
Return a mask of the configuration parameters for which this drawable
may change, requiring that it be re-created.
|
ColorFilter |
getColorFilter()
Returns the current color filter, or
null if none set. |
Drawable.ConstantState |
getConstantState()
Return a
Drawable.ConstantState instance that holds the shared state of this Drawable. |
int |
getGravity()
Get the gravity used to position/stretch the bitmap within its bounds.
|
int |
getIntrinsicHeight()
Returns the drawable's intrinsic height.
|
int |
getIntrinsicWidth()
Returns the drawable's intrinsic width.
|
int |
getOpacity()
Return the opacity/transparency of this Drawable.
|
Insets |
getOpticalInsets()
Return in insets the layout insets suggested by this Drawable for use with alignment
operations during layout.
|
void |
getOutline(Outline outline)
Called to get the drawable to populate the Outline that defines its drawing area.
|
Paint |
getPaint()
Returns the paint used to render this drawable.
|
Shader.TileMode |
getTileModeX()
Indicates the repeat behavior of this drawable on the X axis.
|
Shader.TileMode |
getTileModeY()
Indicates the repeat behavior of this drawable on the Y axis.
|
ColorStateList |
getTint() |
PorterDuff.Mode |
getTintMode() |
boolean |
hasAntiAlias()
Indicates whether anti-aliasing is enabled for this drawable.
|
boolean |
hasMipMap()
Indicates whether the mipmap hint is enabled on this drawable's bitmap.
|
void |
inflate(Resources r,
XmlPullParser parser,
AttributeSet attrs,
Resources.Theme theme)
Inflate this Drawable from an XML resource optionally styled by a theme.
|
boolean |
isAutoMirrored()
Tells if this Drawable will be automatically mirrored when its layout direction is RTL
right-to-left.
|
boolean |
isFilterBitmap() |
boolean |
isStateful()
Indicates whether this drawable will change its appearance based on
state.
|
Drawable |
mutate()
A mutable BitmapDrawable still shares its Bitmap with any other Drawable
that comes from the same resource.
|
protected void |
onBoundsChange(Rect bounds)
Override this in your subclass to change appearance if you vary based on
the bounds.
|
protected boolean |
onStateChange(int[] stateSet)
Override this in your subclass to change appearance if you recognize the
specified state.
|
void |
setAlpha(int alpha)
Specify an alpha value for the drawable. 0 means fully transparent, and
255 means fully opaque.
|
void |
setAntiAlias(boolean aa)
Enables or disables anti-aliasing for this drawable.
|
void |
setAutoMirrored(boolean mirrored)
Set whether this Drawable is automatically mirrored when its layout direction is RTL
(right-to left).
|
void |
setBitmap(Bitmap bitmap) |
void |
setColorFilter(ColorFilter colorFilter)
Specify an optional color filter for the drawable.
|
void |
setDither(boolean dither)
Set to true to have the drawable dither its colors when drawn to a
device with fewer than 8-bits per color component.
|
void |
setFilterBitmap(boolean filter)
Set to true to have the drawable filter its bitmaps with bilinear
sampling when they are scaled or rotated.
|
void |
setGravity(int gravity)
Set the gravity used to position/stretch the bitmap within its bounds.
|
void |
setMipMap(boolean mipMap)
Enables or disables the mipmap hint for this drawable's bitmap.
|
void |
setTargetDensity(Canvas canvas)
Set the density scale at which this drawable will be rendered.
|
void |
setTargetDensity(DisplayMetrics metrics)
Set the density scale at which this drawable will be rendered.
|
void |
setTargetDensity(int density)
Set the density at which this drawable will be rendered.
|
void |
setTileModeX(Shader.TileMode mode)
Sets the repeat behavior of this drawable on the X axis.
|
void |
setTileModeXY(Shader.TileMode xmode,
Shader.TileMode ymode)
Sets the repeat behavior of this drawable on both axis.
|
void |
setTileModeY(Shader.TileMode mode)
Sets the repeat behavior of this drawable on the Y axis.
|
void |
setTintList(ColorStateList tint)
Specifies tint color for this drawable as a color state list.
|
void |
setTintMode(PorterDuff.Mode tintMode)
Specifies a tint blending mode for this drawable.
|
void |
setXfermode(Xfermode xfermode) |
clearColorFilter, copyBounds, copyBounds, createFromPath, createFromResourceStream, createFromResourceStream, createFromStream, createFromXml, createFromXml, createFromXmlInner, createFromXmlInner, getBounds, getCallback, getCurrent, getDirtyBounds, getHotspotBounds, getLayoutDirection, getLevel, getMinimumHeight, getMinimumWidth, getPadding, getState, getTransparentRegion, inflate, invalidateSelf, isProjected, isVisible, jumpToCurrentState, obtainAttributes, onLayoutDirectionChanged, onLevelChange, parseTintMode, resolveOpacity, scheduleSelf, setBounds, setBounds, setCallback, setChangingConfigurations, setColorFilter, setHotspot, setHotspotBounds, setLayoutDirection, setLevel, setState, setTint, setVisible, unscheduleSelf
@Deprecated public BitmapDrawable()
BitmapDrawable(android.content.res.Resources, android.graphics.Bitmap)
instead to specify a bitmap to draw with and ensure the correct density is set.@Deprecated public BitmapDrawable(Resources res)
BitmapDrawable(android.content.res.Resources, android.graphics.Bitmap)
instead to specify a bitmap to draw with.@Deprecated public BitmapDrawable(Bitmap bitmap)
BitmapDrawable(Resources, Bitmap)
to ensure
that the drawable has correctly set its target density.public BitmapDrawable(Resources res, Bitmap bitmap)
@Deprecated public BitmapDrawable(String filepath)
BitmapDrawable(Resources, String)
to ensure
that the drawable has correctly set its target density.public BitmapDrawable(Resources res, String filepath)
@Deprecated public BitmapDrawable(InputStream is)
BitmapDrawable(Resources, java.io.InputStream)
to ensure
that the drawable has correctly set its target density.public BitmapDrawable(Resources res, InputStream is)
public final Paint getPaint()
public final Bitmap getBitmap()
public void setBitmap(Bitmap bitmap)
public void setTargetDensity(Canvas canvas)
canvas
- The Canvas from which the density scale must be obtained.Bitmap.setDensity(int)
,
Bitmap.getDensity()
public void setTargetDensity(DisplayMetrics metrics)
metrics
- The DisplayMetrics indicating the density scale for this drawable.Bitmap.setDensity(int)
,
Bitmap.getDensity()
public void setTargetDensity(int density)
density
- The density scale for this drawable.Bitmap.setDensity(int)
,
Bitmap.getDensity()
public int getGravity()
public void setGravity(int gravity)
gravity
- the gravitypublic void setMipMap(boolean mipMap)
Bitmap.setHasMipMap(boolean)
for more information.
If the bitmap is null calling this method has no effect.mipMap
- True if the bitmap should use mipmaps, false otherwise.hasMipMap()
public boolean hasMipMap()
setMipMap(boolean)
public void setAntiAlias(boolean aa)
aa
- True if the bitmap should be anti-aliased, false otherwise.hasAntiAlias()
public boolean hasAntiAlias()
setAntiAlias(boolean)
public void setFilterBitmap(boolean filter)
Drawable
This can improve appearance when bitmaps are rotated. If the drawable does not use bitmaps, this call is ignored.
setFilterBitmap
in class Drawable
Drawable.isFilterBitmap()
,
Paint.setFilterBitmap(boolean);
public boolean isFilterBitmap()
isFilterBitmap
in class Drawable
Drawable.setFilterBitmap(boolean)
public void setDither(boolean dither)
Drawable
setDither
in class Drawable
Paint.setDither(boolean);
public Shader.TileMode getTileModeX()
Shader.TileMode.CLAMP
if the bitmap does not repeat,
Shader.TileMode.REPEAT
or
Shader.TileMode.MIRROR
otherwise.public Shader.TileMode getTileModeY()
Shader.TileMode.CLAMP
if the bitmap does not repeat,
Shader.TileMode.REPEAT
or
Shader.TileMode.MIRROR
otherwise.public void setTileModeX(Shader.TileMode mode)
Shader.TileMode.REPEAT
or
Shader.TileMode.MIRROR
the bitmap can be repeated (or tiled)
if the bitmap is smaller than this drawable.mode
- The repeat mode for this drawable.setTileModeY(android.graphics.Shader.TileMode)
,
setTileModeXY(android.graphics.Shader.TileMode, android.graphics.Shader.TileMode)
public final void setTileModeY(Shader.TileMode mode)
Shader.TileMode.REPEAT
or
Shader.TileMode.MIRROR
the bitmap can be repeated (or tiled)
if the bitmap is smaller than this drawable.mode
- The repeat mode for this drawable.setTileModeX(android.graphics.Shader.TileMode)
,
setTileModeXY(android.graphics.Shader.TileMode, android.graphics.Shader.TileMode)
public void setTileModeXY(Shader.TileMode xmode, Shader.TileMode ymode)
Shader.TileMode.REPEAT
or
Shader.TileMode.MIRROR
the bitmap can be repeated (or tiled)
if the bitmap is smaller than this drawable.xmode
- The X repeat mode for this drawable.ymode
- The Y repeat mode for this drawable.setTileModeX(android.graphics.Shader.TileMode)
,
setTileModeY(android.graphics.Shader.TileMode)
public void setAutoMirrored(boolean mirrored)
Drawable
LayoutDirection
.setAutoMirrored
in class Drawable
mirrored
- Set to true if the Drawable should be mirrored, false if not.public final boolean isAutoMirrored()
Drawable
LayoutDirection
.isAutoMirrored
in class Drawable
public int getChangingConfigurations()
Drawable
Drawable.setChangingConfigurations(int)
or 0 by default. Subclasses
may extend this to or in the changing configurations of any other
drawables they hold.getChangingConfigurations
in class Drawable
ActivityInfo
.ActivityInfo
protected void onBoundsChange(Rect bounds)
Drawable
onBoundsChange
in class Drawable
public void draw(Canvas canvas)
Drawable
public Insets getOpticalInsets()
Drawable
getOpticalInsets
in class Drawable
public void getOutline(Outline outline)
Drawable
This method is called by the default ViewOutlineProvider
to define
the outline of the View.
The default behavior defines the outline to be the bounding rectangle of 0 alpha. Subclasses that wish to convey a different shape or alpha value must override this method.
getOutline
in class Drawable
View.setOutlineProvider(android.view.ViewOutlineProvider)
public void setAlpha(int alpha)
Drawable
public int getAlpha()
Drawable
public void setColorFilter(ColorFilter colorFilter)
Drawable
If a Drawable has a ColorFilter, each output pixel of the Drawable's drawing contents will be modified by the color filter before it is blended onto the render target of a Canvas.
Pass null
to remove any existing color filter.
Note: Setting a non-null
color
filter disables tint
.
setColorFilter
in class Drawable
colorFilter
- The color filter to apply, or null
to remove the
existing color filterpublic ColorFilter getColorFilter()
Drawable
null
if none set.getColorFilter
in class Drawable
null
if none setpublic void setTintList(ColorStateList tint)
Drawable
A Drawable's drawing content will be blended together with its tint
before it is drawn to the screen. This functions similarly to
Drawable.setColorFilter(int, PorterDuff.Mode)
.
Note: Setting a color filter via
Drawable.setColorFilter(ColorFilter)
or
Drawable.setColorFilter(int, PorterDuff.Mode)
overrides tint.
setTintList
in class Drawable
tint
- Color state list to use for tinting this drawable, or
null
to clear the tintDrawable.setTint(int)
,
Drawable.setTintMode(PorterDuff.Mode)
public void setTintMode(PorterDuff.Mode tintMode)
Drawable
Defines how this drawable's tint color should be blended into the drawable
before it is drawn to screen. Default tint mode is PorterDuff.Mode#SRC_IN
.
Note: Setting a color filter via
Drawable.setColorFilter(ColorFilter)
or
Drawable.setColorFilter(int, PorterDuff.Mode)
overrides tint.
setTintMode
in class Drawable
tintMode
- A Porter-Duff blending modeDrawable.setTint(int)
,
Drawable.setTintList(ColorStateList)
public ColorStateList getTint()
public PorterDuff.Mode getTintMode()
public void setXfermode(Xfermode xfermode)
setXfermode
in class Drawable
public Drawable mutate()
mutate
in class Drawable
Drawable.ConstantState
,
Drawable.getConstantState()
public void clearMutated()
Drawable
This is hidden because only framework drawables can be cached, so custom drawables don't need to support constant state, mutate(), or clearMutated().
clearMutated
in class Drawable
protected boolean onStateChange(int[] stateSet)
Drawable
onStateChange
in class Drawable
public boolean isStateful()
Drawable
isStateful
in class Drawable
Drawable.setState(int[])
public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Resources.Theme theme) throws XmlPullParserException, IOException
Drawable
inflate
in class Drawable
r
- Resources used to resolve attribute valuesparser
- XML parser from which to inflate this Drawableattrs
- Base set of attribute valuestheme
- Theme to apply, may be nullXmlPullParserException
IOException
public void applyTheme(Resources.Theme t)
Drawable
applyTheme
in class Drawable
t
- the theme to applypublic boolean canApplyTheme()
canApplyTheme
in class Drawable
public int getIntrinsicWidth()
Drawable
Intrinsic width is the width at which the drawable would like to be laid out, including any inherent padding. If the drawable has no intrinsic width, such as a solid color, this method returns -1.
getIntrinsicWidth
in class Drawable
public int getIntrinsicHeight()
Drawable
Intrinsic height is the height at which the drawable would like to be laid out, including any inherent padding. If the drawable has no intrinsic height, such as a solid color, this method returns -1.
getIntrinsicHeight
in class Drawable
public int getOpacity()
Drawable
PixelFormat
:
PixelFormat.UNKNOWN
,
PixelFormat.TRANSLUCENT
,
PixelFormat.TRANSPARENT
, or
PixelFormat.OPAQUE
.
An OPAQUE drawable is one that draws all all content within its bounds, completely covering anything behind the drawable. A TRANSPARENT drawable is one that draws nothing within its bounds, allowing everything behind it to show through. A TRANSLUCENT drawable is a drawable in any other state, where the drawable will draw some, but not all, of the content within its bounds and at least some content behind the drawable will be visible. If the visibility of the drawable's contents cannot be determined, the safest/best return value is TRANSLUCENT.
Generally a Drawable should be as conservative as possible with the
value it returns. For example, if it contains multiple child drawables
and only shows one of them at a time, if only one of the children is
TRANSLUCENT and the others are OPAQUE then TRANSLUCENT should be
returned. You can use the method Drawable.resolveOpacity(int, int)
to perform a
standard reduction of two opacities to the appropriate single output.
Note that the returned value does not necessarily take into account a
custom alpha or color filter that has been applied by the client through
the Drawable.setAlpha(int)
or Drawable.setColorFilter(android.graphics.ColorFilter)
methods. Some subclasses,
such as BitmapDrawable
, ColorDrawable
, and GradientDrawable
,
do account for the value of Drawable.setAlpha(int)
, but the general behavior is dependent
upon the implementation of the subclass.
getOpacity
in class Drawable
PixelFormat
public final Drawable.ConstantState getConstantState()
Drawable
Drawable.ConstantState
instance that holds the shared state of this Drawable.getConstantState
in class Drawable
Drawable.ConstantState
,
Drawable.mutate()