diff options
author | Romain Guy <romainguy@google.com> | 2013-06-07 12:17:11 -0700 |
---|---|---|
committer | Romain Guy <romainguy@google.com> | 2013-06-07 12:17:11 -0700 |
commit | f3187b7df158d2de36955ddcc666ba4b8544a2ce (patch) | |
tree | 2fb08b77de183ae78bdd5814f4252c6ab25fedad /graphics/java/android/graphics/NinePatch.java | |
parent | e50848bfba085e6836a5526dfa64261d1ee46e64 (diff) | |
download | frameworks_base-f3187b7df158d2de36955ddcc666ba4b8544a2ce.zip frameworks_base-f3187b7df158d2de36955ddcc666ba4b8544a2ce.tar.gz frameworks_base-f3187b7df158d2de36955ddcc666ba4b8544a2ce.tar.bz2 |
Remove unnecessary Rect, better reuse of NinePatch objects
Cloning drawables (which happens a lot) was creating copies of NinePatch
objects, which would cause the hardware renderer to generate more meshes
than necessary. Also avoid keeping a String we don't need (it was interned
but still.) Last but not least, remove 1 RectF per NinePatch in the system.
Change-Id: If4dbfa0c30892c9b00d68875e334fd5c2bde8b94
Diffstat (limited to 'graphics/java/android/graphics/NinePatch.java')
-rw-r--r-- | graphics/java/android/graphics/NinePatch.java | 37 |
1 files changed, 22 insertions, 15 deletions
diff --git a/graphics/java/android/graphics/NinePatch.java b/graphics/java/android/graphics/NinePatch.java index 81e9d84..932e474 100644 --- a/graphics/java/android/graphics/NinePatch.java +++ b/graphics/java/android/graphics/NinePatch.java @@ -42,8 +42,18 @@ public class NinePatch { public final byte[] mChunk; private Paint mPaint; private String mSrcName; // Useful for debugging - private final RectF mRect = new RectF(); - + + /** + * 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. + */ + public NinePatch(Bitmap bitmap, byte[] chunk) { + this(bitmap, chunk, null); + } + /** * Create a drawable projection from a bitmap to nine patches. * @@ -90,13 +100,13 @@ public class NinePatch { * @param location Where to draw the bitmap. */ public void draw(Canvas canvas, RectF location) { - if (!canvas.isHardwareAccelerated()) { + if (canvas.isHardwareAccelerated()) { + canvas.drawPatch(this, location, mPaint); + } else { nativeDraw(canvas.mNativeCanvas, location, mBitmap.ni(), mChunk, mPaint != null ? mPaint.mNativePaint : 0, canvas.mDensity, mBitmap.mDensity); - } else { - canvas.drawPatch(this, location, mPaint); } } @@ -107,14 +117,13 @@ public class NinePatch { * @param location Where to draw the bitmap. */ public void draw(Canvas canvas, Rect location) { - if (!canvas.isHardwareAccelerated()) { + if (canvas.isHardwareAccelerated()) { + canvas.drawPatch(this, location, mPaint); + } else { nativeDraw(canvas.mNativeCanvas, location, mBitmap.ni(), mChunk, mPaint != null ? mPaint.mNativePaint : 0, canvas.mDensity, mBitmap.mDensity); - } else { - mRect.set(location); - canvas.drawPatch(this, mRect, mPaint); } } @@ -126,13 +135,12 @@ public class NinePatch { * @param paint The Paint to draw through. */ public void draw(Canvas canvas, Rect location, Paint paint) { - if (!canvas.isHardwareAccelerated()) { + if (canvas.isHardwareAccelerated()) { + canvas.drawPatch(this, location, paint); + } else { nativeDraw(canvas.mNativeCanvas, location, mBitmap.ni(), mChunk, paint != null ? paint.mNativePaint : 0, canvas.mDensity, mBitmap.mDensity); - } else { - mRect.set(location); - canvas.drawPatch(this, mRect, paint); } } @@ -170,6 +178,5 @@ public class NinePatch { private static native void nativeDraw(int canvas_instance, Rect loc, int bitmap_instance, byte[] c, int paint_instance_or_null, int destDensity, int srcDensity); - private static native int nativeGetTransparentRegion( - int bitmap, byte[] chunk, Rect location); + private static native int nativeGetTransparentRegion(int bitmap, byte[] chunk, Rect location); } |