java.lang.Object | ||
↳ | android.graphics.drawable.Drawable | |
↳ | android.graphics.drawable.BitmapDrawable |
A Drawable that wraps a bitmap and can be tiled, stretched, or aligned. You can create a
BitmapDrawable from a file path, an input stream, through XML inflation, or from
a 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
.
XML Attributes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
android:antialias | Enables or disables antialiasing. | ||||||||||
android:dither | Enables or disables dithering of the bitmap if the bitmap does not have the same pixel configuration as the screen (for instance: a ARGB 8888 bitmap with an RGB 565 screen). | ||||||||||
android:filter | Enables or disables bitmap filtering. | ||||||||||
android:gravity | Defines the gravity for the bitmap. | ||||||||||
android:mipMap | hasMipMap() | Enables or disables the mipmap hint. | |||||||||
android:src | Identifier of the bitmap file. | ||||||||||
android:tileMode | Defines the tile mode. |
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
This constructor was deprecated
in API level 4.
Use
BitmapDrawable(android.content.res.Resources, android.graphics.Bitmap)
instead to specify a bitmap to draw with and ensure the correct density is set.
| |||||||||||
This constructor was deprecated
in API level 18.
Use
BitmapDrawable(android.content.res.Resources, android.graphics.Bitmap)
instead to specify a bitmap to draw with.
| |||||||||||
This constructor was deprecated
in API level 4.
Use
BitmapDrawable(Resources, Bitmap) to ensure
that the drawable has correctly set its target density.
| |||||||||||
Create drawable from a bitmap, setting initial target density based on
the display metrics of the resources.
| |||||||||||
This constructor was deprecated
in API level 5.
Use
BitmapDrawable(Resources, String) to ensure
that the drawable has correctly set its target density.
| |||||||||||
Create a drawable by opening a given file path and decoding the bitmap.
| |||||||||||
This constructor was deprecated
in API level 5.
Use
BitmapDrawable(Resources, java.io.InputStream) to ensure
that the drawable has correctly set its target density.
| |||||||||||
Create a drawable by decoding a bitmap from the given input stream.
|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Applies the specified theme to this Drawable and its children.
| |||||||||||
Draw in its bounds (set via setBounds) respecting optional effects such
as alpha (set via setAlpha) and color filter (set via setColorFilter).
| |||||||||||
Gets the current alpha value for the drawable.
| |||||||||||
Returns the bitmap used by this drawable to render.
| |||||||||||
Return a mask of the configuration parameters for which this drawable
may change, requiring that it be re-created.
| |||||||||||
Returns the current color filter, or
null if none set. | |||||||||||
Return a
Drawable.ConstantState instance that holds the shared state of this Drawable. | |||||||||||
Get the gravity used to position/stretch the bitmap within its bounds.
| |||||||||||
Return the intrinsic height of the underlying drawable object.
| |||||||||||
Return the intrinsic width of the underlying drawable object.
| |||||||||||
Return the opacity/transparency of this Drawable.
| |||||||||||
Returns the paint used to render this drawable.
| |||||||||||
Indicates the repeat behavior of this drawable on the X axis.
| |||||||||||
Indicates the repeat behavior of this drawable on the Y axis.
| |||||||||||
Indicates whether anti-aliasing is enabled for this drawable.
| |||||||||||
Indicates whether the mipmap hint is enabled on this drawable's bitmap.
| |||||||||||
Inflate this Drawable from an XML resource optionally styled by a theme.
| |||||||||||
Tells if this Drawable will be automatically mirrored when its layout direction is RTL
right-to-left.
| |||||||||||
Indicates whether this drawable will change its appearance based on
state.
| |||||||||||
A mutable BitmapDrawable still shares its Bitmap with any other Drawable
that comes from the same resource.
| |||||||||||
Specify an alpha value for the drawable.
| |||||||||||
Enables or disables anti-aliasing for this drawable.
| |||||||||||
Set whether this Drawable is automatically mirrored when its layout direction is RTL
(right-to left).
| |||||||||||
Specify an optional color filter for the drawable.
| |||||||||||
Set to true to have the drawable dither its colors when drawn to a device
with fewer than 8-bits per color component.
| |||||||||||
Set to true to have the drawable filter its bitmap when scaled or rotated
(for drawables that use bitmaps).
| |||||||||||
Set the gravity used to position/stretch the bitmap within its bounds.
| |||||||||||
Enables or disables the mipmap hint for this drawable's bitmap.
| |||||||||||
Set the density at which this drawable will be rendered.
| |||||||||||
Set the density scale at which this drawable will be rendered.
| |||||||||||
Set the density scale at which this drawable will be rendered.
| |||||||||||
Sets the repeat behavior of this drawable on the X axis.
| |||||||||||
Sets the repeat behavior of this drawable on both axis.
| |||||||||||
Sets the repeat behavior of this drawable on the Y axis.
| |||||||||||
Specifies a tint and blending mode for this drawable.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Override this in your subclass to change appearance if you vary based on
the bounds.
| |||||||||||
Override this in your subclass to change appearance if you recognize the
specified state.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
android.graphics.drawable.Drawable
| |||||||||||
From class
java.lang.Object
|
Enables or disables antialiasing. Antialiasing can be used to smooth the edges of a bitmap when rotated. Default value is false.
Must be a boolean value, either "true
" or "false
".
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol antialias
.
Enables or disables dithering of the bitmap if the bitmap does not have the same pixel configuration as the screen (for instance: a ARGB 8888 bitmap with an RGB 565 screen). Default value is true.
Must be a boolean value, either "true
" or "false
".
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol dither
.
Enables or disables bitmap filtering. Filtering is used when the bitmap is shrunk or stretched to smooth its apperance. Default value is true.
Must be a boolean value, either "true
" or "false
".
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol filter
.
Defines the gravity for the bitmap. The gravity indicates where to position the drawable in its container if the bitmap is smaller than the container.
Must be one or more (separated by '|') of the following constant values.
Constant | Value | Description |
---|---|---|
top | 0x30 | Push object to the top of its container, not changing its size. |
bottom | 0x50 | Push object to the bottom of its container, not changing its size. |
left | 0x03 | Push object to the left of its container, not changing its size. |
right | 0x05 | Push object to the right of its container, not changing its size. |
center_vertical | 0x10 | Place object in the vertical center of its container, not changing its size. |
fill_vertical | 0x70 | Grow the vertical size of the object if needed so it completely fills its container. |
center_horizontal | 0x01 | Place object in the horizontal center of its container, not changing its size. |
fill_horizontal | 0x07 | Grow the horizontal size of the object if needed so it completely fills its container. |
center | 0x11 | Place the object in the center of its container in both the vertical and horizontal axis, not changing its size. |
fill | 0x77 | Grow the horizontal and vertical size of the object if needed so it completely fills its container. |
clip_vertical | 0x80 | Additional option that can be set to have the top and/or bottom edges of the child clipped to its container's bounds. The clip will be based on the vertical gravity: a top gravity will clip the bottom edge, a bottom gravity will clip the top edge, and neither will clip both edges. |
clip_horizontal | 0x08 | Additional option that can be set to have the left and/or right edges of the child clipped to its container's bounds. The clip will be based on the horizontal gravity: a left gravity will clip the right edge, a right gravity will clip the left edge, and neither will clip both edges. |
start | 0x00800003 | Push object to the beginning of its container, not changing its size. |
end | 0x00800005 | Push object to the end of its container, not changing its size. |
This corresponds to the global attribute
resource symbol gravity
.
Enables or disables the mipmap hint. See
setHasMipMap(boolean)
for more information.
Default value is false.
Must be a boolean value, either "true
" or "false
".
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol mipMap
.
Identifier of the bitmap file. This attribute is mandatory.
May be a reference to another resource, in the form "@[+][package:]type:name
"
or to a theme attribute in the form "?[package:][type:]name
".
May be a color value, in the form of "#rgb
", "#argb
",
"#rrggbb
", or "#aarrggbb
".
This corresponds to the global attribute
resource symbol src
.
Defines the tile mode. When the tile mode is enabled, the bitmap is repeated. Gravity is ignored when the tile mode is enabled. Default value is "disabled".
Must be one of the following constant values.
Constant | Value | Description |
---|---|---|
disabled | -1 | Do not tile the bitmap. This is the default value. |
clamp | 0 | Replicates the edge color. |
repeat | 1 | Repeats the bitmap in both direction. |
mirror | 2 | Repeats the shader's image horizontally and vertically, alternating mirror images so that adjacent images always seam. |
This corresponds to the global attribute
resource symbol tileMode
.
This constructor was deprecated
in API level 4.
Use BitmapDrawable(android.content.res.Resources, android.graphics.Bitmap)
instead to specify a bitmap to draw with and ensure the correct density is set.
Create an empty drawable, not dealing with density.
This constructor was deprecated
in API level 18.
Use BitmapDrawable(android.content.res.Resources, android.graphics.Bitmap)
instead to specify a bitmap to draw with.
Create an empty drawable, setting initial target density based on the display metrics of the resources.
This constructor was deprecated
in API level 4.
Use BitmapDrawable(Resources, Bitmap)
to ensure
that the drawable has correctly set its target density.
Create drawable from a bitmap, not dealing with density.
Create drawable from a bitmap, setting initial target density based on the display metrics of the resources.
This constructor was deprecated
in API level 5.
Use BitmapDrawable(Resources, String)
to ensure
that the drawable has correctly set its target density.
Create a drawable by opening a given file path and decoding the bitmap.
Create a drawable by opening a given file path and decoding the bitmap.
This constructor was deprecated
in API level 5.
Use BitmapDrawable(Resources, java.io.InputStream)
to ensure
that the drawable has correctly set its target density.
Create a drawable by decoding a bitmap from the given input stream.
Create a drawable by decoding a bitmap from the given input stream.
Applies the specified theme to this Drawable and its children.
Draw in its bounds (set via setBounds) respecting optional effects such as alpha (set via setAlpha) and color filter (set via setColorFilter).
canvas | The canvas to draw into |
---|
Gets the current alpha value for the drawable. 0 means fully transparent, 255 means fully opaque. This method is implemented by Drawable subclasses and the value returned is specific to how that class treats alpha. The default return value is 255 if the class does not override this method to return a value specific to its use of alpha.
Returns the bitmap used by this drawable to render. May be null.
Return a mask of the configuration parameters for which this drawable
may change, requiring that it be re-created. The default implementation
returns whatever was provided through
setChangingConfigurations(int)
or 0 by default. Subclasses
may extend this to or in the changing configurations of any other
drawables they hold.
Configuration
.Returns the current color filter, or null
if none set.
null
if none set
Return a Drawable.ConstantState
instance that holds the shared state of this Drawable.
Get the gravity used to position/stretch the bitmap within its bounds. See android.view.Gravity
Return the intrinsic height of the underlying drawable object. Returns -1 if it has no intrinsic height, such as with a solid color.
Return the intrinsic width of the underlying drawable object. Returns -1 if it has no intrinsic width, such as with a solid color.
Return the opacity/transparency of this Drawable. The returned value is
one of the abstract format constants in
PixelFormat
:
UNKNOWN
,
TRANSLUCENT
,
TRANSPARENT
, or
OPAQUE
.
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 resolveOpacity(int, int)
to perform a
standard reduction of two opacities to the appropriate single output.
Note that the returned value does not take into account a
custom alpha or color filter that has been applied by the client through
the setAlpha(int)
or setColorFilter(ColorFilter)
methods.
Indicates whether anti-aliasing is enabled for this drawable.
Indicates whether the mipmap hint is enabled on this drawable's bitmap.
Inflate this Drawable from an XML resource optionally styled by a theme.
r | Resources used to resolve attribute values |
---|---|
parser | XML parser from which to inflate this Drawable |
attrs | Base set of attribute values |
theme | Theme to apply, may be null |
Tells if this Drawable will be automatically mirrored when its layout direction is RTL
right-to-left. See LayoutDirection
.
Indicates whether this drawable will change its appearance based on state. Clients can use this to determine whether it is necessary to calculate their state and call setState.
A mutable BitmapDrawable still shares its Bitmap with any other Drawable that comes from the same resource.
Specify an alpha value for the drawable. 0 means fully transparent, and 255 means fully opaque.
Enables or disables anti-aliasing for this drawable. Anti-aliasing affects the edges of the bitmap only so it applies only when the drawable is rotated.
aa | True if the bitmap should be anti-aliased, false otherwise. |
---|
Set whether this Drawable is automatically mirrored when its layout direction is RTL
(right-to left). See LayoutDirection
.
mirrored | Set to true if the Drawable should be mirrored, false if not. |
---|
Specify an optional color filter for the drawable. Pass null
to
remove any existing color filter.
cf | the color filter to apply, or null to remove the
existing color filter
|
---|
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 those devices, but can also slow down the drawing a little.
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, this call is ignored. This can improve the look when scaled or rotated, but also slows down the drawing.
Set the gravity used to position/stretch the bitmap within its bounds. See android.view.Gravity
gravity | the gravity |
---|
Enables or disables the mipmap hint for this drawable's bitmap.
See 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. |
---|
Set the density at which this drawable will be rendered.
density | The density scale for this drawable. |
---|
Set the density scale at which this drawable will be rendered.
metrics | The DisplayMetrics indicating the density scale for this drawable. |
---|
Set the density scale at which this drawable will be rendered. This method assumes the drawable will be rendered at the same density as the specified canvas.
canvas | The Canvas from which the density scale must be obtained. |
---|
Sets the repeat behavior of this drawable on both axis. By default, the drawable
does not repeat its bitmap. Using REPEAT
or
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. |
Specifies a tint and blending mode for this drawable.
Setting a color filter via setColorFilter(ColorFilter)
overrides
tint.
tint | Color state list to use for tinting this drawable, or null to clear the tint |
---|---|
tintMode | A Porter-Duff blending mode |
Override this in your subclass to change appearance if you vary based on the bounds.
Override this in your subclass to change appearance if you recognize the specified state.