NinePatch


public class NinePatch
extends Object

java.lang.Object
   ↳ android.graphics.NinePatch


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.

Summary

Public constructors

NinePatch(Bitmap bitmap, byte[] chunk)

Create a drawable projection from a bitmap to nine patches.

NinePatch(Bitmap bitmap, byte[] chunk, String srcName)

Create a drawable projection from a bitmap to nine patches.

Public methods

void draw(Canvas canvas, Rect location, Paint paint)

Draws the NinePatch.

void draw(Canvas canvas, Rect location)

Draws the NinePatch.

void draw(Canvas canvas, RectF location)

Draws the NinePatch.

Bitmap getBitmap()

Returns the bitmap used to draw this NinePatch.

int getDensity()

Return the underlying bitmap's density, as per Bitmap.getDensity().

int getHeight()

Returns the intrinsic height, in pixels, of this NinePatch.

String getName()

Returns the name of this NinePatch object if one was specified when calling the constructor.

Paint getPaint()

Returns the paint used to draw this NinePatch.

final Region getTransparentRegion(Rect bounds)

Returns a Region representing the parts of the NinePatch that are completely transparent.

int getWidth()

Returns the intrinsic width, in pixels, of this NinePatch.

final boolean hasAlpha()

Indicates whether this NinePatch contains transparent or translucent pixels.

static boolean isNinePatchChunk(byte[] chunk)

Verifies that the specified byte array is a valid 9-patch data chunk.

void setPaint(Paint p)

Sets the paint to use when drawing the NinePatch.

Protected methods

void finalize()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.

Inherited methods

Public constructors

NinePatch

Added in API level 19
public NinePatch (Bitmap bitmap, 
                byte[] chunk)

Create a drawable projection from a bitmap to nine patches.

Parameters
bitmap Bitmap: The bitmap describing the patches.

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

NinePatch

Added in API level 1
public NinePatch (Bitmap bitmap, 
                byte[] chunk, 
                String srcName)

Create a drawable projection from a bitmap to nine patches.

Parameters
bitmap Bitmap: The bitmap describing the patches.

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

srcName String: The name of the source for the bitmap. Might be null.

Public methods

draw

Added in API level 1
public void draw (Canvas canvas, 
                Rect location, 
                Paint paint)

Draws the NinePatch. This method will ignore the paint returned by getPaint() and use the specified paint instead.

Parameters
canvas Canvas: A container for the current matrix and clip used to draw the NinePatch.

location Rect: Where to draw the NinePatch.

paint Paint: The Paint to draw through.

draw

Added in API level 1
public void draw (Canvas canvas, 
                Rect location)

Draws the NinePatch. This method will use the paint returned by getPaint().

Parameters
canvas Canvas: A container for the current matrix and clip used to draw the NinePatch.

location Rect: Where to draw the NinePatch.

draw

Added in API level 1
public void draw (Canvas canvas, 
                RectF location)

Draws the NinePatch. This method will use the paint returned by getPaint().

Parameters
canvas Canvas: A container for the current matrix and clip used to draw the NinePatch.

location RectF: Where to draw the NinePatch.

getBitmap

Added in API level 19
public Bitmap getBitmap ()

Returns the bitmap used to draw this NinePatch.

Returns
Bitmap

getDensity

Added in API level 4
public int getDensity ()

Return the underlying bitmap's density, as per Bitmap.getDensity().

Returns
int

getHeight

Added in API level 1
public int getHeight ()

Returns the intrinsic height, in pixels, of this NinePatch. This is equivalent to querying the height of the underlying bitmap returned by getBitmap().

Returns
int

getName

Added in API level 19
public String getName ()

Returns the name of this NinePatch object if one was specified when calling the constructor.

Returns
String

getPaint

Added in API level 19
public Paint getPaint ()

Returns the paint used to draw this NinePatch. The paint can be null.

Returns
Paint

getTransparentRegion

Added in API level 1
public final Region getTransparentRegion (Rect bounds)

Returns a Region representing the parts of the NinePatch that are completely transparent.

Parameters
bounds Rect: The location and size of the NinePatch.

Returns
Region null if the NinePatch has no transparent region to report, else a Region holding the parts of the specified bounds that are transparent.

getWidth

Added in API level 1
public int getWidth ()

Returns the intrinsic width, in pixels, of this NinePatch. This is equivalent to querying the width of the underlying bitmap returned by getBitmap().

Returns
int

hasAlpha

Added in API level 1
public final boolean hasAlpha ()

Indicates whether this NinePatch contains transparent or translucent pixels. This is equivalent to calling getBitmap().hasAlpha() on this NinePatch.

Returns
boolean

isNinePatchChunk

Added in API level 1
public static boolean isNinePatchChunk (byte[] chunk)

Verifies that the specified byte array is a valid 9-patch data chunk.

Parameters
chunk byte: A byte array representing a 9-patch data chunk.

Returns
boolean True if the specified byte array represents a 9-patch data chunk, false otherwise.

setPaint

Added in API level 1
public void setPaint (Paint p)

Sets the paint to use when drawing the NinePatch.

Parameters
p Paint: The paint that will be used to draw this NinePatch.

Protected methods

finalize

Added in API level 1
protected void finalize ()

Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. A subclass overrides the finalize method to dispose of system resources or to perform other cleanup.

The general contract of finalize is that it is invoked if and when the Java virtual machine has determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, except as a result of an action taken by the finalization of some other object or class which is ready to be finalized. The finalize method may take any action, including making this object available again to other threads; the usual purpose of finalize, however, is to perform cleanup actions before the object is irrevocably discarded. For example, the finalize method for an object that represents an input/output connection might perform explicit I/O transactions to break the connection before the object is permanently discarded.

The finalize method of class Object performs no special action; it simply returns normally. Subclasses of Object may override this definition.

The Java programming language does not guarantee which thread will invoke the finalize method for any given object. It is guaranteed, however, that the thread that invokes finalize will not be holding any user-visible synchronization locks when finalize is invoked. If an uncaught exception is thrown by the finalize method, the exception is ignored and finalization of that object terminates.

After the finalize method has been invoked for an object, no further action is taken until the Java virtual machine has again determined that there is no longer any means by which this object can be accessed by any thread that has not yet died, including possible actions by other objects or classes which are ready to be finalized, at which point the object may be discarded.

The finalize method is never invoked more than once by a Java virtual machine for any given object.

Any exception thrown by the finalize method causes the finalization of this object to be halted, but is otherwise ignored.

Throws
Throwable