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. |
|
BitmapDrawable(res: Resources!) Create an empty drawable, setting initial target density based on the display metrics of the resources. |
|
BitmapDrawable(bitmap: Bitmap!) 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. |
|
BitmapDrawable(is: InputStream!) Create a drawable by decoding a bitmap from the given input stream. |
|
BitmapDrawable(res: Resources!, is: InputStream!) Create a drawable by decoding a bitmap from the given input stream. |
Public methods | |
---|---|
open Unit | |
open Boolean | |
open Unit | |
open Int |
getAlpha() |
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! |
getPaint() 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 |
mutate() A mutable BitmapDrawable still shares its Bitmap with any other Drawable that comes from the same resource. |
open Unit | |
open Unit |
setAntiAlias(aa: Boolean) Enables or disables anti-aliasing for this drawable. |
open Unit |
setAutoMirrored(mirrored: Boolean) |
open Unit |
Switch to a new Bitmap object. |
open Unit |
setColorFilter(colorFilter: ColorFilter?) |
open Unit | |
open Unit |
setFilterBitmap(filter: Boolean) |
open Unit |
setGravity(gravity: Int) Set the gravity used to position/stretch the bitmap within its bounds. |
open Unit |
Enables or disables the mipmap hint for this drawable's bitmap. |
open Unit |
setTargetDensity(canvas: Canvas!) Set the density scale at which this drawable will be rendered. |
open Unit |
setTargetDensity(metrics: DisplayMetrics!) Set the density scale at which this drawable will be rendered. |
open Unit |
setTargetDensity(density: Int) Set the density at which this drawable will be rendered. |
open Unit |
setTileModeX(mode: Shader.TileMode!) Sets the repeat behavior of this drawable on the X axis. |
open Unit |
setTileModeXY(xmode: Shader.TileMode!, ymode: Shader.TileMode!) Sets the repeat behavior of this drawable on both axis. |
Unit |
setTileModeY(mode: Shader.TileMode!) Sets the repeat behavior of this drawable on the Y axis. |
open Unit |
setTintBlendMode(blendMode: BlendMode!) Specifies a tint blending mode for this drawable. |
open Unit |
setTintList(tint: ColorStateList?) |
Protected methods | |
---|---|
open Unit |
onBoundsChange(bounds: Rect) |
open Boolean |
onStateChange(stateSet: IntArray) |
Inherited functions | |
---|---|
XML attributes
android:antialias
android:antialias
May be a boolean value, such as "true
" or "false
".
android:dither
android:dither
May be a boolean value, such as "true
" or "false
".
android:filter
android:filter
May be a boolean value, such as "true
" or "false
".
android:gravity
android:gravity
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
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
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
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
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
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
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
BitmapDrawable(
res: Resources!,
bitmap: Bitmap!)
Create drawable from a bitmap, setting initial target density based on the display metrics of the resources.
BitmapDrawable
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
BitmapDrawable(
res: Resources!,
filepath: String!)
Create a drawable by opening a given file path and decoding the bitmap.
BitmapDrawable
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
BitmapDrawable(
res: Resources!,
is: InputStream!)
Create a drawable by decoding a bitmap from the given input stream.
Public methods
applyTheme
open fun applyTheme(t: Resources.Theme): Unit
Parameters | |
---|---|
t |
Resources.Theme: the theme to apply This value cannot be null . |
draw
open fun draw(canvas: Canvas): Unit
Parameters | |
---|---|
canvas |
Canvas: The canvas to draw into This value cannot be null . |
getAlpha
open fun getAlpha(): Int
Return | |
---|---|
Int |
Value is between 0 and 255 inclusive |
getBitmap
fun getBitmap(): Bitmap!
Returns the bitmap used by this drawable to render. May be null.
getChangingConfigurations
open fun getChangingConfigurations(): Int
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.
getColorFilter
open fun getColorFilter(): ColorFilter?
Return | |
---|---|
ColorFilter? |
the current color filter, or null if none set |
getConstantState
fun getConstantState(): Drawable.ConstantState?
Return | |
---|---|
Drawable.ConstantState? |
The ConstantState associated to that Drawable. This value may be null . |
getGravity
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
open fun getIntrinsicHeight(): Int
Return | |
---|---|
Int |
the intrinsic height, or -1 if no intrinsic height |
getIntrinsicWidth
open fun getIntrinsicWidth(): Int
Return | |
---|---|
Int |
the intrinsic width, or -1 if no intrinsic width |
getOpacity
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
open fun getOpticalInsets(): Insets
Return | |
---|---|
Insets |
This value cannot be null . |
getOutline
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
fun getPaint(): Paint!
Returns the paint used to render this drawable.
getTileModeX
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
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
open fun hasAntiAlias(): Boolean
Indicates whether anti-aliasing is enabled for this drawable.
Return | |
---|---|
Boolean |
True if anti-aliasing is enabled, false otherwise. |
See Also
hasFocusStateSpecified
open fun hasFocusStateSpecified(): Boolean
Return | |
---|---|
Boolean |
true if android.R.attr#state_focused is specified for this drawable. |
hasMipMap
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. |
See Also
inflate
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
fun isAutoMirrored(): Boolean
Return | |
---|---|
Boolean |
boolean Returns true if this Drawable will be automatically mirrored. |
isFilterBitmap
open fun isFilterBitmap(): Boolean
Return | |
---|---|
Boolean |
whether this drawable filters its bitmaps |
isStateful
open fun isStateful(): Boolean
Return | |
---|---|
Boolean |
True if this drawable changes its appearance based on state, false otherwise. |
mutate
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
open fun setAlpha(alpha: Int): Unit
Parameters | |
---|---|
alpha |
Int: Value is between 0 and 255 inclusive |
setAntiAlias
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
open fun setAutoMirrored(mirrored: Boolean): Unit
Parameters | |
---|---|
mirrored |
Boolean: Set to true if the Drawable should be mirrored, false if not. |
setBitmap
open fun setBitmap(bitmap: Bitmap?): Unit
Switch to a new Bitmap object.
Parameters | |
---|---|
bitmap |
Bitmap?: This value may be null . |
setColorFilter
open fun setColorFilter(colorFilter: ColorFilter?): Unit
Parameters | |
---|---|
colorFilter |
ColorFilter?: The color filter to apply, or null to remove the existing color filter |
setGravity
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
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
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
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
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
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
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
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
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
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
protected open fun onBoundsChange(bounds: Rect): Unit
Parameters | |
---|---|
bounds |
Rect: This value cannot be null . |
onStateChange
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. |