Added in API level 1

BitmapDrawable

open class BitmapDrawable : Drawable
kotlin.Any
   ↳ 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 android.graphics.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 android.graphics.Bitmap class, which handles the management and transformation of raw bitmap graphics, and should be used when drawing to a android.graphics.Canvas.

Summary

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 Enables or disables the mipmap hint.
android:src Identifier of the bitmap file.
android:tileMode Defines the tile mode.
Public constructors

Create an empty drawable, not dealing with density.

Create an empty drawable, setting initial target density based on the display metrics of the resources.

Create drawable from a bitmap, not dealing with density.

BitmapDrawable(res: Resources!, bitmap: Bitmap!)

Create drawable from a bitmap, setting initial target density based on the display metrics of the resources.

BitmapDrawable(filepath: String!)

Create a drawable by opening a given file path and decoding the bitmap.

BitmapDrawable(res: Resources!, filepath: String!)

Create a drawable by opening a given file path and decoding the bitmap.

Create a drawable by decoding a bitmap from the given input stream.

Create a drawable by decoding a bitmap from the given input stream.

Public methods
open Unit

open Boolean

open Unit
draw(canvas: Canvas)

open Int

Bitmap!

Returns the bitmap used by this drawable to render.

open Int

Return a mask of the configuration parameters for which this drawable may change, requiring that it be re-created.

open ColorFilter?

Drawable.ConstantState?

open Int

Get the gravity used to position/stretch the bitmap within its bounds.

open Int

open Int

open Int

open Insets

open Unit
getOutline(outline: Outline)

Called to get the drawable to populate the Outline that defines its drawing area.

Paint!

Returns the paint used to render this drawable.

open Shader.TileMode!

Indicates the repeat behavior of this drawable on the X axis.

open Shader.TileMode!

Indicates the repeat behavior of this drawable on the Y axis.

open Boolean

Indicates whether anti-aliasing is enabled for this drawable.

open Boolean

open Boolean

Indicates whether the mipmap hint is enabled on this drawable's bitmap.

open Unit
inflate(r: Resources, parser: XmlPullParser, attrs: AttributeSet, theme: Resources.Theme?)

Boolean

open Boolean

open Boolean

open Drawable

A mutable BitmapDrawable still shares its Bitmap with any other Drawable that comes from the same resource.

open Unit
setAlpha(alpha: Int)

open Unit

Enables or disables anti-aliasing for this drawable.

open Unit

open Unit
setBitmap(bitmap: Bitmap?)

Switch to a new Bitmap object.

open Unit
setColorFilter(colorFilter: ColorFilter?)

open Unit
setDither(dither: Boolean)

open Unit

open Unit
setGravity(gravity: Int)

Set the gravity used to position/stretch the bitmap within its bounds.

open Unit
setMipMap(mipMap: Boolean)

Enables or disables the mipmap hint for this drawable's bitmap.

open Unit

Set the density scale at which this drawable will be rendered.

open Unit

Set the density scale at which this drawable will be rendered.

open Unit

Set the density at which this drawable will be rendered.

open Unit

Sets the repeat behavior of this drawable on the X axis.

open Unit

Sets the repeat behavior of this drawable on both axis.

Unit

Sets the repeat behavior of this drawable on the Y axis.

open Unit

Specifies a tint blending mode for this drawable.

open Unit

Protected methods
open Unit

open Boolean

Inherited functions

XML attributes

android:antialias

android:antialias
Enables or disables antialiasing. Antialiasing can be used to smooth the edges of a bitmap when rotated. Default value is false.

May be a boolean value, such as "true" or "false".

android:dither

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). Default value is true.

May be a boolean value, such as "true" or "false".

android:filter

android:filter
Enables or disables bitmap filtering. Filtering is used when the bitmap is shrunk or stretched to smooth its appearance. Default value is true.

May be a boolean value, such as "true" or "false".

android:gravity

android:gravity
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
bottom 50 Push object to the bottom of its container, not changing its size.
center 11 Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.
center_horizontal 1 Place object in the horizontal center of its container, not changing its size.
center_vertical 10 Place object in the vertical center of its container, not changing its size.
clip_horizontal 8 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.
clip_vertical 80 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.
end 800005 Push object to the end of its container, not changing its size.
fill 77 Grow the horizontal and vertical size of the object if needed so it completely fills its container.
fill_horizontal 7 Grow the horizontal size of the object if needed so it completely fills its container.
fill_vertical 70 Grow the vertical size of the object if needed so it completely fills its container.
left 3 Push object to the left of its container, not changing its size.
right 5 Push object to the right of its container, not changing its size.
start 800003 Push object to the beginning of its container, not changing its size.
top 30 Push object to the top of its container, not changing its size.

android:mipMap

android:mipMap
Enables or disables the mipmap hint. See android.graphics.Bitmap#setHasMipMap(boolean) for more information. Default value is false.

May be a boolean value, such as "true" or "false".

android:src

android:src
Identifier of the bitmap file. This attribute is mandatory.

May be a reference to another resource, in the form "@[+][package:]type/name" or a theme attribute in the form "?[package:]type/name".

May be a color value, in the form of "rgb", "argb", "rrggbb", or "aarrggbb".

android:tileMode

android:tileMode
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
clamp 0 Replicates the edge color.
disabled ffffffff Do not tile the bitmap. This is the default value.
mirror 2 Repeats the shader's image horizontally and vertically, alternating mirror images so that adjacent images always seam.
repeat 1 Repeats the bitmap in both direction.

Public constructors

BitmapDrawable

Added in API level 1
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.

Create an empty drawable, not dealing with density.

BitmapDrawable

Added in API level 1
BitmapDrawable(res: Resources!)

Deprecated: 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.

BitmapDrawable

Added in API level 1
BitmapDrawable(bitmap: Bitmap!)

Deprecated: Use BitmapDrawable(android.content.res.Resources,android.graphics.Bitmap) to ensure that the drawable has correctly set its target density.

Create drawable from a bitmap, not dealing with density.

BitmapDrawable

Added in API level 1
BitmapDrawable(
    res: Resources!,
    bitmap: Bitmap!)

Create drawable from a bitmap, setting initial target density based on the display metrics of the resources.

BitmapDrawable

Added in API level 1
BitmapDrawable(filepath: String!)

Deprecated: Use BitmapDrawable(android.content.res.Resources,java.lang.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.

BitmapDrawable

Added in API level 1
BitmapDrawable(
    res: Resources!,
    filepath: String!)

Create a drawable by opening a given file path and decoding the bitmap.

BitmapDrawable

Added in API level 1
BitmapDrawable(is: InputStream!)

Deprecated: Use BitmapDrawable(android.content.res.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.

BitmapDrawable

Added in API level 1
BitmapDrawable(
    res: Resources!,
    is: InputStream!)

Create a drawable by decoding a bitmap from the given input stream.

Public methods

applyTheme

Added in API level 21
open fun applyTheme(t: Resources.Theme): Unit
Parameters
t Resources.Theme: the theme to apply This value cannot be null.

canApplyTheme

Added in API level 21
open fun canApplyTheme(): Boolean

draw

Added in API level 1
open fun draw(canvas: Canvas): Unit
Parameters
canvas Canvas: The canvas to draw into This value cannot be null.

getAlpha

Added in API level 19
open fun getAlpha(): Int
Return
Int Value is between 0 and 255 inclusive

getBitmap

Added in API level 1
fun getBitmap(): Bitmap!

Returns the bitmap used by this drawable to render. May be null.

getColorFilter

Added in API level 21
open fun getColorFilter(): ColorFilter?
Return
ColorFilter? the current color filter, or null if none set

getConstantState

Added in API level 1
fun getConstantState(): Drawable.ConstantState?
Return
Drawable.ConstantState? The ConstantState associated to that Drawable. This value may be null.

getGravity

Added in API level 1
open fun getGravity(): Int

Get the gravity used to position/stretch the bitmap within its bounds. See android.view.Gravity

Return
Int the gravity applied to the bitmap

getIntrinsicHeight

Added in API level 1
open fun getIntrinsicHeight(): Int
Return
Int the intrinsic height, or -1 if no intrinsic height

getIntrinsicWidth

Added in API level 1
open fun getIntrinsicWidth(): Int
Return
Int the intrinsic width, or -1 if no intrinsic width

getOpacity

Added in API level 1
open fun getOpacity(): Int
Return
Int int The opacity class of the Drawable. Value is android.graphics.PixelFormat#UNKNOWN, android.graphics.PixelFormat#TRANSLUCENT, android.graphics.PixelFormat#TRANSPARENT, or android.graphics.PixelFormat#OPAQUE

getOpticalInsets

Added in API level 29
open fun getOpticalInsets(): Insets
Return
Insets This value cannot be null.

getOutline

Added in API level 21
open fun getOutline(outline: Outline): Unit

Called to get the drawable to populate the Outline that defines its drawing area.

This method is called by the default android.view.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.

Parameters
outline Outline: This value cannot be null.

getPaint

Added in API level 1
fun getPaint(): Paint!

Returns the paint used to render this drawable.

getTileModeX

Added in API level 1
open fun getTileModeX(): Shader.TileMode!

Indicates the repeat behavior of this drawable on the X axis.

Return
Shader.TileMode! android.graphics.Shader.TileMode#CLAMP if the bitmap does not repeat, android.graphics.Shader.TileMode#REPEAT or android.graphics.Shader.TileMode#MIRROR otherwise.

getTileModeY

Added in API level 1
open fun getTileModeY(): Shader.TileMode!

Indicates the repeat behavior of this drawable on the Y axis.

Return
Shader.TileMode! android.graphics.Shader.TileMode#CLAMP if the bitmap does not repeat, android.graphics.Shader.TileMode#REPEAT or android.graphics.Shader.TileMode#MIRROR otherwise.

hasAntiAlias

Added in API level 18
open fun hasAntiAlias(): Boolean

Indicates whether anti-aliasing is enabled for this drawable.

Return
Boolean True if anti-aliasing is enabled, false otherwise.

hasFocusStateSpecified

Added in API level 31
open fun hasFocusStateSpecified(): Boolean
Return
Boolean true if android.R.attr#state_focused is specified for this drawable.

hasMipMap

Added in API level 18
open fun hasMipMap(): Boolean

Indicates whether the mipmap hint is enabled on this drawable's bitmap.

Return
Boolean True if the mipmap hint is set, false otherwise. If the bitmap is null, this method always returns false.

inflate

Added in API level 21
open fun inflate(
    r: Resources,
    parser: XmlPullParser,
    attrs: AttributeSet,
    theme: Resources.Theme?
): Unit
Parameters
r Resources: Resources used to resolve attribute values This value cannot be null.
parser XmlPullParser: XML parser from which to inflate this Drawable This value cannot be null.
attrs AttributeSet: Base set of attribute values This value cannot be null.
theme Resources.Theme?: Theme to apply, may be null
Exceptions
org.xmlpull.v1.XmlPullParserException
java.io.IOException

isAutoMirrored

Added in API level 19
fun isAutoMirrored(): Boolean
Return
Boolean boolean Returns true if this Drawable will be automatically mirrored.

isFilterBitmap

Added in API level 23
open fun isFilterBitmap(): Boolean
Return
Boolean whether this drawable filters its bitmaps

isStateful

Added in API level 1
open fun isStateful(): Boolean
Return
Boolean True if this drawable changes its appearance based on state, false otherwise.

mutate

Added in API level 3
open fun mutate(): Drawable

A mutable BitmapDrawable still shares its Bitmap with any other Drawable that comes from the same resource.

Return
Drawable This drawable.

setAlpha

Added in API level 1
open fun setAlpha(alpha: Int): Unit
Parameters
alpha Int: Value is between 0 and 255 inclusive

setAntiAlias

Added in API level 1
open fun setAntiAlias(aa: Boolean): Unit

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.

Parameters
aa Boolean: True if the bitmap should be anti-aliased, false otherwise.

See Also

setAutoMirrored

Added in API level 19
open fun setAutoMirrored(mirrored: Boolean): Unit
Parameters
mirrored Boolean: Set to true if the Drawable should be mirrored, false if not.

setBitmap

Added in API level 31
open fun setBitmap(bitmap: Bitmap?): Unit

Switch to a new Bitmap object.

Parameters
bitmap Bitmap?: This value may be null.

setColorFilter

Added in API level 1
open fun setColorFilter(colorFilter: ColorFilter?): Unit
Parameters
colorFilter ColorFilter?: The color filter to apply, or null to remove the existing color filter

setDither

Added in API level 1
open fun setDither(dither: Boolean): Unit

setFilterBitmap

Added in API level 1
open fun setFilterBitmap(filter: Boolean): Unit

setGravity

Added in API level 1
open fun setGravity(gravity: Int): Unit

Set the gravity used to position/stretch the bitmap within its bounds. See android.view.Gravity

Parameters
gravity Int: the gravity

setMipMap

Added in API level 18
open fun setMipMap(mipMap: Boolean): Unit

Enables or disables the mipmap hint for this drawable's bitmap. See Bitmap#setHasMipMap(boolean) for more information. If the bitmap is null calling this method has no effect.

Parameters
mipMap Boolean: True if the bitmap should use mipmaps, false otherwise.

See Also

setTargetDensity

Added in API level 4
open fun setTargetDensity(canvas: Canvas!): Unit

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.

Parameters
canvas Canvas!: The Canvas from which the density scale must be obtained.

setTargetDensity

Added in API level 4
open fun setTargetDensity(metrics: DisplayMetrics!): Unit

Set the density scale at which this drawable will be rendered.

Parameters
metrics DisplayMetrics!: The DisplayMetrics indicating the density scale for this drawable.

setTargetDensity

Added in API level 4
open fun setTargetDensity(density: Int): Unit

Set the density at which this drawable will be rendered.

Parameters
density Int: The density scale for this drawable.

setTileModeX

Added in API level 1
open fun setTileModeX(mode: Shader.TileMode!): Unit

Sets the repeat behavior of this drawable on the X axis. By default, the drawable does not repeat its bitmap. Using android.graphics.Shader.TileMode#REPEAT or android.graphics.Shader.TileMode#MIRROR the bitmap can be repeated (or tiled) if the bitmap is smaller than this drawable.

Parameters
mode Shader.TileMode!: The repeat mode for this drawable.

setTileModeXY

Added in API level 1
open fun setTileModeXY(
    xmode: Shader.TileMode!,
    ymode: Shader.TileMode!
): Unit

Sets the repeat behavior of this drawable on both axis. By default, the drawable does not repeat its bitmap. Using android.graphics.Shader.TileMode#REPEAT or android.graphics.Shader.TileMode#MIRROR the bitmap can be repeated (or tiled) if the bitmap is smaller than this drawable.

Parameters
xmode Shader.TileMode!: The X repeat mode for this drawable.
ymode Shader.TileMode!: The Y repeat mode for this drawable.

setTileModeY

Added in API level 1
fun setTileModeY(mode: Shader.TileMode!): Unit

Sets the repeat behavior of this drawable on the Y axis. By default, the drawable does not repeat its bitmap. Using android.graphics.Shader.TileMode#REPEAT or android.graphics.Shader.TileMode#MIRROR the bitmap can be repeated (or tiled) if the bitmap is smaller than this drawable.

Parameters
mode Shader.TileMode!: The repeat mode for this drawable.

setTintBlendMode

Added in API level 29
open fun setTintBlendMode(blendMode: BlendMode!): Unit

Specifies a tint blending mode for this drawable.

Defines how this drawable's tint color should be blended into the drawable before it is drawn to screen. Default tint mode is BlendMode#SRC_IN.

Note: Setting a color filter via setColorFilter(android.graphics.ColorFilter)

Parameters
blendMode BlendMode!: This value cannot be null.

setTintList

Added in API level 21
open fun setTintList(tint: ColorStateList?): Unit
Parameters
tint ColorStateList?: Color state list to use for tinting this drawable, or null to clear the tint

Protected methods

onBoundsChange

Added in API level 1
protected open fun onBoundsChange(bounds: Rect): Unit
Parameters
bounds Rect: This value cannot be null.

onStateChange

Added in API level 1
protected open fun onStateChange(stateSet: IntArray): Boolean
Parameters
state This value cannot be null.
Return
Boolean Returns true if the state change has caused the appearance of 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 last state.