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.net.URL;
import java.io.IOException;
import java.io.InputStream;
public class GraphicsUtilities {
public static BufferedImage loadCompatibleImage(URL resource) throws IOException {
@@ -32,6 +33,11 @@ public class GraphicsUtilities {
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) {
return getGraphicsConfiguration().createCompatibleImage(width, height);
}

View File

@@ -21,6 +21,7 @@ import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
@@ -58,12 +59,10 @@ public class NinePatch {
private int mRemainderVertical;
private final URL mFileUrl;
/**
* Loads a 9 patch or regular bitmap.
* @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
* <code>null</code>.
* @return a {@link NinePatch} or <code>null</code>.
@@ -80,6 +79,43 @@ public class NinePatch {
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 (convert) {
image = convertTo9Patch(image);
@@ -90,8 +126,7 @@ public class NinePatch {
ensure9Patch(image);
}
return new NinePatch(fileUrl, image);
return new NinePatch(image);
}
public int getWidth() {
@@ -284,8 +319,7 @@ public class NinePatch {
}
private NinePatch(URL fileUrl, BufferedImage image) {
mFileUrl = fileUrl;
private NinePatch(BufferedImage image) {
mImage = image;
findPatches();