Merge "Add isPremultiplied option to Bitmap_Delegate in LayoutLib" into klp-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
a712754011
@ -24,10 +24,13 @@ import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
|
|||||||
|
|
||||||
import android.content.res.BridgeResources.NinePatchInputStream;
|
import android.content.res.BridgeResources.NinePatchInputStream;
|
||||||
import android.graphics.BitmapFactory.Options;
|
import android.graphics.BitmapFactory.Options;
|
||||||
|
import android.graphics.Bitmap_Delegate.BitmapCreateFlags;
|
||||||
|
|
||||||
import java.io.FileDescriptor;
|
import java.io.FileDescriptor;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.util.EnumSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delegate implementing the native methods of android.graphics.BitmapFactory
|
* Delegate implementing the native methods of android.graphics.BitmapFactory
|
||||||
@ -98,8 +101,12 @@ import java.io.InputStream;
|
|||||||
//TODO support rescaling
|
//TODO support rescaling
|
||||||
|
|
||||||
Density density = Density.MEDIUM;
|
Density density = Density.MEDIUM;
|
||||||
|
Set<BitmapCreateFlags> bitmapCreateFlags = EnumSet.of(BitmapCreateFlags.MUTABLE);
|
||||||
if (opts != null) {
|
if (opts != null) {
|
||||||
density = Density.getEnum(opts.inDensity);
|
density = Density.getEnum(opts.inDensity);
|
||||||
|
if (opts.inPremultiplied) {
|
||||||
|
bitmapCreateFlags.add(BitmapCreateFlags.PREMULTIPLIED);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -112,7 +119,7 @@ import java.io.InputStream;
|
|||||||
npis, true /*is9Patch*/, false /*convert*/);
|
npis, true /*is9Patch*/, false /*convert*/);
|
||||||
|
|
||||||
// get the bitmap and chunk objects.
|
// get the bitmap and chunk objects.
|
||||||
bm = Bitmap_Delegate.createBitmap(ninePatch.getImage(), true /*isMutable*/,
|
bm = Bitmap_Delegate.createBitmap(ninePatch.getImage(), bitmapCreateFlags,
|
||||||
density);
|
density);
|
||||||
NinePatchChunk chunk = ninePatch.getChunk();
|
NinePatchChunk chunk = ninePatch.getChunk();
|
||||||
|
|
||||||
@ -127,7 +134,7 @@ import java.io.InputStream;
|
|||||||
padding.bottom = paddingarray[3];
|
padding.bottom = paddingarray[3];
|
||||||
} else {
|
} else {
|
||||||
// load the bitmap directly.
|
// load the bitmap directly.
|
||||||
bm = Bitmap_Delegate.createBitmap(is, true, density);
|
bm = Bitmap_Delegate.createBitmap(is, bitmapCreateFlags, density);
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
Bridge.getLog().error(null,"Failed to load image" , e, null);
|
Bridge.getLog().error(null,"Failed to load image" , e, null);
|
||||||
|
@ -33,6 +33,8 @@ import java.io.InputStream;
|
|||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.Buffer;
|
import java.nio.Buffer;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.EnumSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
@ -51,6 +53,10 @@ import javax.imageio.ImageIO;
|
|||||||
*/
|
*/
|
||||||
public final class Bitmap_Delegate {
|
public final class Bitmap_Delegate {
|
||||||
|
|
||||||
|
public enum BitmapCreateFlags {
|
||||||
|
PREMULTIPLIED, MUTABLE
|
||||||
|
}
|
||||||
|
|
||||||
// ---- delegate manager ----
|
// ---- delegate manager ----
|
||||||
private static final DelegateManager<Bitmap_Delegate> sManager =
|
private static final DelegateManager<Bitmap_Delegate> sManager =
|
||||||
new DelegateManager<Bitmap_Delegate>(Bitmap_Delegate.class);
|
new DelegateManager<Bitmap_Delegate>(Bitmap_Delegate.class);
|
||||||
@ -93,10 +99,25 @@ public final class Bitmap_Delegate {
|
|||||||
*/
|
*/
|
||||||
public static Bitmap createBitmap(File input, boolean isMutable, Density density)
|
public static Bitmap createBitmap(File input, boolean isMutable, Density density)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
return createBitmap(input, getPremultipliedBitmapCreateFlags(isMutable), density);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates and returns a {@link Bitmap} initialized with the given file content.
|
||||||
|
*
|
||||||
|
* @param input the file from which to read the bitmap content
|
||||||
|
* @param density the density associated with the bitmap
|
||||||
|
*
|
||||||
|
* @see Bitmap#isPremultiplied()
|
||||||
|
* @see Bitmap#isMutable()
|
||||||
|
* @see Bitmap#getDensity()
|
||||||
|
*/
|
||||||
|
public static Bitmap createBitmap(File input, Set<BitmapCreateFlags> createFlags,
|
||||||
|
Density density) throws IOException {
|
||||||
// create a delegate with the content of the file.
|
// create a delegate with the content of the file.
|
||||||
Bitmap_Delegate delegate = new Bitmap_Delegate(ImageIO.read(input), Config.ARGB_8888);
|
Bitmap_Delegate delegate = new Bitmap_Delegate(ImageIO.read(input), Config.ARGB_8888);
|
||||||
|
|
||||||
return createBitmap(delegate, isMutable, density.getDpiValue());
|
return createBitmap(delegate, createFlags, density.getDpiValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -111,10 +132,26 @@ public final class Bitmap_Delegate {
|
|||||||
*/
|
*/
|
||||||
public static Bitmap createBitmap(InputStream input, boolean isMutable, Density density)
|
public static Bitmap createBitmap(InputStream input, boolean isMutable, Density density)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
|
return createBitmap(input, getPremultipliedBitmapCreateFlags(isMutable), density);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates and returns a {@link Bitmap} initialized with the given stream content.
|
||||||
|
*
|
||||||
|
* @param input the stream from which to read the bitmap content
|
||||||
|
* @param createFlags
|
||||||
|
* @param density the density associated with the bitmap
|
||||||
|
*
|
||||||
|
* @see Bitmap#isPremultiplied()
|
||||||
|
* @see Bitmap#isMutable()
|
||||||
|
* @see Bitmap#getDensity()
|
||||||
|
*/
|
||||||
|
public static Bitmap createBitmap(InputStream input, Set<BitmapCreateFlags> createFlags,
|
||||||
|
Density density) throws IOException {
|
||||||
// create a delegate with the content of the stream.
|
// create a delegate with the content of the stream.
|
||||||
Bitmap_Delegate delegate = new Bitmap_Delegate(ImageIO.read(input), Config.ARGB_8888);
|
Bitmap_Delegate delegate = new Bitmap_Delegate(ImageIO.read(input), Config.ARGB_8888);
|
||||||
|
|
||||||
return createBitmap(delegate, isMutable, density.getDpiValue());
|
return createBitmap(delegate, createFlags, density.getDpiValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -129,10 +166,26 @@ public final class Bitmap_Delegate {
|
|||||||
*/
|
*/
|
||||||
public static Bitmap createBitmap(BufferedImage image, boolean isMutable,
|
public static Bitmap createBitmap(BufferedImage image, boolean isMutable,
|
||||||
Density density) throws IOException {
|
Density density) throws IOException {
|
||||||
|
return createBitmap(image, getPremultipliedBitmapCreateFlags(isMutable), density);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates and returns a {@link Bitmap} initialized with the given {@link BufferedImage}
|
||||||
|
*
|
||||||
|
* @param image the bitmap content
|
||||||
|
* @param createFlags
|
||||||
|
* @param density the density associated with the bitmap
|
||||||
|
*
|
||||||
|
* @see Bitmap#isPremultiplied()
|
||||||
|
* @see Bitmap#isMutable()
|
||||||
|
* @see Bitmap#getDensity()
|
||||||
|
*/
|
||||||
|
public static Bitmap createBitmap(BufferedImage image, Set<BitmapCreateFlags> createFlags,
|
||||||
|
Density density) throws IOException {
|
||||||
// create a delegate with the given image.
|
// create a delegate with the given image.
|
||||||
Bitmap_Delegate delegate = new Bitmap_Delegate(image, Config.ARGB_8888);
|
Bitmap_Delegate delegate = new Bitmap_Delegate(image, Config.ARGB_8888);
|
||||||
|
|
||||||
return createBitmap(delegate, isMutable, density.getDpiValue());
|
return createBitmap(delegate, createFlags, density.getDpiValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -203,7 +256,7 @@ public final class Bitmap_Delegate {
|
|||||||
|
|
||||||
@LayoutlibDelegate
|
@LayoutlibDelegate
|
||||||
/*package*/ static Bitmap nativeCreate(int[] colors, int offset, int stride, int width,
|
/*package*/ static Bitmap nativeCreate(int[] colors, int offset, int stride, int width,
|
||||||
int height, int nativeConfig, boolean mutable) {
|
int height, int nativeConfig, boolean isMutable) {
|
||||||
int imageType = getBufferedImageType(nativeConfig);
|
int imageType = getBufferedImageType(nativeConfig);
|
||||||
|
|
||||||
// create the image
|
// create the image
|
||||||
@ -216,7 +269,8 @@ public final class Bitmap_Delegate {
|
|||||||
// create a delegate with the content of the stream.
|
// create a delegate with the content of the stream.
|
||||||
Bitmap_Delegate delegate = new Bitmap_Delegate(image, Config.nativeToConfig(nativeConfig));
|
Bitmap_Delegate delegate = new Bitmap_Delegate(image, Config.nativeToConfig(nativeConfig));
|
||||||
|
|
||||||
return createBitmap(delegate, mutable, Bitmap.getDefaultDensity());
|
return createBitmap(delegate, getPremultipliedBitmapCreateFlags(isMutable),
|
||||||
|
Bitmap.getDefaultDensity());
|
||||||
}
|
}
|
||||||
|
|
||||||
@LayoutlibDelegate
|
@LayoutlibDelegate
|
||||||
@ -244,7 +298,8 @@ public final class Bitmap_Delegate {
|
|||||||
// create a delegate with the content of the stream.
|
// create a delegate with the content of the stream.
|
||||||
Bitmap_Delegate delegate = new Bitmap_Delegate(image, Config.nativeToConfig(nativeConfig));
|
Bitmap_Delegate delegate = new Bitmap_Delegate(image, Config.nativeToConfig(nativeConfig));
|
||||||
|
|
||||||
return createBitmap(delegate, isMutable, Bitmap.getDefaultDensity());
|
return createBitmap(delegate, getPremultipliedBitmapCreateFlags(isMutable),
|
||||||
|
Bitmap.getDefaultDensity());
|
||||||
}
|
}
|
||||||
|
|
||||||
@LayoutlibDelegate
|
@LayoutlibDelegate
|
||||||
@ -464,7 +519,7 @@ public final class Bitmap_Delegate {
|
|||||||
Bitmap_Delegate delegate = new Bitmap_Delegate(image, Config.ALPHA_8);
|
Bitmap_Delegate delegate = new Bitmap_Delegate(image, Config.ALPHA_8);
|
||||||
|
|
||||||
// the density doesn't matter, it's set by the Java method.
|
// the density doesn't matter, it's set by the Java method.
|
||||||
return createBitmap(delegate, false /*isMutable*/,
|
return createBitmap(delegate, EnumSet.of(BitmapCreateFlags.MUTABLE),
|
||||||
Density.DEFAULT_DENSITY /*density*/);
|
Density.DEFAULT_DENSITY /*density*/);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -546,15 +601,27 @@ public final class Bitmap_Delegate {
|
|||||||
mConfig = config;
|
mConfig = config;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Bitmap createBitmap(Bitmap_Delegate delegate, boolean isMutable, int density) {
|
private static Bitmap createBitmap(Bitmap_Delegate delegate,
|
||||||
|
Set<BitmapCreateFlags> createFlags, int density) {
|
||||||
// get its native_int
|
// get its native_int
|
||||||
int nativeInt = sManager.addNewDelegate(delegate);
|
int nativeInt = sManager.addNewDelegate(delegate);
|
||||||
|
|
||||||
|
int width = delegate.mImage.getWidth();
|
||||||
|
int height = delegate.mImage.getHeight();
|
||||||
|
boolean isMutable = createFlags.contains(BitmapCreateFlags.MUTABLE);
|
||||||
|
boolean isPremultiplied = createFlags.contains(BitmapCreateFlags.PREMULTIPLIED);
|
||||||
|
|
||||||
// and create/return a new Bitmap with it
|
// and create/return a new Bitmap with it
|
||||||
// TODO: pass correct width, height, isPremultiplied
|
return new Bitmap(nativeInt, null /* buffer */, width, height, density, isMutable,
|
||||||
return new Bitmap(nativeInt, null /* buffer */, -1 /* width */, -1 /* height */, density,
|
isPremultiplied, null /*ninePatchChunk*/, null /* layoutBounds */);
|
||||||
isMutable, true /* isPremultiplied */,
|
}
|
||||||
null /*ninePatchChunk*/, null /* layoutBounds */);
|
|
||||||
|
private static Set<BitmapCreateFlags> getPremultipliedBitmapCreateFlags(boolean isMutable) {
|
||||||
|
Set<BitmapCreateFlags> createFlags = EnumSet.of(BitmapCreateFlags.PREMULTIPLIED);
|
||||||
|
if (isMutable) {
|
||||||
|
createFlags.add(BitmapCreateFlags.MUTABLE);
|
||||||
|
}
|
||||||
|
return createFlags;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user