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

NinePatchDrawable

public class NinePatchDrawable extends Drawable
A resizeable bitmap, with stretchable areas that you define. This type of image is defined in a .png file with a special format, described in Resources.

Fields Summary
private NinePatchState
mNinePatchState
private NinePatch
mNinePatch
private Rect
mPadding
private Paint
mPaint
private boolean
mMutated
Constructors Summary
NinePatchDrawable()

    
public NinePatchDrawable(Bitmap bitmap, byte[] chunk, Rect padding, String srcName)

        this(new NinePatchState(new NinePatch(bitmap, chunk, srcName), padding));
    
private NinePatchDrawable(NinePatchState state)

        setNinePatchState(state);
    
public NinePatchDrawable(NinePatch patch)

        this(new NinePatchState(patch, null));
    
Methods Summary
public voiddraw(Canvas canvas)

        mNinePatch.draw(canvas, getBounds(), mPaint);
    
public intgetChangingConfigurations()

        return super.getChangingConfigurations() | mNinePatchState.mChangingConfigurations;
    
public ConstantStategetConstantState()

        mNinePatchState.mChangingConfigurations = super.getChangingConfigurations();
        return mNinePatchState;
    
public intgetIntrinsicHeight()
Retrieves the height of the source .png file (before resizing).

        return mNinePatch.getHeight();
    
public intgetIntrinsicWidth()
Retrieves the width of the source .png file (before resizing).

        return mNinePatch.getWidth();
    
public intgetMinimumHeight()

        return mNinePatch.getHeight();
    
public intgetMinimumWidth()

        return mNinePatch.getWidth();
    
public intgetOpacity()
Returns a {@link android.graphics.PixelFormat graphics.PixelFormat} value of OPAQUE or TRANSLUCENT.

        return mNinePatch.hasAlpha() || (mPaint != null && mPaint.getAlpha() < 255) ?
                PixelFormat.TRANSLUCENT : PixelFormat.OPAQUE;
    
public booleangetPadding(Rect padding)

        padding.set(mPadding);
        return true;
    
public PaintgetPaint()

        if (mPaint == null) {
            mPaint = new Paint();
        }
        return mPaint;
    
public RegiongetTransparentRegion()

        return mNinePatch.getTransparentRegion(getBounds());
    
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.NinePatchDrawable);

        final int id = a.getResourceId(com.android.internal.R.styleable.NinePatchDrawable_src, 0);
        if (id == 0) {
            throw new XmlPullParserException(parser.getPositionDescription() +
                    ": <nine-patch> requires a valid src attribute");
        }

        final boolean dither = a.getBoolean(
                com.android.internal.R.styleable.NinePatchDrawable_dither, false);
        final BitmapFactory.Options options = new BitmapFactory.Options();
        if (dither) {
            options.inDither = false;
        }

        final Rect padding = new Rect();        
        Bitmap bitmap = null;

        try {
            final TypedValue value = new TypedValue();
            final InputStream is = r.openRawResource(id, value);

            bitmap = BitmapFactory.decodeStream(r, value, is, padding, options);

            is.close();
        } catch (IOException e) {
            // Ignore
        }

        if (bitmap == null) {
            throw new XmlPullParserException(parser.getPositionDescription() +
                    ": <nine-patch> requires a valid src attribute");
        } else if (bitmap.getNinePatchChunk() == null) {
            throw new XmlPullParserException(parser.getPositionDescription() +
                    ": <nine-patch> requires a valid 9-patch source image");
        }

        setNinePatchState(new NinePatchState(
                new NinePatch(bitmap, bitmap.getNinePatchChunk(), "XML 9-patch"), padding, dither));

        a.recycle();
    
public Drawablemutate()

        if (!mMutated && super.mutate() == this) {
            mNinePatchState = new NinePatchState(mNinePatchState);
            mNinePatch = mNinePatchState.mNinePatch;
            mPadding = mNinePatchState.mPadding;
            mMutated = true;
        }
        return this;
    
public voidsetAlpha(int alpha)

        getPaint().setAlpha(alpha);
    
public voidsetColorFilter(ColorFilter cf)

        getPaint().setColorFilter(cf);
    
public voidsetDither(boolean dither)

        getPaint().setDither(dither);
    
private voidsetNinePatchState(android.graphics.drawable.NinePatchDrawable$NinePatchState state)

        mNinePatchState = state;
        mNinePatch = state.mNinePatch;
        mPadding = state.mPadding;
        if (state.mDither) setDither(state.mDither);