aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXavier Ducrohet <xav@android.com>2010-12-13 18:27:10 -0800
committerXavier Ducrohet <xav@android.com>2010-12-13 19:08:12 -0800
commitc27acc39b25a1dee3fecd28601d57e9aebbc7cc0 (patch)
treef523dec8e6d5e3318d333802e959ca394c32fddf
parent3ab7b4c18c727ae39bead514b3c666f6407e02a7 (diff)
downloadsdk-c27acc39b25a1dee3fecd28601d57e9aebbc7cc0.zip
sdk-c27acc39b25a1dee3fecd28601d57e9aebbc7cc0.tar.gz
sdk-c27acc39b25a1dee3fecd28601d57e9aebbc7cc0.tar.bz2
ninepatch support for drawing at a different density.
Change-Id: I0932f8a51d30c256157fccf67e77cf165ba93b58
-rw-r--r--ninepatch/src/com/android/ninepatch/NinePatch.java2
-rw-r--r--ninepatch/src/com/android/ninepatch/NinePatchChunk.java32
2 files changed, 33 insertions, 1 deletions
diff --git a/ninepatch/src/com/android/ninepatch/NinePatch.java b/ninepatch/src/com/android/ninepatch/NinePatch.java
index ed5ecff..2803a9e 100644
--- a/ninepatch/src/com/android/ninepatch/NinePatch.java
+++ b/ninepatch/src/com/android/ninepatch/NinePatch.java
@@ -173,7 +173,7 @@ public class NinePatch {
* @param scaledHeight
*/
public void draw(Graphics2D graphics2D, int x, int y, int scaledWidth, int scaledHeight) {
- mChunk.draw(mImage, graphics2D, x, y, scaledWidth, scaledHeight);
+ mChunk.draw(mImage, graphics2D, x, y, scaledWidth, scaledHeight, 0 , 0);
}
private NinePatch(BufferedImage image) {
diff --git a/ninepatch/src/com/android/ninepatch/NinePatchChunk.java b/ninepatch/src/com/android/ninepatch/NinePatchChunk.java
index dedc1d6..6dca61e 100644
--- a/ninepatch/src/com/android/ninepatch/NinePatchChunk.java
+++ b/ninepatch/src/com/android/ninepatch/NinePatchChunk.java
@@ -77,6 +77,38 @@ public class NinePatchChunk implements Serializable {
}
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 / 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;