androidx.core.graphics.drawable


Classes

DrawableCompat

Helper for accessing features in android.graphics.drawable.Drawable.

IconCompat

Helper for accessing features in android.graphics.drawable.Icon.

RoundedBitmapDrawable

A Drawable that wraps a bitmap and can be drawn with rounded corners.

RoundedBitmapDrawableFactory

Constructs RoundedBitmapDrawable objects, either from Bitmaps directly, or from streams and files.

Extension functions summary

inline Icon

Create an Icon from this adaptive Bitmap.

Bitmap
Drawable.toBitmap(width: @Px Int, height: @Px Int, config: Bitmap.Config?)

Return a Bitmap representation of this Drawable.

Bitmap?
Drawable.toBitmapOrNull(
    width: @Px Int,
    height: @Px Int,
    config: Bitmap.Config?
)

Returns a Bitmap representation of this Drawable or null if the drawable cannot be represented as a bitmap.

inline ColorDrawable

Create a ColorDrawable from this color value.

inline ColorDrawable
@RequiresApi(value = 26)
Color.toDrawable()

Create a ColorDrawable from this Color (via Color.toArgb).

inline BitmapDrawable

Create a BitmapDrawable from this Bitmap.

inline Icon
@RequiresApi(value = 26)
Bitmap.toIcon()

Create an Icon from this Bitmap.

inline Icon
@RequiresApi(value = 26)
Uri.toIcon()

Create an Icon from this Uri.

inline Icon
@RequiresApi(value = 26)
ByteArray.toIcon()

Create an Icon from this ByteArray.

Unit
Drawable.updateBounds(left: @Px Int, top: @Px Int, right: @Px Int, bottom: @Px Int)

Updates this drawable's bounds.

Extension functions

toAdaptiveIcon

@RequiresApi(value = 26)
inline fun Bitmap.toAdaptiveIcon(): Icon

Create an Icon from this adaptive Bitmap.

toBitmap

fun Drawable.toBitmap(
    width: @Px Int = intrinsicWidth,
    height: @Px Int = intrinsicHeight,
    config: Bitmap.Config? = null
): Bitmap

Return a Bitmap representation of this Drawable.

If this instance is a BitmapDrawable and the width, height, and config match, the underlying Bitmap instance will be returned directly. If any of those three properties differ then a new Bitmap is created. For all other Drawable types, a new Bitmap is created.

Parameters
width: @Px Int = intrinsicWidth

Width of the desired bitmap. Defaults to Drawable.getIntrinsicWidth.

height: @Px Int = intrinsicHeight

Height of the desired bitmap. Defaults to Drawable.getIntrinsicHeight.

config: Bitmap.Config? = null

Bitmap config of the desired bitmap. Null attempts to use the native config, if any. Defaults to Config.ARGB_8888 otherwise.

Throws
kotlin.IllegalArgumentException

if the underlying drawable is a BitmapDrawable where BitmapDrawable.getBitmap returns null or the drawable cannot otherwise be represented as a bitmap

See also
toBitmapOrNull

toBitmapOrNull

fun Drawable.toBitmapOrNull(
    width: @Px Int = intrinsicWidth,
    height: @Px Int = intrinsicHeight,
    config: Bitmap.Config? = null
): Bitmap?

Returns a Bitmap representation of this Drawable or null if the drawable cannot be represented as a bitmap.

If this instance is a BitmapDrawable and the width, height, and config match, the underlying Bitmap instance will be returned directly. If any of those three properties differ then a new Bitmap is created. For all other Drawable types, a new Bitmap is created.

If the result of BitmapDrawable.getBitmap is null or the drawable cannot otherwise be represented as a bitmap, returns null.

Parameters
width: @Px Int = intrinsicWidth

Width of the desired bitmap. Defaults to Drawable.getIntrinsicWidth.

height: @Px Int = intrinsicHeight

Height of the desired bitmap. Defaults to Drawable.getIntrinsicHeight.

config: Bitmap.Config? = null

Bitmap config of the desired bitmap. Null attempts to use the native config, if any. Defaults to Config.ARGB_8888 otherwise.

See also
toBitmap

toDrawable

inline fun @ColorInt Int.toDrawable(): ColorDrawable

Create a ColorDrawable from this color value.

toDrawable

@RequiresApi(value = 26)
inline fun Color.toDrawable(): ColorDrawable

Create a ColorDrawable from this Color (via Color.toArgb).

toDrawable

inline fun Bitmap.toDrawable(resources: Resources): BitmapDrawable

Create a BitmapDrawable from this Bitmap.

toIcon

@RequiresApi(value = 26)
inline fun Bitmap.toIcon(): Icon

Create an Icon from this Bitmap.

See also
createWithBitmap

toIcon

@RequiresApi(value = 26)
inline fun Uri.toIcon(): Icon

Create an Icon from this Uri.

toIcon

@RequiresApi(value = 26)
inline fun ByteArray.toIcon(): Icon

Create an Icon from this ByteArray.

See also
createWithData

updateBounds

fun Drawable.updateBounds(
    left: @Px Int = bounds.left,
    top: @Px Int = bounds.top,
    right: @Px Int = bounds.right,
    bottom: @Px Int = bounds.bottom
): Unit

Updates this drawable's bounds. This version of the method allows using named parameters to just set one or more axes.

See also
setBounds