FileDocCategorySizeDatePackage
BitmapDrawable.javaAPI DocAndroid 1.5 API14164Wed May 06 22:42:00 BST 2009android.graphics.drawable

BitmapDrawable

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

It can be defined in an XML file with the <bitmap> element.

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}.

attr
ref android.R.styleable#BitmapDrawable_src
attr
ref android.R.styleable#BitmapDrawable_antialias
attr
ref android.R.styleable#BitmapDrawable_filter
attr
ref android.R.styleable#BitmapDrawable_dither
attr
ref android.R.styleable#BitmapDrawable_gravity
attr
ref android.R.styleable#BitmapDrawable_tileMode

Fields Summary
private static final int
DEFAULT_PAINT_FLAGS
private BitmapState
mBitmapState
private android.graphics.Bitmap
mBitmap
private final android.graphics.Rect
mDstRect
private boolean
mApplyGravity
private boolean
mRebuildShader
private int
mBitmapWidth
private int
mBitmapHeight
private boolean
mMutated
Constructors Summary
public BitmapDrawable()


      
        mBitmapState = new BitmapState((Bitmap) null);
    
public BitmapDrawable(android.graphics.Bitmap bitmap)

        this(new BitmapState(bitmap));
    
public BitmapDrawable(String filepath)

        this(new BitmapState(BitmapFactory.decodeFile(filepath)));
        if (mBitmap == null) {
            android.util.Log.w("BitmapDrawable", "BitmapDrawable cannot decode " + filepath);
        }
    
private BitmapDrawable(BitmapState state)

        mBitmapState = state;
        setBitmap(state.mBitmap);
    
public BitmapDrawable(InputStream is)

        this(new BitmapState(BitmapFactory.decodeStream(is)));
        if (mBitmap == null) {
            android.util.Log.w("BitmapDrawable", "BitmapDrawable cannot decode " + is);
        }
    
Methods Summary
public voiddraw(android.graphics.Canvas canvas)

        Bitmap bitmap = mBitmap;
        if (bitmap != null) {
            final BitmapState state = mBitmapState;
            if (mRebuildShader) {
                Shader.TileMode tmx = state.mTileModeX;
                Shader.TileMode tmy = state.mTileModeY;

                if (tmx == null && tmy == null) {
                    state.mPaint.setShader(null);
                } else {
                    Shader s = new BitmapShader(bitmap,
                            tmx == null ? Shader.TileMode.CLAMP : tmx,
                            tmy == null ? Shader.TileMode.CLAMP : tmy);
                    state.mPaint.setShader(s);
                }
                mRebuildShader = false;
                copyBounds(mDstRect);
            }

            Shader shader = state.mPaint.getShader();
            if (shader == null) {
                if (mApplyGravity) {
                    Gravity.apply(state.mGravity, mBitmapWidth, mBitmapHeight,
                            getBounds(), mDstRect);
                    mApplyGravity = false;
                }
                canvas.drawBitmap(bitmap, null, mDstRect, state.mPaint);
            } else {
                if (mApplyGravity) {
                    mDstRect.set(getBounds());
                    mApplyGravity = false;
                }
                canvas.drawRect(mDstRect, state.mPaint);
            }
        }
    
public final android.graphics.BitmapgetBitmap()

        return mBitmap;
    
public intgetChangingConfigurations()

        return super.getChangingConfigurations() | mBitmapState.mChangingConfigurations;
    
public final ConstantStategetConstantState()

        mBitmapState.mChangingConfigurations = super.getChangingConfigurations();
        return mBitmapState;
    
public intgetGravity()
Get the gravity used to position/stretch the bitmap within its bounds. See android.view.Gravity

return
the gravity applied to the bitmap

        return mBitmapState.mGravity;
    
public intgetIntrinsicHeight()

        final Bitmap bitmap = mBitmap;
        final BitmapState state = mBitmapState;

        if (!state.mAutoScale || state.mBitmapScale == Bitmap.DENSITY_SCALE_UNKNOWN) {
            return mBitmapHeight;
        } else {
            return bitmap != null ? (int) (mBitmapHeight /
                    (state.mBitmapScale / state.mTargetDensityScale) + 0.5f) : -1;
        }
    
public intgetIntrinsicWidth()

        final Bitmap bitmap = mBitmap;
        final BitmapState state = mBitmapState;

        if (!state.mAutoScale || state.mBitmapScale == Bitmap.DENSITY_SCALE_UNKNOWN) {
            return mBitmapWidth;
        } else {
            return bitmap != null ? (int) (mBitmapWidth /
                    (state.mBitmapScale / state.mTargetDensityScale) + 0.5f) : -1;

        }
    
public intgetOpacity()

        if (mBitmapState.mGravity != Gravity.FILL) {
            return PixelFormat.TRANSLUCENT;
        }
        Bitmap bm = mBitmap;
        return (bm == null || bm.hasAlpha() || mBitmapState.mPaint.getAlpha() < 255) ?
                PixelFormat.TRANSLUCENT : PixelFormat.OPAQUE;
    
public final android.graphics.PaintgetPaint()

        return mBitmapState.mPaint;
    
public Shader.TileModegetTileModeX()

        return mBitmapState.mTileModeX;
    
public Shader.TileModegetTileModeY()

        return mBitmapState.mTileModeY;
    
public voidinflate(android.content.res.Resources r, org.xmlpull.v1.XmlPullParser parser, android.util.AttributeSet attrs)

        super.inflate(r, parser, attrs);

        TypedArray a = r.obtainAttributes(attrs, com.android.internal.R.styleable.BitmapDrawable);

        final int id = a.getResourceId(com.android.internal.R.styleable.BitmapDrawable_src, 0);
        if (id == 0) {
            throw new XmlPullParserException(parser.getPositionDescription() +
                    ": <bitmap> requires a valid src attribute");
        }
        final Bitmap bitmap = BitmapFactory.decodeResource(r, id);
        if (bitmap == null) {
            throw new XmlPullParserException(parser.getPositionDescription() +
                    ": <bitmap> requires a valid src attribute");
        }
        mBitmapState.mBitmap = bitmap;
        setBitmap(bitmap);
        setDensityScale(r.getDisplayMetrics());

        final Paint paint = mBitmapState.mPaint;
        paint.setAntiAlias(a.getBoolean(com.android.internal.R.styleable.BitmapDrawable_antialias,
                paint.isAntiAlias()));
        paint.setFilterBitmap(a.getBoolean(com.android.internal.R.styleable.BitmapDrawable_filter,
                paint.isFilterBitmap()));
        paint.setDither(a.getBoolean(com.android.internal.R.styleable.BitmapDrawable_dither,
                paint.isDither()));
        setGravity(a.getInt(com.android.internal.R.styleable.BitmapDrawable_gravity, Gravity.FILL));
        int tileMode = a.getInt(com.android.internal.R.styleable.BitmapDrawable_tileMode, -1);
        if (tileMode != -1) {
            switch (tileMode) {
                case 0:
                    setTileModeXY(Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
                    break;
                case 1:
                    setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
                    break;
                case 2:
                    setTileModeXY(Shader.TileMode.MIRROR, Shader.TileMode.MIRROR);
                    break;
            }
        }

        a.recycle();
    
public Drawablemutate()
A mutable BitmapDrawable still shares its Bitmap with any other Drawable that comes from the same resource.

return
This drawable.

        if (!mMutated && super.mutate() == this) {
            mBitmapState = new BitmapState(mBitmapState);
            mMutated = true;
        }
        return this;
    
protected voidonBoundsChange(android.graphics.Rect bounds)

        super.onBoundsChange(bounds);
        mApplyGravity = true;
    
public voidsetAlpha(int alpha)

        mBitmapState.mPaint.setAlpha(alpha);
    
public voidsetAntiAlias(boolean aa)

        mBitmapState.mPaint.setAntiAlias(aa);
    
private voidsetBitmap(android.graphics.Bitmap bitmap)

        mBitmap = bitmap;
        if (bitmap != null) {
            mBitmapWidth = bitmap.getWidth();
            mBitmapHeight = bitmap.getHeight();
        } else {
            mBitmapWidth = mBitmapHeight = -1;
        }
    
public voidsetColorFilter(android.graphics.ColorFilter cf)

        mBitmapState.mPaint.setColorFilter(cf);
    
public voidsetDensityScale(float density)
Set the density scale at which this drawable will be rendered.

param
density The density scale for this drawable.
see
android.graphics.Bitmap#setDensityScale(float)
see
android.graphics.Bitmap#getDensityScale()
hide
pending API council approval

        density = (density == Bitmap.DENSITY_SCALE_UNKNOWN ? 1.0f : density);
        mBitmapState.mTargetDensityScale = density;
    
public voidsetDensityScale(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#setDensityScale(float)
see
android.graphics.Bitmap#getDensityScale()
hide
pending API council approval

        setDensityScale(canvas.getDensityScale());
    
public voidsetDensityScale(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#setDensityScale(float)
see
android.graphics.Bitmap#getDensityScale()
hide
pending API council approval

        setDensityScale(metrics.density);
    
public voidsetDither(boolean dither)

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

        mBitmapState.mPaint.setFilterBitmap(filter);
    
public voidsetGravity(int gravity)
Set the gravity used to position/stretch the bitmap within its bounds. See android.view.Gravity

param
gravity the gravity

        mBitmapState.mGravity = gravity;
        mApplyGravity = true;
    
public voidsetTileModeX(Shader.TileMode mode)

        setTileModeXY(mode, mBitmapState.mTileModeY);
    
public voidsetTileModeXY(Shader.TileMode xmode, Shader.TileMode ymode)

        final BitmapState state = mBitmapState;
        if (state.mTileModeX != xmode || state.mTileModeY != ymode) {
            state.mTileModeX = xmode;
            state.mTileModeY = ymode;
            mRebuildShader = true;
        }
    
public final voidsetTileModeY(Shader.TileMode mode)

        setTileModeXY(mBitmapState.mTileModeX, mode);