Search in sources :

Example 1 with NinePatchChunk

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

the class BitmapFactory_Delegate method nativeDecodeStream.

@LayoutlibDelegate
static /*package*/
Bitmap nativeDecodeStream(InputStream is, byte[] storage, Rect padding, Options opts, boolean applyScale, float scale) {
    Bitmap bm = null;
    //TODO support rescaling
    Density density = Density.MEDIUM;
    if (opts != null) {
        density = Density.getEnum(opts.inDensity);
    }
    try {
        if (is instanceof NinePatchInputStream) {
            NinePatchInputStream npis = (NinePatchInputStream) is;
            npis.disableFakeMarkSupport();
            // load the bitmap as a nine patch
            com.android.ninepatch.NinePatch ninePatch = com.android.ninepatch.NinePatch.load(npis, true, /*is9Patch*/
            false);
            // get the bitmap and chunk objects.
            bm = Bitmap_Delegate.createBitmap(ninePatch.getImage(), true, /*isMutable*/
            density);
            NinePatchChunk chunk = ninePatch.getChunk();
            // put the chunk in the bitmap
            bm.setNinePatchChunk(NinePatch_Delegate.serialize(chunk));
            // read the padding
            int[] paddingarray = chunk.getPadding();
            padding.left = paddingarray[0];
            padding.top = paddingarray[1];
            padding.right = paddingarray[2];
            padding.bottom = paddingarray[3];
        } else {
            // load the bitmap directly.
            bm = Bitmap_Delegate.createBitmap(is, true, density);
        }
    } catch (IOException e) {
        Bridge.getLog().error(null, "Failed to load image", e, null);
    }
    return bm;
}
Also used : NinePatchInputStream(android.content.res.BridgeResources.NinePatchInputStream) NinePatchChunk(com.android.ninepatch.NinePatchChunk) IOException(java.io.IOException) Density(com.android.resources.Density) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Example 2 with NinePatchChunk

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

the class NinePatch_Delegate method draw.

// ---- Private Helper methods ----
private static void draw(int canvas_instance, final int left, final int top, final int right, final int bottom, int bitmap_instance, byte[] c, int paint_instance_or_null, final int destDensity, final int srcDensity) {
    // get the delegate from the native int.
    final Bitmap_Delegate bitmap_delegate = Bitmap_Delegate.getDelegate(bitmap_instance);
    if (bitmap_delegate == null) {
        return;
    }
    if (c == null) {
        // not a 9-patch?
        BufferedImage image = bitmap_delegate.getImage();
        Canvas_Delegate.native_drawBitmap(canvas_instance, bitmap_instance, new Rect(0, 0, image.getWidth(), image.getHeight()), new Rect(left, top, right, bottom), paint_instance_or_null, destDensity, srcDensity);
        return;
    }
    final NinePatchChunk chunkObject = getChunk(c);
    assert chunkObject != null;
    if (chunkObject == null) {
        return;
    }
    Canvas_Delegate canvas_delegate = Canvas_Delegate.getDelegate(canvas_instance);
    if (canvas_delegate == null) {
        return;
    }
    // this one can be null
    Paint_Delegate paint_delegate = Paint_Delegate.getDelegate(paint_instance_or_null);
    canvas_delegate.getSnapshot().draw(new GcSnapshot.Drawable() {

        @Override
        public void draw(Graphics2D graphics, Paint_Delegate paint) {
            chunkObject.draw(bitmap_delegate.getImage(), graphics, left, top, right - left, bottom - top, destDensity, srcDensity);
        }
    }, paint_delegate, true, /*compositeOnly*/
    false);
}
Also used : GcSnapshot(com.android.layoutlib.bridge.impl.GcSnapshot) NinePatchChunk(com.android.ninepatch.NinePatchChunk) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Example 3 with NinePatchChunk

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

the class NinePatch_Delegate method serialize.

// ---- Public Helper methods ----
/**
     * Serializes the given chunk.
     *
     * @return the serialized data for the chunk.
     */
public static byte[] serialize(NinePatchChunk chunk) {
    // serialize the chunk to get a byte[]
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = null;
    try {
        oos = new ObjectOutputStream(baos);
        oos.writeObject(chunk);
    } catch (IOException e) {
        Bridge.getLog().error(null, "Failed to serialize NinePatchChunk.", e, null);
        return null;
    } finally {
        if (oos != null) {
            try {
                oos.close();
            } catch (IOException e) {
            }
        }
    }
    // get the array and add it to the cache
    byte[] array = baos.toByteArray();
    sChunkCache.put(array, new SoftReference<NinePatchChunk>(chunk));
    return array;
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) NinePatchChunk(com.android.ninepatch.NinePatchChunk) ObjectOutputStream(java.io.ObjectOutputStream)

Example 4 with NinePatchChunk

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

the class NinePatch_Delegate method getChunk.

/**
     * Returns a {@link NinePatchChunk} object for the given serialized representation.
     *
     * If the chunk is present in the cache then the object from the cache is returned, otherwise
     * the array is deserialized into a {@link NinePatchChunk} object.
     *
     * @param array the serialized representation of the chunk.
     * @return the NinePatchChunk or null if deserialization failed.
     */
public static NinePatchChunk getChunk(byte[] array) {
    SoftReference<NinePatchChunk> chunkRef = sChunkCache.get(array);
    NinePatchChunk chunk = chunkRef.get();
    if (chunk == null) {
        ByteArrayInputStream bais = new ByteArrayInputStream(array);
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(bais);
            chunk = (NinePatchChunk) ois.readObject();
            // put back the chunk in the cache
            if (chunk != null) {
                sChunkCache.put(array, new SoftReference<NinePatchChunk>(chunk));
            }
        } catch (IOException e) {
            Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Failed to deserialize NinePatchChunk content.", e, null);
            return null;
        } catch (ClassNotFoundException e) {
            Bridge.getLog().error(LayoutLog.TAG_BROKEN, "Failed to deserialize NinePatchChunk class.", e, null);
            return null;
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                }
            }
        }
    }
    return chunk;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) NinePatchChunk(com.android.ninepatch.NinePatchChunk) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 5 with NinePatchChunk

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

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