Add support for loading 9-patch from a stream.

This will be used by the tests.
Also added the eclipse project files.

Change-Id: I56443773d90594475fc3f14464d48e56d5a1bbb5
This commit is contained in:
Xavier Ducrohet
2009-10-06 12:10:42 -07:00
parent 67d9bcdf5c
commit db9d3e2baf
4 changed files with 93 additions and 30 deletions

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="bin"/>
</classpath>

17
tools/ninepatch/.project Normal file
View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ninepatch</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@@ -25,6 +25,7 @@ import java.awt.Graphics;
import java.awt.Transparency; import java.awt.Transparency;
import java.net.URL; import java.net.URL;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
public class GraphicsUtilities { public class GraphicsUtilities {
public static BufferedImage loadCompatibleImage(URL resource) throws IOException { public static BufferedImage loadCompatibleImage(URL resource) throws IOException {
@@ -32,6 +33,11 @@ public class GraphicsUtilities {
return toCompatibleImage(image); return toCompatibleImage(image);
} }
public static BufferedImage loadCompatibleImage(InputStream stream) throws IOException {
BufferedImage image = ImageIO.read(stream);
return toCompatibleImage(image);
}
public static BufferedImage createCompatibleImage(int width, int height) { public static BufferedImage createCompatibleImage(int width, int height) {
return getGraphicsConfiguration().createCompatibleImage(width, height); return getGraphicsConfiguration().createCompatibleImage(width, height);
} }

View File

@@ -21,6 +21,7 @@ import java.awt.Rectangle;
import java.awt.RenderingHints; import java.awt.RenderingHints;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
@@ -33,7 +34,7 @@ public class NinePatch {
public static final String EXTENSION_9PATCH = ".9.png"; public static final String EXTENSION_9PATCH = ".9.png";
private BufferedImage mImage; private BufferedImage mImage;
private int mMinWidth; private int mMinWidth;
private int mMinHeight; private int mMinHeight;
@@ -50,7 +51,7 @@ public class NinePatch {
private Pair<Integer> mHorizontalPadding; private Pair<Integer> mHorizontalPadding;
private Pair<Integer> mVerticalPadding; private Pair<Integer> mVerticalPadding;
private float mHorizontalPatchesSum; private float mHorizontalPatchesSum;
private float mVerticalPatchesSum; private float mVerticalPatchesSum;
@@ -58,12 +59,10 @@ public class NinePatch {
private int mRemainderVertical; private int mRemainderVertical;
private final URL mFileUrl;
/** /**
* Loads a 9 patch or regular bitmap. * Loads a 9 patch or regular bitmap.
* @param fileUrl the URL of the file to load. * @param fileUrl the URL of the file to load.
* @param convert if <code>true</code>, non 9-patch bitmpa will be converted into a 9 patch. * @param convert if <code>true</code>, non 9-patch bitmap will be converted into a 9 patch.
* If <code>false</code> and the bitmap is not a 9 patch, the method will return * If <code>false</code> and the bitmap is not a 9 patch, the method will return
* <code>null</code>. * <code>null</code>.
* @return a {@link NinePatch} or <code>null</code>. * @return a {@link NinePatch} or <code>null</code>.
@@ -77,9 +76,46 @@ public class NinePatch {
// really this shouldn't be happening since we're not creating the URL manually. // really this shouldn't be happening since we're not creating the URL manually.
return null; return null;
} }
boolean is9Patch = fileUrl.getPath().toLowerCase().endsWith(EXTENSION_9PATCH); boolean is9Patch = fileUrl.getPath().toLowerCase().endsWith(EXTENSION_9PATCH);
return load(image, is9Patch, convert);
}
/**
* Loads a 9 patch or regular bitmap.
* @param stream the {@link InputStream} of the file to load.
* @param is9Patch whether the file represents a 9-patch
* @param convert if <code>true</code>, non 9-patch bitmap will be converted into a 9 patch.
* If <code>false</code> and the bitmap is not a 9 patch, the method will return
* <code>null</code>.
* @return a {@link NinePatch} or <code>null</code>.
* @throws IOException
*/
public static NinePatch load(InputStream stream, boolean is9Patch, boolean convert)
throws IOException {
BufferedImage image = null;
try {
image = GraphicsUtilities.loadCompatibleImage(stream);
} catch (MalformedURLException e) {
// really this shouldn't be happening since we're not creating the URL manually.
return null;
}
return load(image, is9Patch, convert);
}
/**
* Loads a 9 patch or regular bitmap.
* @param image the source {@link BufferedImage}.
* @param is9Patch whether the file represents a 9-patch
* @param convert if <code>true</code>, non 9-patch bitmap will be converted into a 9 patch.
* If <code>false</code> and the bitmap is not a 9 patch, the method will return
* <code>null</code>.
* @return a {@link NinePatch} or <code>null</code>.
* @throws IOException
*/
public static NinePatch load(BufferedImage image, boolean is9Patch, boolean convert) {
if (is9Patch == false) { if (is9Patch == false) {
if (convert) { if (convert) {
image = convertTo9Patch(image); image = convertTo9Patch(image);
@@ -90,10 +126,9 @@ public class NinePatch {
ensure9Patch(image); ensure9Patch(image);
} }
return new NinePatch(image);
return new NinePatch(fileUrl, image);
} }
public int getWidth() { public int getWidth() {
return mImage.getWidth() - 2; return mImage.getWidth() - 2;
} }
@@ -101,9 +136,9 @@ public class NinePatch {
public int getHeight() { public int getHeight() {
return mImage.getHeight() - 2; return mImage.getHeight() - 2;
} }
/** /**
* *
* @param padding array of left, top, right, bottom padding * @param padding array of left, top, right, bottom padding
* @return * @return
*/ */
@@ -124,7 +159,7 @@ public class NinePatch {
Graphics2D g = (Graphics2D)graphics2D.create(); Graphics2D g = (Graphics2D)graphics2D.create();
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
RenderingHints.VALUE_INTERPOLATION_BILINEAR); RenderingHints.VALUE_INTERPOLATION_BILINEAR);
try { try {
if (mPatches.size() == 0) { if (mPatches.size() == 0) {
@@ -134,30 +169,30 @@ public class NinePatch {
g.translate(x, y); g.translate(x, y);
x = y = 0; x = y = 0;
computePatches(scaledWidth, scaledHeight); computePatches(scaledWidth, scaledHeight);
int fixedIndex = 0; int fixedIndex = 0;
int horizontalIndex = 0; int horizontalIndex = 0;
int verticalIndex = 0; int verticalIndex = 0;
int patchIndex = 0; int patchIndex = 0;
boolean hStretch; boolean hStretch;
boolean vStretch; boolean vStretch;
float vWeightSum = 1.0f; float vWeightSum = 1.0f;
float vRemainder = mRemainderVertical; float vRemainder = mRemainderVertical;
vStretch = mVerticalStartWithPatch; vStretch = mVerticalStartWithPatch;
while (y < scaledHeight - 1) { while (y < scaledHeight - 1) {
hStretch = mHorizontalStartWithPatch; hStretch = mHorizontalStartWithPatch;
int height = 0; int height = 0;
float vExtra = 0.0f; float vExtra = 0.0f;
float hWeightSum = 1.0f; float hWeightSum = 1.0f;
float hRemainder = mRemainderHorizontal; float hRemainder = mRemainderHorizontal;
while (x < scaledWidth - 1) { while (x < scaledWidth - 1) {
Rectangle r; Rectangle r;
if (!vStretch) { if (!vStretch) {
@@ -197,7 +232,7 @@ public class NinePatch {
r.x + r.width, r.y + r.height, null); r.x + r.width, r.y + r.height, null);
x += r.width; x += r.width;
} }
} }
hStretch = !hStretch; hStretch = !hStretch;
} }
@@ -209,12 +244,12 @@ public class NinePatch {
} }
vStretch = !vStretch; vStretch = !vStretch;
} }
} finally { } finally {
g.dispose(); g.dispose();
} }
} }
void computePatches(int scaledWidth, int scaledHeight) { void computePatches(int scaledWidth, int scaledHeight) {
boolean measuredWidth = false; boolean measuredWidth = false;
boolean endRow = true; boolean endRow = true;
@@ -283,14 +318,13 @@ public class NinePatch {
} }
} }
private NinePatch(URL fileUrl, BufferedImage image) { private NinePatch(BufferedImage image) {
mFileUrl = fileUrl;
mImage = image; mImage = image;
findPatches(); findPatches();
} }
private void findPatches() { private void findPatches() {
int width = mImage.getWidth(); int width = mImage.getWidth();
int height = mImage.getHeight(); int height = mImage.getHeight();
@@ -462,7 +496,7 @@ public class NinePatch {
return buffer; return buffer;
} }
static class Pair<E> { static class Pair<E> {
E mFirst; E mFirst;
E mSecond; E mSecond;