use of com.android.ninepatch.NinePatchChunk in project android_frameworks_base by ResurrectionRemix.
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;
}
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;
}
use of com.android.ninepatch.NinePatchChunk in project android_frameworks_base by crdroidandroid.
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;
}
use of com.android.ninepatch.NinePatchChunk in project android_frameworks_base by crdroidandroid.
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;
}
use of com.android.ninepatch.NinePatchChunk in project android_frameworks_base by crdroidandroid.
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);
}
Aggregations