FileDocCategorySizeDatePackage
RoundedBitmapDrawable.javaAPI DocAndroid 5.1 API9732Thu Mar 12 22:22:56 GMT 2015android.support.v4.graphics.drawable

RoundedBitmapDrawable

public abstract class RoundedBitmapDrawable extends android.graphics.drawable.Drawable
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 {@link android.graphics.Bitmap} object.

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

Fields Summary
private static final int
DEFAULT_PAINT_FLAGS
android.graphics.Bitmap
mBitmap
private int
mTargetDensity
private int
mGravity
private android.graphics.Paint
mPaint
private android.graphics.BitmapShader
mBitmapShader
private float
mCornerRadius
final android.graphics.Rect
mDstRect
final android.graphics.RectF
mDstRectF
private boolean
mApplyGravity
private int
mBitmapWidth
private int
mBitmapHeight
Constructors Summary
RoundedBitmapDrawable(android.content.res.Resources res, android.graphics.Bitmap bitmap)

        if (res != null) {
            mTargetDensity = res.getDisplayMetrics().densityDpi;
        }

        mBitmap = bitmap;
        if (mBitmap != null) {
            computeBitmapSize();
            mBitmapShader = new BitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        } else {
            mBitmapWidth = mBitmapHeight = -1;
        }
    
Methods Summary
private voidcomputeBitmapSize()

        mBitmapWidth = mBitmap.getScaledWidth(mTargetDensity);
        mBitmapHeight = mBitmap.getScaledHeight(mTargetDensity);
    
public voiddraw(android.graphics.Canvas canvas)

        final Bitmap bitmap = mBitmap;
        if (bitmap == null) {
            return;
        }

        updateDstRect();

        final Paint paint = mPaint;
        final Shader shader = paint.getShader();
        if (shader == null) {
            canvas.drawBitmap(bitmap, null, mDstRect, paint);
        } else {
            canvas.drawRoundRect(mDstRectF, mCornerRadius, mCornerRadius, paint);
        }
    
public intgetAlpha()

        return mPaint.getAlpha();
    
public final android.graphics.BitmapgetBitmap()
Returns the bitmap used by this drawable to render. May be null.

        return mBitmap;
    
public android.graphics.ColorFiltergetColorFilter()

        return mPaint.getColorFilter();
    
public floatgetCornerRadius()

return
The corner radius applied when drawing the bitmap.

        return mCornerRadius;
    
public intgetGravity()
Get the gravity used to position/stretch the bitmap within its bounds.

return
the gravity applied to the bitmap
see
android.view.Gravity

        return mGravity;
    
public intgetIntrinsicHeight()

        return mBitmapHeight;
    
public intgetIntrinsicWidth()

        return mBitmapWidth;
    
public intgetOpacity()

        if (mGravity != Gravity.FILL) {
            return PixelFormat.TRANSLUCENT;
        }
        Bitmap bm = mBitmap;
        return (bm == null
                || bm.hasAlpha()
                || mPaint.getAlpha() < 255
                || isGreaterThanZero(mCornerRadius))
                ? PixelFormat.TRANSLUCENT : PixelFormat.OPAQUE;
    
public final android.graphics.PaintgetPaint()
Returns the paint used to render this drawable.


                 
        
        return mPaint;
    
voidgravityCompatApply(int gravity, int bitmapWidth, int bitmapHeight, android.graphics.Rect bounds, android.graphics.Rect outRect)

        throw new UnsupportedOperationException();
    
public booleanhasAntiAlias()
Indicates whether anti-aliasing is enabled for this drawable.

return
True if anti-aliasing is enabled, false otherwise.
see
#setAntiAlias(boolean)

        return mPaint.isAntiAlias();
    
public booleanhasMipMap()
Indicates whether the mipmap hint is enabled on this drawable's bitmap.

return
True if the mipmap hint is set, false otherwise. If the bitmap is null, this method always returns false.
see
#setMipMap(boolean)

        throw new UnsupportedOperationException(); // must be overridden in subclasses
    
private static booleanisGreaterThanZero(float toCompare)

        return Float.compare(toCompare, +0.0f) > 0;
    
public voidsetAlpha(int alpha)

        final int oldAlpha = mPaint.getAlpha();
        if (alpha != oldAlpha) {
            mPaint.setAlpha(alpha);
            invalidateSelf();
        }
    
public voidsetAntiAlias(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.

param
aa True if the bitmap should be anti-aliased, false otherwise.
see
#hasAntiAlias()

        mPaint.setAntiAlias(aa);
        invalidateSelf();
    
public voidsetColorFilter(android.graphics.ColorFilter cf)

        mPaint.setColorFilter(cf);
        invalidateSelf();
    
public voidsetCornerRadius(float cornerRadius)
Sets the corner radius to be applied when drawing the bitmap.

        if (isGreaterThanZero(cornerRadius)) {
            mPaint.setShader(mBitmapShader);
        } else {
            mPaint.setShader(null);
        }
        mCornerRadius = cornerRadius;
    
public voidsetDither(boolean dither)

        mPaint.setDither(dither);
        invalidateSelf();
    
public voidsetFilterBitmap(boolean filter)

        mPaint.setFilterBitmap(filter);
        invalidateSelf();
    
public voidsetGravity(int gravity)
Set the gravity used to position/stretch the bitmap within its bounds.

param
gravity the gravity
see
android.view.Gravity

        if (mGravity != gravity) {
            mGravity = gravity;
            mApplyGravity = true;
            invalidateSelf();
        }
    
public voidsetMipMap(boolean mipMap)
Enables or disables the mipmap hint for this drawable's bitmap. See {@link Bitmap#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.

param
mipMap True if the bitmap should use mipmaps, false otherwise.
see
#hasMipMap()

        throw new UnsupportedOperationException(); // must be overridden in subclasses
    
public voidsetTargetDensity(android.graphics.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.

param
canvas The Canvas from which the density scale must be obtained.
see
android.graphics.Bitmap#setDensity(int)
see
android.graphics.Bitmap#getDensity()

        setTargetDensity(canvas.getDensity());
    
public voidsetTargetDensity(android.util.DisplayMetrics metrics)
Set the density scale at which this drawable will be rendered.

param
metrics The DisplayMetrics indicating the density scale for this drawable.
see
android.graphics.Bitmap#setDensity(int)
see
android.graphics.Bitmap#getDensity()

        setTargetDensity(metrics.densityDpi);
    
public voidsetTargetDensity(int density)
Set the density at which this drawable will be rendered.

param
density The density scale for this drawable.
see
android.graphics.Bitmap#setDensity(int)
see
android.graphics.Bitmap#getDensity()

        if (mTargetDensity != density) {
            mTargetDensity = density == 0 ? DisplayMetrics.DENSITY_DEFAULT : density;
            if (mBitmap != null) {
                computeBitmapSize();
            }
            invalidateSelf();
        }
    
voidupdateDstRect()

        if (mApplyGravity) {
            gravityCompatApply(mGravity, mBitmapWidth, mBitmapHeight,
                    getBounds(), mDstRect);
            mDstRectF.set(mDstRect);
            mApplyGravity = false;
        }