Search in sources :

Example 26 with NinePatchChunk

use of com.android.ninepatch.NinePatchChunk in project android_frameworks_base by crdroidandroid.

the class ResourceHelper method getNinePatchDrawable.

private static Drawable getNinePatchDrawable(InputStream inputStream, Density density, boolean isFramework, String cacheKey, BridgeContext context) throws IOException {
    // see if we still have both the chunk and the bitmap in the caches
    NinePatchChunk chunk = Bridge.getCached9Patch(cacheKey, isFramework ? null : context.getProjectKey());
    Bitmap bitmap = Bridge.getCachedBitmap(cacheKey, isFramework ? null : context.getProjectKey());
    // if either chunk or bitmap is null, then we reload the 9-patch file.
    if (chunk == null || bitmap == null) {
        try {
            NinePatch ninePatch = NinePatch.load(inputStream, true, /*is9Patch*/
            false);
            if (ninePatch != null) {
                if (chunk == null) {
                    chunk = ninePatch.getChunk();
                    Bridge.setCached9Patch(cacheKey, chunk, isFramework ? null : context.getProjectKey());
                }
                if (bitmap == null) {
                    bitmap = Bitmap_Delegate.createBitmap(ninePatch.getImage(), false, /*isMutable*/
                    density);
                    Bridge.setCachedBitmap(cacheKey, bitmap, isFramework ? null : context.getProjectKey());
                }
            }
        } catch (MalformedURLException e) {
        // URL is wrong, we'll return null below
        }
    }
    if (chunk != null && bitmap != null) {
        int[] padding = chunk.getPadding();
        Rect paddingRect = new Rect(padding[0], padding[1], padding[2], padding[3]);
        return new NinePatchDrawable(context.getResources(), bitmap, NinePatch_Delegate.serialize(chunk), paddingRect, null);
    }
    return null;
}
Also used : Bitmap(android.graphics.Bitmap) MalformedURLException(java.net.MalformedURLException) Rect(android.graphics.Rect) NinePatch(com.android.ninepatch.NinePatch) NinePatchChunk(com.android.ninepatch.NinePatchChunk) NinePatchDrawable(android.graphics.drawable.NinePatchDrawable)

Aggregations

NinePatchChunk (com.android.ninepatch.NinePatchChunk)26 IOException (java.io.IOException)15 LayoutlibDelegate (com.android.tools.layoutlib.annotations.LayoutlibDelegate)9 Bitmap (android.graphics.Bitmap)6 Rect (android.graphics.Rect)6 NinePatchDrawable (android.graphics.drawable.NinePatchDrawable)6 NinePatch (com.android.ninepatch.NinePatch)6 MalformedURLException (java.net.MalformedURLException)6 GcSnapshot (com.android.layoutlib.bridge.impl.GcSnapshot)5 Density (com.android.resources.Density)5 Graphics2D (java.awt.Graphics2D)5 BufferedImage (java.awt.image.BufferedImage)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 ObjectInputStream (java.io.ObjectInputStream)5 ObjectOutputStream (java.io.ObjectOutputStream)5 BitmapCreateFlags (android.graphics.Bitmap_Delegate.BitmapCreateFlags)4 NinePatchInputStream (com.android.layoutlib.bridge.util.NinePatchInputStream)4 NinePatchInputStream (android.content.res.BridgeResources.NinePatchInputStream)1