19066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/* 29066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Copyright (C) 2006 The Android Open Source Project 39066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 49066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License"); 59066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * you may not use this file except in compliance with the License. 69066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * You may obtain a copy of the License at 79066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 89066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * http://www.apache.org/licenses/LICENSE-2.0 99066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Unless required by applicable law or agreed to in writing, software 119066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS, 129066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 139066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * See the License for the specific language governing permissions and 149066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * limitations under the License. 159066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 169066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 179066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpackage android.graphics; 189066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 199066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project/** 20e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * The NinePatch class permits drawing a bitmap in nine or more sections. 21e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * Essentially, it allows the creation of custom graphics that will scale the 229066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * way that you define, when content added within the image exceeds the normal 239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * bounds of the graphic. For a thorough explanation of a NinePatch image, 249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * read the discussion in the 259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <a href="{@docRoot}guide/topics/graphics/2d-graphics.html#nine-patch">2D 269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Graphics</a> document. 279066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * <p> 289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * The <a href="{@docRoot}guide/developing/tools/draw9patch.html">Draw 9-Patch</a> 299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * tool offers an extremely handy way to create your NinePatch images, 309066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * using a WYSIWYG graphics editor. 319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * </p> 329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Projectpublic class NinePatch { 3447cd8e921db73e894f94ec4729ade90da50996f5Chris Craik /** 3547cd8e921db73e894f94ec4729ade90da50996f5Chris Craik * Struct of inset information attached to a 9 patch bitmap. 3647cd8e921db73e894f94ec4729ade90da50996f5Chris Craik * 3747cd8e921db73e894f94ec4729ade90da50996f5Chris Craik * Present on a 9 patch bitmap if it optical insets were manually included, 3847cd8e921db73e894f94ec4729ade90da50996f5Chris Craik * or if outline insets were automatically included by aapt. 3947cd8e921db73e894f94ec4729ade90da50996f5Chris Craik * 4047cd8e921db73e894f94ec4729ade90da50996f5Chris Craik * @hide 4147cd8e921db73e894f94ec4729ade90da50996f5Chris Craik */ 4247cd8e921db73e894f94ec4729ade90da50996f5Chris Craik public static class InsetStruct { 4347cd8e921db73e894f94ec4729ade90da50996f5Chris Craik @SuppressWarnings({"UnusedDeclaration"}) // called from JNI 4447cd8e921db73e894f94ec4729ade90da50996f5Chris Craik InsetStruct(int opticalLeft, int opticalTop, int opticalRight, int opticalBottom, 4547cd8e921db73e894f94ec4729ade90da50996f5Chris Craik int outlineLeft, int outlineTop, int outlineRight, int outlineBottom, 4677b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik float outlineRadius, int outlineAlpha, float decodeScale) { 4747cd8e921db73e894f94ec4729ade90da50996f5Chris Craik opticalRect = new Rect(opticalLeft, opticalTop, opticalRight, opticalBottom); 4847cd8e921db73e894f94ec4729ade90da50996f5Chris Craik outlineRect = new Rect(outlineLeft, outlineTop, outlineRight, outlineBottom); 4947cd8e921db73e894f94ec4729ade90da50996f5Chris Craik 5047cd8e921db73e894f94ec4729ade90da50996f5Chris Craik if (decodeScale != 1.0f) { 5147cd8e921db73e894f94ec4729ade90da50996f5Chris Craik // if bitmap was scaled when decoded, scale the insets from the metadata values 5247cd8e921db73e894f94ec4729ade90da50996f5Chris Craik opticalRect.scale(decodeScale); 5347cd8e921db73e894f94ec4729ade90da50996f5Chris Craik 5447cd8e921db73e894f94ec4729ade90da50996f5Chris Craik // round inward while scaling outline, as the outline should always be conservative 5547cd8e921db73e894f94ec4729ade90da50996f5Chris Craik outlineRect.scaleRoundIn(decodeScale); 5647cd8e921db73e894f94ec4729ade90da50996f5Chris Craik } 5747cd8e921db73e894f94ec4729ade90da50996f5Chris Craik this.outlineRadius = outlineRadius * decodeScale; 5877b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik this.outlineAlpha = outlineAlpha / 255.0f; 5947cd8e921db73e894f94ec4729ade90da50996f5Chris Craik } 6047cd8e921db73e894f94ec4729ade90da50996f5Chris Craik 6147cd8e921db73e894f94ec4729ade90da50996f5Chris Craik public final Rect opticalRect; 6247cd8e921db73e894f94ec4729ade90da50996f5Chris Craik public final Rect outlineRect; 6347cd8e921db73e894f94ec4729ade90da50996f5Chris Craik public final float outlineRadius; 6477b5cad3efedd20f2b7cc14d87ccce1b0261960aChris Craik public final float outlineAlpha; 6547cd8e921db73e894f94ec4729ade90da50996f5Chris Craik } 6647cd8e921db73e894f94ec4729ade90da50996f5Chris Craik 67deba785f122a47915756ffd991f5540d952cf937Romain Guy private final Bitmap mBitmap; 68e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy 693b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy /** 70e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * Used by native code. This pointer is an instance of Res_png_9patch*. 71e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * 723b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy * @hide 733b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy */ 7436bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat public final long mNativeChunk; 75e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy 76deba785f122a47915756ffd991f5540d952cf937Romain Guy private Paint mPaint; 77e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy private String mSrcName; 78f3187b7df158d2de36955ddcc666ba4b8544a2ceRomain Guy 79f3187b7df158d2de36955ddcc666ba4b8544a2ceRomain Guy /** 80f3187b7df158d2de36955ddcc666ba4b8544a2ceRomain Guy * Create a drawable projection from a bitmap to nine patches. 81f3187b7df158d2de36955ddcc666ba4b8544a2ceRomain Guy * 82e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * @param bitmap The bitmap describing the patches. 83e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * @param chunk The 9-patch data chunk describing how the underlying bitmap 84e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * is split apart and drawn. 85f3187b7df158d2de36955ddcc666ba4b8544a2ceRomain Guy */ 86f3187b7df158d2de36955ddcc666ba4b8544a2ceRomain Guy public NinePatch(Bitmap bitmap, byte[] chunk) { 87f3187b7df158d2de36955ddcc666ba4b8544a2ceRomain Guy this(bitmap, chunk, null); 88f3187b7df158d2de36955ddcc666ba4b8544a2ceRomain Guy } 89f3187b7df158d2de36955ddcc666ba4b8544a2ceRomain Guy 909066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 919066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * Create a drawable projection from a bitmap to nine patches. 929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 93e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * @param bitmap The bitmap describing the patches. 94e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * @param chunk The 9-patch data chunk describing how the underlying 95e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * bitmap is split apart and drawn. 96e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * @param srcName The name of the source for the bitmap. Might be null. 979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 989066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public NinePatch(Bitmap bitmap, byte[] chunk, String srcName) { 999066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mBitmap = bitmap; 1009066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mSrcName = srcName; 1017c103a36f60b690e3fe83c40210e1cb0c76bba43John Reck mNativeChunk = validateNinePatchChunk(chunk); 1029066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1039066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1049066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 1059066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * @hide 1069066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 1079066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public NinePatch(NinePatch patch) { 1089066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mBitmap = patch.mBitmap; 1099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mSrcName = patch.mSrcName; 11054285f2cbfb6e307d594ca264f7230b4e1e3cdcePhil Dubach if (patch.mPaint != null) { 11154285f2cbfb6e307d594ca264f7230b4e1e3cdcePhil Dubach mPaint = new Paint(patch.mPaint); 11254285f2cbfb6e307d594ca264f7230b4e1e3cdcePhil Dubach } 113e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy // No need to validate the 9patch chunk again, it was done by 114e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy // the instance we're copying from 115e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy mNativeChunk = patch.mNativeChunk; 116e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy } 117e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy 118e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy @Override 119e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy protected void finalize() throws Throwable { 120e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy try { 12101af516a8768cf3c544afb02283c9d8f1dba786cChris Craik if (mNativeChunk != 0) { 12201af516a8768cf3c544afb02283c9d8f1dba786cChris Craik // only attempt to destroy correctly initilized chunks 12301af516a8768cf3c544afb02283c9d8f1dba786cChris Craik nativeFinalize(mNativeChunk); 12401af516a8768cf3c544afb02283c9d8f1dba786cChris Craik } 125e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy } finally { 126e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy super.finalize(); 127e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy } 1289066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1299066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 130e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy /** 131e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * Returns the name of this NinePatch object if one was specified 132e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * when calling the constructor. 133e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy */ 134e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy public String getName() { 135e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy return mSrcName; 136e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy } 137e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy 138e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy /** 139e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * Returns the paint used to draw this NinePatch. The paint can be null. 140e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * 141e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * @see #setPaint(Paint) 142e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * @see #draw(Canvas, Rect) 143e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * @see #draw(Canvas, RectF) 144e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy */ 145e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy public Paint getPaint() { 146e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy return mPaint; 147e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy } 148e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy 149e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy /** 150e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * Sets the paint to use when drawing the NinePatch. 151e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * 152e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * @param p The paint that will be used to draw this NinePatch. 153e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * 154e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * @see #getPaint() 155e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * @see #draw(Canvas, Rect) 156e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * @see #draw(Canvas, RectF) 157e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy */ 1589066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public void setPaint(Paint p) { 1599066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project mPaint = p; 1609066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1613b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy 1623b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy /** 163e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * Returns the bitmap used to draw this NinePatch. 1643b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy */ 1653b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy public Bitmap getBitmap() { 1663b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy return mBitmap; 1673b748a44c6bd2ea05fe16839caf73dbe50bd7ae9Romain Guy } 1689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1699066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 170e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * Draws the NinePatch. This method will use the paint returned by {@link #getPaint()}. 1719066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 172e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * @param canvas A container for the current matrix and clip used to draw the NinePatch. 173e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * @param location Where to draw the NinePatch. 1749066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 1759066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public void draw(Canvas canvas, RectF location) { 176f296dca95f09be9832b5dcc79717986525d2b6cbRomain Guy canvas.drawPatch(this, location, mPaint); 1779066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 178f296dca95f09be9832b5dcc79717986525d2b6cbRomain Guy 1799066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 180e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * Draws the NinePatch. This method will use the paint returned by {@link #getPaint()}. 1819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 182e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * @param canvas A container for the current matrix and clip used to draw the NinePatch. 183e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * @param location Where to draw the NinePatch. 1849066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 1859066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public void draw(Canvas canvas, Rect location) { 186f296dca95f09be9832b5dcc79717986525d2b6cbRomain Guy canvas.drawPatch(this, location, mPaint); 1879066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 1889066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 1899066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project /** 190e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * Draws the NinePatch. This method will ignore the paint returned 191e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * by {@link #getPaint()} and use the specified paint instead. 1929066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project * 193e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * @param canvas A container for the current matrix and clip used to draw the NinePatch. 194e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * @param location Where to draw the NinePatch. 195e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * @param paint The Paint to draw through. 1969066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project */ 1979066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public void draw(Canvas canvas, Rect location, Paint paint) { 198f296dca95f09be9832b5dcc79717986525d2b6cbRomain Guy canvas.drawPatch(this, location, paint); 199f296dca95f09be9832b5dcc79717986525d2b6cbRomain Guy } 200f296dca95f09be9832b5dcc79717986525d2b6cbRomain Guy 201f296dca95f09be9832b5dcc79717986525d2b6cbRomain Guy void drawSoftware(Canvas canvas, RectF location, Paint paint) { 2027c103a36f60b690e3fe83c40210e1cb0c76bba43John Reck nativeDraw(canvas.getNativeCanvasWrapper(), location, mBitmap, mNativeChunk, 203dfba4d3d11bbf47dff45f94d61d4d97510b3034aDerek Sollenberger paint != null ? paint.getNativeInstance() : 0, canvas.mDensity, mBitmap.mDensity); 204f296dca95f09be9832b5dcc79717986525d2b6cbRomain Guy } 205f296dca95f09be9832b5dcc79717986525d2b6cbRomain Guy 206f296dca95f09be9832b5dcc79717986525d2b6cbRomain Guy void drawSoftware(Canvas canvas, Rect location, Paint paint) { 2077c103a36f60b690e3fe83c40210e1cb0c76bba43John Reck nativeDraw(canvas.getNativeCanvasWrapper(), location, mBitmap, mNativeChunk, 208dfba4d3d11bbf47dff45f94d61d4d97510b3034aDerek Sollenberger paint != null ? paint.getNativeInstance() : 0, canvas.mDensity, mBitmap.mDensity); 2099066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2109066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 21111ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn /** 21211ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn * Return the underlying bitmap's density, as per 21311ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn * {@link Bitmap#getDensity() Bitmap.getDensity()}. 21411ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn */ 21511ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn public int getDensity() { 21611ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn return mBitmap.mDensity; 21711ea33471e1a14a8594f0b2cd012d86340dd3bd8Dianne Hackborn } 218e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy 219e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy /** 220e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * Returns the intrinsic width, in pixels, of this NinePatch. This is equivalent 221e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * to querying the width of the underlying bitmap returned by {@link #getBitmap()}. 222e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy */ 2239066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public int getWidth() { 2249066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return mBitmap.getWidth(); 2259066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2269066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 227e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy /** 228e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * Returns the intrinsic height, in pixels, of this NinePatch. This is equivalent 229e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * to querying the height of the underlying bitmap returned by {@link #getBitmap()}. 230e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy */ 2319066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public int getHeight() { 2329066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return mBitmap.getHeight(); 2339066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2349066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 235e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy /** 236e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * Indicates whether this NinePatch contains transparent or translucent pixels. 237e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * This is equivalent to calling <code>getBitmap().hasAlpha()</code> on this 238e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * NinePatch. 239e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy */ 2409066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public final boolean hasAlpha() { 2419066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return mBitmap.hasAlpha(); 2429066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 2439066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 244e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy /** 245e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * Returns a {@link Region} representing the parts of the NinePatch that are 246e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * completely transparent. 247e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * 248e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * @param bounds The location and size of the NinePatch. 249e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * 250e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * @return null if the NinePatch has no transparent region to 251e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * report, else a {@link Region} holding the parts of the specified bounds 252e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * that are transparent. 253e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy */ 254e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy public final Region getTransparentRegion(Rect bounds) { 2557c103a36f60b690e3fe83c40210e1cb0c76bba43John Reck long r = nativeGetTransparentRegion(mBitmap, mNativeChunk, bounds); 2569066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project return r != 0 ? new Region(r) : null; 2579066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project } 258e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy 259e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy /** 260e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * Verifies that the specified byte array is a valid 9-patch data chunk. 261e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * 262e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * @param chunk A byte array representing a 9-patch data chunk. 263e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * 264e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * @return True if the specified byte array represents a 9-patch data chunk, 265e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * false otherwise. 266e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy */ 2679066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project public native static boolean isNinePatchChunk(byte[] chunk); 2689066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project 269e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy /** 270e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * Validates the 9-patch chunk and throws an exception if the chunk is invalid. 271e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * If validation is successful, this method returns a native Res_png_9patch* 272e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy * object used by the renderers. 273e3b0a0117a2ab4118f868a731b238fe8f2430276Romain Guy */ 2747c103a36f60b690e3fe83c40210e1cb0c76bba43John Reck private static native long validateNinePatchChunk(byte[] chunk); 27536bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat private static native void nativeFinalize(long chunk); 2767c103a36f60b690e3fe83c40210e1cb0c76bba43John Reck private static native void nativeDraw(long canvas_instance, RectF loc, Bitmap bitmap_instance, 27736bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat long c, long paint_instance_or_null, int destDensity, int srcDensity); 2787c103a36f60b690e3fe83c40210e1cb0c76bba43John Reck private static native void nativeDraw(long canvas_instance, Rect loc, Bitmap bitmap_instance, 27936bef0bf30d6bae48cf3837df351075ca4fce654Ashok Bhat long c, long paint_instance_or_null, int destDensity, int srcDensity); 2807c103a36f60b690e3fe83c40210e1cb0c76bba43John Reck private static native long nativeGetTransparentRegion(Bitmap bitmap, long chunk, Rect location); 2819066cfe9886ac131c34d59ed0e2d287b0e3c0087The Android Open Source Project} 282