Android APIs
public abstract class

RoundedBitmapDrawable

extends Drawable
java.lang.Object
   ↳ android.graphics.drawable.Drawable
     ↳ android.support.v4.graphics.drawable.RoundedBitmapDrawable

Class Overview

A Drawable that wraps a bitmap and can be drawn with rounded corners. You can create a RoundedBitmapDrawable from a file path, an input stream, or from a Bitmap object.

Also see the Bitmap class, which handles the management and transformation of raw bitmap graphics, and should be used when drawing to a Canvas.

Summary

Public Methods
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.
final Bitmap getBitmap()
Returns the bitmap used by this drawable to render.
ColorFilter getColorFilter()
Returns the current color filter, or null if none set.
float getCornerRadius()
int getGravity()
Get the gravity used to position/stretch the bitmap within its bounds.
int getIntrinsicHeight()
Return the intrinsic height of the underlying drawable object.
int getIntrinsicWidth()
Return the intrinsic width of the underlying drawable object.
int getOpacity()
Return the opacity/transparency of this Drawable.
final Paint getPaint()
Returns the paint used to render this drawable.
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.
boolean isCircular()
void setAlpha(int alpha)
Specify an alpha value for the drawable.
void setAntiAlias(boolean aa)
Enables or disables anti-aliasing for this drawable.
void setCircular(boolean circular)
Sets the image shape to circular.
void setColorFilter(ColorFilter cf)
Specify an optional color filter for the drawable.
void setCornerRadius(float cornerRadius)
Sets the corner radius to be applied when drawing the bitmap.
void setDither(boolean dither)
This method is deprecated. This property is ignored.
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(int density)
Set the density at which this drawable will be rendered.
void setTargetDensity(DisplayMetrics metrics)
Set the density scale at which this drawable will be rendered.
void setTargetDensity(Canvas canvas)
Set the density scale at which this drawable will be rendered.
Protected Methods
void onBoundsChange(Rect bounds)
Override this in your subclass to change appearance if you vary based on the bounds.
[Expand]
Inherited Methods
From class android.graphics.drawable.Drawable
From class java.lang.Object

Public Methods

public 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).

Parameters
canvas The canvas to draw into

public int getAlpha ()

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.

public final Bitmap getBitmap ()

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

public ColorFilter getColorFilter ()

Returns the current color filter, or null if none set.

Returns
  • the current color filter, or null if none set

public float getCornerRadius ()

Returns
  • The corner radius applied when drawing the bitmap.

public int getGravity ()

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

Returns
  • the gravity applied to the bitmap
See Also

public int getIntrinsicHeight ()

Return the intrinsic height of the underlying drawable object. Returns -1 if it has no intrinsic height, such as with a solid color.

public int getIntrinsicWidth ()

Return the intrinsic width of the underlying drawable object. Returns -1 if it has no intrinsic width, such as with a solid color.

public int getOpacity ()

Return the opacity/transparency of this Drawable. The returned value is one of the abstract format constants in PixelFormat: UNKNOWN, TRANSLUCENT, TRANSPARENT, or 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 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 setAlpha(int) or setColorFilter(ColorFilter) methods. Some subclasses, such as BitmapDrawable, ColorDrawable, and GradientDrawable, do account for the value of setAlpha(int), but the general behavior is dependent upon the implementation of the subclass.

Returns
  • int The opacity class of the Drawable.

public final Paint getPaint ()

Returns the paint used to render this drawable.

public boolean hasAntiAlias ()

Indicates whether anti-aliasing is enabled for this drawable.

Returns
  • True if anti-aliasing is enabled, false otherwise.

public boolean hasMipMap ()

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

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

public boolean isCircular ()

Returns
  • true if the image is circular, else false.

public void setAlpha (int alpha)

Specify an alpha value for the drawable. 0 means fully transparent, and 255 means fully opaque.

public void setAntiAlias (boolean aa)

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 True if the bitmap should be anti-aliased, false otherwise.
See Also

public void setCircular (boolean circular)

Sets the image shape to circular.

This overwrites any calls made to setCornerRadius(float) so far.

public void setColorFilter (ColorFilter cf)

Specify an optional color filter for the 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.

Parameters
cf The color filter to apply, or null to remove the existing color filter

public void setCornerRadius (float cornerRadius)

Sets the corner radius to be applied when drawing the bitmap.

public void setDither (boolean dither)

This method is deprecated.
This property is ignored.

Set to true to have the drawable dither its colors when drawn to a device with fewer than 8-bits per color component.

public void setFilterBitmap (boolean filter)

Set to true to have the drawable filter its bitmaps with bilinear sampling when they are scaled or rotated.

This can improve appearance when bitmaps are rotated. If the drawable does not use bitmaps, this call is ignored.

public void setGravity (int gravity)

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

Parameters
gravity the gravity
See Also

public void setMipMap (boolean mipMap)

Enables or disables the mipmap hint for this drawable's bitmap. See setHasMipMap(boolean) for more information. If the bitmap is null, or the current API version does not support setting a mipmap hint, calling this method has no effect.

Parameters
mipMap True if the bitmap should use mipmaps, false otherwise.
See Also

public void setTargetDensity (int density)

Set the density at which this drawable will be rendered.

Parameters
density The density scale for this drawable.

public void setTargetDensity (DisplayMetrics metrics)

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

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

public void setTargetDensity (Canvas canvas)

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 The Canvas from which the density scale must be obtained.

Protected Methods

protected void onBoundsChange (Rect bounds)

Override this in your subclass to change appearance if you vary based on the bounds.