Search in sources :

Example 21 with NinePatchChunk

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

the class BitmapFactory_Delegate method nativeDecodeStream.

// ------ Native Delegates ------
@LayoutlibDelegate
static /*package*/
Bitmap nativeDecodeStream(InputStream is, byte[] storage, @Nullable Rect padding, @Nullable Options opts) {
    Bitmap bm = null;
    Density density = Density.MEDIUM;
    Set<BitmapCreateFlags> bitmapCreateFlags = EnumSet.of(BitmapCreateFlags.MUTABLE);
    if (opts != null) {
        density = Density.getEnum(opts.inDensity);
        if (opts.inPremultiplied) {
            bitmapCreateFlags.add(BitmapCreateFlags.PREMULTIPLIED);
        }
        opts.inScaled = false;
    }
    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(), bitmapCreateFlags, density);
            NinePatchChunk chunk = ninePatch.getChunk();
            // put the chunk in the bitmap
            bm.setNinePatchChunk(NinePatch_Delegate.serialize(chunk));
            if (padding != null) {
                // 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, bitmapCreateFlags, density);
        }
    } catch (IOException e) {
        Bridge.getLog().error(null, "Failed to load image", e, null);
    }
    return bm;
}
Also used : NinePatchInputStream(com.android.layoutlib.bridge.util.NinePatchInputStream) NinePatchChunk(com.android.ninepatch.NinePatchChunk) IOException(java.io.IOException) Density(com.android.resources.Density) BitmapCreateFlags(android.graphics.Bitmap_Delegate.BitmapCreateFlags) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Example 22 with NinePatchChunk

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

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 ignored) {
            }
        }
    }
    // 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 23 with NinePatchChunk

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

the class BitmapFactory_Delegate method nativeDecodeStream.

// ------ Native Delegates ------
@LayoutlibDelegate
static /*package*/
Bitmap nativeDecodeStream(InputStream is, byte[] storage, @Nullable Rect padding, @Nullable Options opts) {
    Bitmap bm = null;
    Density density = Density.MEDIUM;
    Set<BitmapCreateFlags> bitmapCreateFlags = EnumSet.of(BitmapCreateFlags.MUTABLE);
    if (opts != null) {
        density = Density.getEnum(opts.inDensity);
        if (opts.inPremultiplied) {
            bitmapCreateFlags.add(BitmapCreateFlags.PREMULTIPLIED);
        }
        opts.inScaled = false;
    }
    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(), bitmapCreateFlags, density);
            NinePatchChunk chunk = ninePatch.getChunk();
            // put the chunk in the bitmap
            bm.setNinePatchChunk(NinePatch_Delegate.serialize(chunk));
            if (padding != null) {
                // 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, bitmapCreateFlags, density);
        }
    } catch (IOException e) {
        Bridge.getLog().error(null, "Failed to load image", e, null);
    }
    return bm;
}
Also used : NinePatchInputStream(com.android.layoutlib.bridge.util.NinePatchInputStream) NinePatchChunk(com.android.ninepatch.NinePatchChunk) IOException(java.io.IOException) Density(com.android.resources.Density) BitmapCreateFlags(android.graphics.Bitmap_Delegate.BitmapCreateFlags) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Example 24 with NinePatchChunk

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

the class Canvas_Delegate method native_drawNinePatch.

@LayoutlibDelegate
public static void native_drawNinePatch(Canvas thisCanvas, long nativeCanvas, long nativeBitmap, long ninePatch, final float dstLeft, final float dstTop, final float dstRight, final float dstBottom, long nativePaintOrZero, final int screenDensity, final int bitmapDensity) {
    // get the delegate from the native int.
    final Bitmap_Delegate bitmapDelegate = Bitmap_Delegate.getDelegate(nativeBitmap);
    if (bitmapDelegate == null) {
        return;
    }
    byte[] c = NinePatch_Delegate.getChunk(ninePatch);
    if (c == null) {
        // not a 9-patch?
        BufferedImage image = bitmapDelegate.getImage();
        drawBitmap(nativeCanvas, bitmapDelegate, nativePaintOrZero, 0, 0, image.getWidth(), image.getHeight(), (int) dstLeft, (int) dstTop, (int) dstRight, (int) dstBottom);
        return;
    }
    final NinePatchChunk chunkObject = NinePatch_Delegate.getChunk(c);
    assert chunkObject != null;
    if (chunkObject == null) {
        return;
    }
    Canvas_Delegate canvasDelegate = Canvas_Delegate.getDelegate(nativeCanvas);
    if (canvasDelegate == null) {
        return;
    }
    // this one can be null
    Paint_Delegate paintDelegate = Paint_Delegate.getDelegate(nativePaintOrZero);
    canvasDelegate.getSnapshot().draw(new GcSnapshot.Drawable() {

        @Override
        public void draw(Graphics2D graphics, Paint_Delegate paint) {
            chunkObject.draw(bitmapDelegate.getImage(), graphics, (int) dstLeft, (int) dstTop, (int) (dstRight - dstLeft), (int) (dstBottom - dstTop), screenDensity, bitmapDensity);
        }
    }, paintDelegate, true, false);
}
Also used : GcSnapshot(com.android.layoutlib.bridge.impl.GcSnapshot) NinePatchChunk(com.android.ninepatch.NinePatchChunk) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) LayoutlibDelegate(com.android.tools.layoutlib.annotations.LayoutlibDelegate)

Example 25 with NinePatchChunk

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

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 ignored) {
                }
            }
        }
    }
    return chunk;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) NinePatchChunk(com.android.ninepatch.NinePatchChunk) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

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