diff options
Diffstat (limited to 'ninepatch')
-rw-r--r-- | ninepatch/.classpath | 6 | ||||
-rw-r--r-- | ninepatch/.gitignore | 1 | ||||
-rw-r--r-- | ninepatch/.project | 17 | ||||
-rw-r--r-- | ninepatch/Android.mk | 26 | ||||
-rw-r--r-- | ninepatch/NOTICE | 190 | ||||
-rw-r--r-- | ninepatch/src/com/android/ninepatch/GraphicsUtilities.java | 102 | ||||
-rw-r--r-- | ninepatch/src/com/android/ninepatch/NinePatch.java | 224 | ||||
-rw-r--r-- | ninepatch/src/com/android/ninepatch/NinePatchChunk.java | 494 | ||||
-rw-r--r-- | ninepatch/tests/.classpath | 8 | ||||
-rw-r--r-- | ninepatch/tests/.project | 17 | ||||
-rw-r--r-- | ninepatch/tests/Android.mk | 33 | ||||
-rw-r--r-- | ninepatch/tests/res/com/android/ninepatch/button.9.png | bin | 3750 -> 0 bytes | |||
-rw-r--r-- | ninepatch/tests/src/com/android/ninepatch/NinePatchTest.java | 48 |
13 files changed, 0 insertions, 1166 deletions
diff --git a/ninepatch/.classpath b/ninepatch/.classpath deleted file mode 100644 index fb50116..0000000 --- a/ninepatch/.classpath +++ /dev/null @@ -1,6 +0,0 @@ -<?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> diff --git a/ninepatch/.gitignore b/ninepatch/.gitignore deleted file mode 100644 index ba077a4..0000000 --- a/ninepatch/.gitignore +++ /dev/null @@ -1 +0,0 @@ -bin diff --git a/ninepatch/.project b/ninepatch/.project deleted file mode 100644 index 192f000..0000000 --- a/ninepatch/.project +++ /dev/null @@ -1,17 +0,0 @@ -<?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> diff --git a/ninepatch/Android.mk b/ninepatch/Android.mk deleted file mode 100644 index 5f6cbed..0000000 --- a/ninepatch/Android.mk +++ /dev/null @@ -1,26 +0,0 @@ -# -# Copyright (C) 2008 The Android Open Source Project -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -LOCAL_PATH := $(call my-dir) -include $(CLEAR_VARS) - -LOCAL_SRC_FILES := $(call all-java-files-under,src) - -LOCAL_MODULE := ninepatch - -include $(BUILD_HOST_JAVA_LIBRARY) - -# Build all sub-directories -include $(call all-makefiles-under,$(LOCAL_PATH)) diff --git a/ninepatch/NOTICE b/ninepatch/NOTICE deleted file mode 100644 index c5b1efa..0000000 --- a/ninepatch/NOTICE +++ /dev/null @@ -1,190 +0,0 @@ - - Copyright (c) 2005-2008, The Android Open Source Project - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - - - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - diff --git a/ninepatch/src/com/android/ninepatch/GraphicsUtilities.java b/ninepatch/src/com/android/ninepatch/GraphicsUtilities.java deleted file mode 100644 index e8d71d6..0000000 --- a/ninepatch/src/com/android/ninepatch/GraphicsUtilities.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.ninepatch; - -import javax.imageio.ImageIO; -import java.awt.image.BufferedImage; -import java.awt.image.Raster; -import java.awt.GraphicsConfiguration; -import java.awt.GraphicsEnvironment; -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 { - BufferedImage image = ImageIO.read(resource); - 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); - } - - public static BufferedImage toCompatibleImage(BufferedImage image) { - if (isHeadless()) { - return image; - } - - if (image.getColorModel().equals(getGraphicsConfiguration().getColorModel())) { - return image; - } - - BufferedImage compatibleImage = getGraphicsConfiguration().createCompatibleImage( - image.getWidth(), image.getHeight(), image.getTransparency()); - Graphics g = compatibleImage.getGraphics(); - g.drawImage(image, 0, 0, null); - g.dispose(); - - return compatibleImage; - } - - public static BufferedImage createCompatibleImage(BufferedImage image, int width, int height) { - return getGraphicsConfiguration().createCompatibleImage(width, height, - image.getTransparency()); - } - - private static GraphicsConfiguration getGraphicsConfiguration() { - GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment(); - return environment.getDefaultScreenDevice().getDefaultConfiguration(); - } - - private static boolean isHeadless() { - return GraphicsEnvironment.isHeadless(); - } - - public static BufferedImage createTranslucentCompatibleImage(int width, int height) { - return getGraphicsConfiguration().createCompatibleImage(width, height, - Transparency.TRANSLUCENT); - } - - public static int[] getPixels(BufferedImage img, int x, int y, int w, int h, int[] pixels) { - if (w == 0 || h == 0) { - return new int[0]; - } - - if (pixels == null) { - pixels = new int[w * h]; - } else if (pixels.length < w * h) { - throw new IllegalArgumentException("Pixels array must have a length >= w * h"); - } - - int imageType = img.getType(); - if (imageType == BufferedImage.TYPE_INT_ARGB || imageType == BufferedImage.TYPE_INT_RGB) { - Raster raster = img.getRaster(); - return (int[]) raster.getDataElements(x, y, w, h, pixels); - } - - // Unmanages the image - return img.getRGB(x, y, w, h, pixels, 0, w); - } -} diff --git a/ninepatch/src/com/android/ninepatch/NinePatch.java b/ninepatch/src/com/android/ninepatch/NinePatch.java deleted file mode 100644 index 2803a9e..0000000 --- a/ninepatch/src/com/android/ninepatch/NinePatch.java +++ /dev/null @@ -1,224 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.ninepatch; - -import java.awt.Graphics2D; -import java.awt.image.BufferedImage; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.net.URL; - -/** - * Represents a 9-Patch bitmap. - * - * DO NOT CHANGE THIS API OR OLDER VERSIONS OF LAYOUTLIB WILL CRASH. - * - * This is a full representation of a NinePatch with both a {@link BufferedImage} and a - * {@link NinePatchChunk}. - * - * Newer versions of the Layoutlib will use only the {@link NinePatchChunk} as the default - * nine patch drawable references a normal Android bitmap which contains a BufferedImage - * through a Bitmap_Delegate. - * - */ -public class NinePatch { - public static final String EXTENSION_9PATCH = ".9.png"; - - private BufferedImage mImage; - private NinePatchChunk mChunk; - - public BufferedImage getImage() { - return mImage; - } - - public NinePatchChunk getChunk() { - return mChunk; - } - - /** - * LEGACY METHOD to run older versions of Android Layoutlib. - * ==== DO NOT CHANGE ==== - * - * 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 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(URL fileUrl, boolean convert) throws IOException { - BufferedImage image = null; - try { - image = GraphicsUtilities.loadCompatibleImage(fileUrl); - } catch (MalformedURLException e) { - // really this shouldn't be happening since we're not creating the URL manually. - return null; - } - - boolean is9Patch = fileUrl.getPath().toLowerCase().endsWith(EXTENSION_9PATCH); - - return load(image, is9Patch, convert); - } - - /** - * LEGACY METHOD to run older versions of Android Layoutlib. - * ==== DO NOT CHANGE ==== - * - * 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); - } - - /** - * LEGACY METHOD to run older versions of Android Layoutlib. - * ==== DO NOT CHANGE ==== - * - * 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); - } else { - return null; - } - } else { - ensure9Patch(image); - } - - return new NinePatch(image); - } - - /** - * LEGACY METHOD to run older versions of Android Layoutlib. - * ==== DO NOT CHANGE ==== - * - * @return - */ - public int getWidth() { - return mImage.getWidth(); - } - - /** - * LEGACY METHOD to run older versions of Android Layoutlib. - * ==== DO NOT CHANGE ==== - * - * @return - */ - public int getHeight() { - return mImage.getHeight(); - } - - /** - * LEGACY METHOD to run older versions of Android Layoutlib. - * ==== DO NOT CHANGE ==== - * - * @param padding array of left, top, right, bottom padding - * @return - */ - public boolean getPadding(int[] padding) { - mChunk.getPadding(padding); - return true; - } - - - /** - * LEGACY METHOD to run older versions of Android Layoutlib. - * ==== DO NOT CHANGE ==== - * - * @param graphics2D - * @param x - * @param y - * @param scaledWidth - * @param scaledHeight - */ - public void draw(Graphics2D graphics2D, int x, int y, int scaledWidth, int scaledHeight) { - mChunk.draw(mImage, graphics2D, x, y, scaledWidth, scaledHeight, 0 , 0); - } - - private NinePatch(BufferedImage image) { - mChunk = NinePatchChunk.create(image); - mImage = extractBitmapContent(image); - } - - private static void ensure9Patch(BufferedImage image) { - int width = image.getWidth(); - int height = image.getHeight(); - for (int i = 0; i < width; i++) { - int pixel = image.getRGB(i, 0); - if (pixel != 0 && pixel != 0xFF000000) { - image.setRGB(i, 0, 0); - } - pixel = image.getRGB(i, height - 1); - if (pixel != 0 && pixel != 0xFF000000) { - image.setRGB(i, height - 1, 0); - } - } - for (int i = 0; i < height; i++) { - int pixel = image.getRGB(0, i); - if (pixel != 0 && pixel != 0xFF000000) { - image.setRGB(0, i, 0); - } - pixel = image.getRGB(width - 1, i); - if (pixel != 0 && pixel != 0xFF000000) { - image.setRGB(width - 1, i, 0); - } - } - } - - private static BufferedImage convertTo9Patch(BufferedImage image) { - BufferedImage buffer = GraphicsUtilities.createTranslucentCompatibleImage( - image.getWidth() + 2, image.getHeight() + 2); - - Graphics2D g2 = buffer.createGraphics(); - g2.drawImage(image, 1, 1, null); - g2.dispose(); - - return buffer; - } - - private BufferedImage extractBitmapContent(BufferedImage image) { - return image.getSubimage(1, 1, image.getWidth() - 2, image.getHeight() - 2); - } - -} diff --git a/ninepatch/src/com/android/ninepatch/NinePatchChunk.java b/ninepatch/src/com/android/ninepatch/NinePatchChunk.java deleted file mode 100644 index 20abaeb..0000000 --- a/ninepatch/src/com/android/ninepatch/NinePatchChunk.java +++ /dev/null @@ -1,494 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.ninepatch; - -import java.awt.Graphics2D; -import java.awt.Rectangle; -import java.awt.RenderingHints; -import java.awt.image.BufferedImage; -import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; - -/** - * The chunk information for a nine patch. - * - * This does not represent the bitmap, only the chunk info responsible for the padding and the - * stretching areas. - * - * Since android.graphics.drawable.NinePatchDrawable and android.graphics.NinePatch both deal with - * the nine patch chunk as a byte[], this class is converted to and from byte[] through - * serialization. - * - * This is meant to be used with the NinePatch_Delegate in Layoutlib API 5+. - */ -public class NinePatchChunk implements Serializable { - - /** Generated Serial Version UID */ - private static final long serialVersionUID = -7353439224505296217L; - - private static final int[] sPaddingRect = new int[4]; - - private boolean mVerticalStartWithPatch; - private boolean mHorizontalStartWithPatch; - - private List<Rectangle> mFixed; - private List<Rectangle> mPatches; - private List<Rectangle> mHorizontalPatches; - private List<Rectangle> mVerticalPatches; - - private Pair<Integer> mHorizontalPadding; - private Pair<Integer> mVerticalPadding; - - - /** - * Data computed during drawing. - */ - static final class DrawingData { - private int mRemainderHorizontal; - private int mRemainderVertical; - private float mHorizontalPatchesSum; - private float mVerticalPatchesSum; - } - - /** - * Computes and returns the 9-patch chunks. - * @param image the image containing both the content and the control outer line. - * @return the {@link NinePatchChunk}. - */ - public static NinePatchChunk create(BufferedImage image) { - NinePatchChunk chunk = new NinePatchChunk(); - chunk.findPatches(image); - return chunk; - } - - public void draw(BufferedImage image, Graphics2D graphics2D, int x, int y, int scaledWidth, - int scaledHeight, int destDensity, int srcDensity) { - - boolean scaling = destDensity != srcDensity && destDensity != 0 && srcDensity != 0; - - if (scaling) { - try { - graphics2D = (Graphics2D) graphics2D.create(); - - // scale and transform - float densityScale = ((float) destDensity) / (float) srcDensity; - - // translate/rotate the canvas. - graphics2D.translate(x, y); - graphics2D.scale(densityScale, densityScale); - - // sets the new drawing bounds. - scaledWidth /= densityScale; - scaledHeight /= densityScale; - x = y = 0; - - // draw - draw(image, graphics2D, x, y, scaledWidth, scaledHeight); - } finally { - graphics2D.dispose(); - } - } else { - // non density-scaled rendering - draw(image, graphics2D, x, y, scaledWidth, scaledHeight); - } - } - - private void draw(BufferedImage image, Graphics2D graphics2D, int x, int y, int scaledWidth, - int scaledHeight) { - if (scaledWidth <= 1 || scaledHeight <= 1) { - return; - } - - Graphics2D g = (Graphics2D)graphics2D.create(); - g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, - RenderingHints.VALUE_INTERPOLATION_BILINEAR); - - try { - if (mPatches.size() == 0) { - g.drawImage(image, x, y, scaledWidth, scaledHeight, null); - return; - } - - g.translate(x, y); - x = y = 0; - - DrawingData data = computePatches(scaledWidth, scaledHeight); - - int fixedIndex = 0; - int horizontalIndex = 0; - int verticalIndex = 0; - int patchIndex = 0; - - boolean hStretch; - boolean vStretch; - - float vWeightSum = 1.0f; - float vRemainder = data.mRemainderVertical; - - vStretch = mVerticalStartWithPatch; - while (y < scaledHeight - 1) { - hStretch = mHorizontalStartWithPatch; - - int height = 0; - float vExtra = 0.0f; - - float hWeightSum = 1.0f; - float hRemainder = data.mRemainderHorizontal; - - while (x < scaledWidth - 1) { - Rectangle r; - if (!vStretch) { - if (hStretch) { - r = mHorizontalPatches.get(horizontalIndex++); - float extra = r.width / data.mHorizontalPatchesSum; - int width = (int) (extra * hRemainder / hWeightSum); - hWeightSum -= extra; - hRemainder -= width; - g.drawImage(image, x, y, x + width, y + r.height, r.x, r.y, - r.x + r.width, r.y + r.height, null); - x += width; - } else { - r = mFixed.get(fixedIndex++); - g.drawImage(image, x, y, x + r.width, y + r.height, r.x, r.y, - r.x + r.width, r.y + r.height, null); - x += r.width; - } - height = r.height; - } else { - if (hStretch) { - r = mPatches.get(patchIndex++); - vExtra = r.height / data.mVerticalPatchesSum; - height = (int) (vExtra * vRemainder / vWeightSum); - float extra = r.width / data.mHorizontalPatchesSum; - int width = (int) (extra * hRemainder / hWeightSum); - hWeightSum -= extra; - hRemainder -= width; - g.drawImage(image, x, y, x + width, y + height, r.x, r.y, - r.x + r.width, r.y + r.height, null); - x += width; - } else { - r = mVerticalPatches.get(verticalIndex++); - vExtra = r.height / data.mVerticalPatchesSum; - height = (int) (vExtra * vRemainder / vWeightSum); - g.drawImage(image, x, y, x + r.width, y + height, r.x, r.y, - r.x + r.width, r.y + r.height, null); - x += r.width; - } - - } - hStretch = !hStretch; - } - x = 0; - y += height; - if (vStretch) { - vWeightSum -= vExtra; - vRemainder -= height; - } - vStretch = !vStretch; - } - - } finally { - g.dispose(); - } - } - - /** - * Fills the given array with the nine patch padding. - * - * @param padding array of left, top, right, bottom padding - */ - public void getPadding(int[] padding) { - padding[0] = mHorizontalPadding.mFirst; // left - padding[2] = mHorizontalPadding.mSecond; // right - padding[1] = mVerticalPadding.mFirst; // top - padding[3] = mVerticalPadding.mSecond; // bottom - } - - /** - * Returns the padding as an int[] describing left, top, right, bottom. - * - * This method is not thread-safe and returns an array owned by the {@link NinePatchChunk} - * class. - * @return an internal array filled with the padding. - */ - public int[] getPadding() { - getPadding(sPaddingRect); - return sPaddingRect; - } - - private DrawingData computePatches(int scaledWidth, int scaledHeight) { - DrawingData data = new DrawingData(); - boolean measuredWidth = false; - boolean endRow = true; - - int remainderHorizontal = 0; - int remainderVertical = 0; - - if (mFixed.size() > 0) { - int start = mFixed.get(0).y; - for (Rectangle rect : mFixed) { - if (rect.y > start) { - endRow = true; - measuredWidth = true; - } - if (!measuredWidth) { - remainderHorizontal += rect.width; - } - if (endRow) { - remainderVertical += rect.height; - endRow = false; - start = rect.y; - } - } - } - - data.mRemainderHorizontal = scaledWidth - remainderHorizontal; - data.mRemainderVertical = scaledHeight - remainderVertical; - - data.mHorizontalPatchesSum = 0; - if (mHorizontalPatches.size() > 0) { - int start = -1; - for (Rectangle rect : mHorizontalPatches) { - if (rect.x > start) { - data.mHorizontalPatchesSum += rect.width; - start = rect.x; - } - } - } else { - int start = -1; - for (Rectangle rect : mPatches) { - if (rect.x > start) { - data.mHorizontalPatchesSum += rect.width; - start = rect.x; - } - } - } - - data.mVerticalPatchesSum = 0; - if (mVerticalPatches.size() > 0) { - int start = -1; - for (Rectangle rect : mVerticalPatches) { - if (rect.y > start) { - data.mVerticalPatchesSum += rect.height; - start = rect.y; - } - } - } else { - int start = -1; - for (Rectangle rect : mPatches) { - if (rect.y > start) { - data.mVerticalPatchesSum += rect.height; - start = rect.y; - } - } - } - - return data; - } - - - /** - * Finds the 9-patch patches and padding from a {@link BufferedImage} image that contains - * both the image content and the control outer lines. - */ - private void findPatches(BufferedImage image) { - // the size of the actual image content - int width = image.getWidth() - 2; - int height = image.getHeight() - 2; - - int[] row = null; - int[] column = null; - - // extract the patch line. Make sure to start at 1 and be only as long as the image content, - // to not include the outer control line. - row = GraphicsUtilities.getPixels(image, 1, 0, width, 1, row); - column = GraphicsUtilities.getPixels(image, 0, 1, 1, height, column); - - boolean[] result = new boolean[1]; - Pair<List<Pair<Integer>>> left = getPatches(column, result); - mVerticalStartWithPatch = result[0]; - - result = new boolean[1]; - Pair<List<Pair<Integer>>> top = getPatches(row, result); - mHorizontalStartWithPatch = result[0]; - - mFixed = getRectangles(left.mFirst, top.mFirst); - mPatches = getRectangles(left.mSecond, top.mSecond); - - if (mFixed.size() > 0) { - mHorizontalPatches = getRectangles(left.mFirst, top.mSecond); - mVerticalPatches = getRectangles(left.mSecond, top.mFirst); - } else { - if (top.mFirst.size() > 0) { - mHorizontalPatches = new ArrayList<Rectangle>(0); - mVerticalPatches = getVerticalRectangles(height, top.mFirst); - } else if (left.mFirst.size() > 0) { - mHorizontalPatches = getHorizontalRectangles(width, left.mFirst); - mVerticalPatches = new ArrayList<Rectangle>(0); - } else { - mHorizontalPatches = mVerticalPatches = new ArrayList<Rectangle>(0); - } - } - - // extract the padding line. Make sure to start at 1 and be only as long as the image - // content, to not include the outer control line. - row = GraphicsUtilities.getPixels(image, 1, height + 1, width, 1, row); - column = GraphicsUtilities.getPixels(image, width + 1, 1, 1, height, column); - - top = getPatches(row, result); - mHorizontalPadding = getPadding(top.mFirst); - - left = getPatches(column, result); - mVerticalPadding = getPadding(left.mFirst); - } - - private List<Rectangle> getVerticalRectangles(int imageHeight, - List<Pair<Integer>> topPairs) { - List<Rectangle> rectangles = new ArrayList<Rectangle>(); - for (Pair<Integer> top : topPairs) { - int x = top.mFirst; - int width = top.mSecond - top.mFirst; - - rectangles.add(new Rectangle(x, 0, width, imageHeight)); - } - return rectangles; - } - - private List<Rectangle> getHorizontalRectangles(int imageWidth, - List<Pair<Integer>> leftPairs) { - List<Rectangle> rectangles = new ArrayList<Rectangle>(); - for (Pair<Integer> left : leftPairs) { - int y = left.mFirst; - int height = left.mSecond - left.mFirst; - - rectangles.add(new Rectangle(0, y, imageWidth, height)); - } - return rectangles; - } - - private Pair<Integer> getPadding(List<Pair<Integer>> pairs) { - if (pairs.size() == 0) { - return new Pair<Integer>(0, 0); - } else if (pairs.size() == 1) { - if (pairs.get(0).mFirst == 0) { - return new Pair<Integer>(pairs.get(0).mSecond - pairs.get(0).mFirst, 0); - } else { - return new Pair<Integer>(0, pairs.get(0).mSecond - pairs.get(0).mFirst); - } - } else { - int index = pairs.size() - 1; - return new Pair<Integer>(pairs.get(0).mSecond - pairs.get(0).mFirst, - pairs.get(index).mSecond - pairs.get(index).mFirst); - } - } - - private List<Rectangle> getRectangles(List<Pair<Integer>> leftPairs, - List<Pair<Integer>> topPairs) { - List<Rectangle> rectangles = new ArrayList<Rectangle>(); - for (Pair<Integer> left : leftPairs) { - int y = left.mFirst; - int height = left.mSecond - left.mFirst; - for (Pair<Integer> top : topPairs) { - int x = top.mFirst; - int width = top.mSecond - top.mFirst; - - rectangles.add(new Rectangle(x, y, width, height)); - } - } - return rectangles; - } - - /** - * Computes a list of Patch based on a pixel line. - * - * This returns both the fixed areas, and the patches (stretchable) areas. - * - * The return value is a pair of list. The first list ({@link Pair#mFirst}) is the list - * of fixed area. The second list ({@link Pair#mSecond}) is the list of stretchable areas. - * - * Each area is defined as a Pair of (start, end) coordinate in the given line. - * - * @param pixels the pixels of the control line. The line should have the same length as the - * content (i.e. it should be stripped of the first/last control pixel which are not - * used) - * @param startWithPatch a boolean array of size 1 used to return the boolean value of whether - * a patch (stretchable area) is first or not. - * @return - */ - private Pair<List<Pair<Integer>>> getPatches(int[] pixels, boolean[] startWithPatch) { - int lastIndex = 0; - int lastPixel = pixels[0]; - boolean first = true; - - List<Pair<Integer>> fixed = new ArrayList<Pair<Integer>>(); - List<Pair<Integer>> patches = new ArrayList<Pair<Integer>>(); - - for (int i = 0; i < pixels.length; i++) { - int pixel = pixels[i]; - if (pixel != lastPixel) { - if (lastPixel == 0xFF000000) { - if (first) startWithPatch[0] = true; - patches.add(new Pair<Integer>(lastIndex, i)); - } else { - fixed.add(new Pair<Integer>(lastIndex, i)); - } - first = false; - - lastIndex = i; - lastPixel = pixel; - } - } - if (lastPixel == 0xFF000000) { - if (first) startWithPatch[0] = true; - patches.add(new Pair<Integer>(lastIndex, pixels.length)); - } else { - fixed.add(new Pair<Integer>(lastIndex, pixels.length)); - } - - if (patches.size() == 0) { - patches.add(new Pair<Integer>(1, pixels.length)); - startWithPatch[0] = true; - fixed.clear(); - } - - return new Pair<List<Pair<Integer>>>(fixed, patches); - } - - /** - * A pair of values. - * - * @param <E> - */ - /*package*/ static class Pair<E> implements Serializable { - /** Generated Serial Version UID */ - private static final long serialVersionUID = -2204108979541762418L; - E mFirst; - E mSecond; - - Pair(E first, E second) { - mFirst = first; - mSecond = second; - } - - @Override - public String toString() { - return "Pair[" + mFirst + ", " + mSecond + "]"; - } - } - -} diff --git a/ninepatch/tests/.classpath b/ninepatch/tests/.classpath deleted file mode 100644 index 26542d3..0000000 --- a/ninepatch/tests/.classpath +++ /dev/null @@ -1,8 +0,0 @@ -<?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="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/3"/> - <classpathentry combineaccessrules="false" kind="src" path="/ninepatch"/> - <classpathentry kind="output" path="bin"/> -</classpath> diff --git a/ninepatch/tests/.project b/ninepatch/tests/.project deleted file mode 100644 index 3d049cf..0000000 --- a/ninepatch/tests/.project +++ /dev/null @@ -1,17 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<projectDescription> - <name>ninepatch-tests</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> diff --git a/ninepatch/tests/Android.mk b/ninepatch/tests/Android.mk deleted file mode 100644 index 8a9fd71..0000000 --- a/ninepatch/tests/Android.mk +++ /dev/null @@ -1,33 +0,0 @@ -# Copyright (C) 2010 The Android Open Source Project -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -LOCAL_PATH := $(call my-dir) - -include $(CLEAR_VARS) - -# Only compile source java files in this lib. -LOCAL_SRC_FILES := $(call all-java-files-under, src) -LOCAL_JAVA_RESOURCE_DIRS := res - -LOCAL_MODULE := ninepatch-tests -LOCAL_MODULE_TAGS := optional - -LOCAL_JAVA_LIBRARIES := junit -# bundle ninepatch inside the test jar for continuous tests -LOCAL_STATIC_JAVA_LIBRARIES := ninepatch - -include $(BUILD_HOST_JAVA_LIBRARY) - -# Build all sub-directories -include $(call all-makefiles-under,$(LOCAL_PATH)) diff --git a/ninepatch/tests/res/com/android/ninepatch/button.9.png b/ninepatch/tests/res/com/android/ninepatch/button.9.png Binary files differdeleted file mode 100644 index 9d52f40..0000000 --- a/ninepatch/tests/res/com/android/ninepatch/button.9.png +++ /dev/null diff --git a/ninepatch/tests/src/com/android/ninepatch/NinePatchTest.java b/ninepatch/tests/src/com/android/ninepatch/NinePatchTest.java deleted file mode 100644 index 1722b55..0000000 --- a/ninepatch/tests/src/com/android/ninepatch/NinePatchTest.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (C) 2008 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.ninepatch; - -import java.io.InputStream; - -import junit.framework.TestCase; - -public class NinePatchTest extends TestCase { - - private NinePatch mPatch; - - @Override - protected void setUp() throws Exception { - InputStream stream = this.getClass().getResourceAsStream("button.9.png"); - - mPatch = NinePatch.load(stream, true /* is9Patch*/, false /* convert */); - } - - public void test9PatchLoad() throws Exception { - assertNotNull(mPatch); - } - - public void test9PatchMinSize() { - int[] padding = new int[4]; - mPatch.getPadding(padding); - assertEquals(13, padding[0]); - assertEquals(3, padding[1]); - assertEquals(13, padding[2]); - assertEquals(4, padding[3]); - assertEquals(36, mPatch.getWidth()); - assertEquals(25, mPatch.getHeight()); - } -} |