FileDocCategorySizeDatePackage
NinePatch.javaAPI DocAndroid 5.1 API9981Thu Mar 12 22:22:30 GMT 2015android.graphics

NinePatch

public class NinePatch extends Object
The NinePatch class permits drawing a bitmap in nine or more sections. Essentially, it allows the creation of custom graphics that will scale the way that you define, when content added within the image exceeds the normal bounds of the graphic. For a thorough explanation of a NinePatch image, read the discussion in the 2D Graphics document.

The Draw 9-Patch tool offers an extremely handy way to create your NinePatch images, using a WYSIWYG graphics editor.

Fields Summary
private final Bitmap
mBitmap
public final long
mNativeChunk
Used by native code. This pointer is an instance of Res_png_9patch*.
private Paint
mPaint
private String
mSrcName
Constructors Summary
public NinePatch(Bitmap bitmap, byte[] chunk)
Create a drawable projection from a bitmap to nine patches.

param
bitmap The bitmap describing the patches.
param
chunk The 9-patch data chunk describing how the underlying bitmap is split apart and drawn.

        this(bitmap, chunk, null);
    
public NinePatch(Bitmap bitmap, byte[] chunk, String srcName)
Create a drawable projection from a bitmap to nine patches.

param
bitmap The bitmap describing the patches.
param
chunk The 9-patch data chunk describing how the underlying bitmap is split apart and drawn.
param
srcName The name of the source for the bitmap. Might be null.

        mBitmap = bitmap;
        mSrcName = srcName;
        mNativeChunk = validateNinePatchChunk(mBitmap.ni(), chunk);
    
public NinePatch(NinePatch patch)

hide

        mBitmap = patch.mBitmap;
        mSrcName = patch.mSrcName;
        if (patch.mPaint != null) {
            mPaint = new Paint(patch.mPaint);
        }
        // No need to validate the 9patch chunk again, it was done by
        // the instance we're copying from
        mNativeChunk = patch.mNativeChunk;
    
Methods Summary
public voiddraw(Canvas canvas, Rect location)
Draws the NinePatch. This method will use the paint returned by {@link #getPaint()}.

param
canvas A container for the current matrix and clip used to draw the NinePatch.
param
location Where to draw the NinePatch.

        canvas.drawPatch(this, location, mPaint);
    
public voiddraw(Canvas canvas, Rect location, Paint paint)
Draws the NinePatch. This method will ignore the paint returned by {@link #getPaint()} and use the specified paint instead.

param
canvas A container for the current matrix and clip used to draw the NinePatch.
param
location Where to draw the NinePatch.
param
paint The Paint to draw through.

        canvas.drawPatch(this, location, paint);
    
public voiddraw(Canvas canvas, RectF location)
Draws the NinePatch. This method will use the paint returned by {@link #getPaint()}.

param
canvas A container for the current matrix and clip used to draw the NinePatch.
param
location Where to draw the NinePatch.

        canvas.drawPatch(this, location, mPaint);
    
voiddrawSoftware(Canvas canvas, RectF location, Paint paint)

        nativeDraw(canvas.getNativeCanvasWrapper(), location, mBitmap.ni(), mNativeChunk,
                paint != null ? paint.mNativePaint : 0, canvas.mDensity, mBitmap.mDensity);
    
voiddrawSoftware(Canvas canvas, Rect location, Paint paint)

        nativeDraw(canvas.getNativeCanvasWrapper(), location, mBitmap.ni(), mNativeChunk,
                paint != null ? paint.mNativePaint : 0, canvas.mDensity, mBitmap.mDensity);
    
protected voidfinalize()

        try {
            if (mNativeChunk != 0) {
                // only attempt to destroy correctly initilized chunks
                nativeFinalize(mNativeChunk);
            }
        } finally {
            super.finalize();
        }
    
public BitmapgetBitmap()
Returns the bitmap used to draw this NinePatch.

        return mBitmap;
    
public intgetDensity()
Return the underlying bitmap's density, as per {@link Bitmap#getDensity() Bitmap.getDensity()}.

        return mBitmap.mDensity;
    
public intgetHeight()
Returns the intrinsic height, in pixels, of this NinePatch. This is equivalent to querying the height of the underlying bitmap returned by {@link #getBitmap()}.

        return mBitmap.getHeight();
    
public java.lang.StringgetName()
Returns the name of this NinePatch object if one was specified when calling the constructor.

        return mSrcName;
    
public PaintgetPaint()
Returns the paint used to draw this NinePatch. The paint can be null.

see
#setPaint(Paint)
see
#draw(Canvas, Rect)
see
#draw(Canvas, RectF)

        return mPaint;
    
public final RegiongetTransparentRegion(Rect bounds)
Returns a {@link Region} representing the parts of the NinePatch that are completely transparent.

param
bounds The location and size of the NinePatch.
return
null if the NinePatch has no transparent region to report, else a {@link Region} holding the parts of the specified bounds that are transparent.

        long r = nativeGetTransparentRegion(mBitmap.ni(), mNativeChunk, bounds);
        return r != 0 ? new Region(r) : null;
    
public intgetWidth()
Returns the intrinsic width, in pixels, of this NinePatch. This is equivalent to querying the width of the underlying bitmap returned by {@link #getBitmap()}.

        return mBitmap.getWidth();
    
public final booleanhasAlpha()
Indicates whether this NinePatch contains transparent or translucent pixels. This is equivalent to calling getBitmap().hasAlpha() on this NinePatch.

        return mBitmap.hasAlpha();
    
public static native booleanisNinePatchChunk(byte[] chunk)
Verifies that the specified byte array is a valid 9-patch data chunk.

param
chunk A byte array representing a 9-patch data chunk.
return
True if the specified byte array represents a 9-patch data chunk, false otherwise.

private static native voidnativeDraw(long canvas_instance, RectF loc, long bitmap_instance, long c, long paint_instance_or_null, int destDensity, int srcDensity)

private static native voidnativeDraw(long canvas_instance, Rect loc, long bitmap_instance, long c, long paint_instance_or_null, int destDensity, int srcDensity)

private static native voidnativeFinalize(long chunk)

private static native longnativeGetTransparentRegion(long bitmap, long chunk, Rect location)

public voidsetPaint(Paint p)
Sets the paint to use when drawing the NinePatch.

param
p The paint that will be used to draw this NinePatch.
see
#getPaint()
see
#draw(Canvas, Rect)
see
#draw(Canvas, RectF)

        mPaint = p;
    
private static native longvalidateNinePatchChunk(long bitmap, byte[] chunk)
Validates the 9-patch chunk and throws an exception if the chunk is invalid. If validation is successful, this method returns a native Res_png_9patch* object used by the renderers.