diff options
author | Dianne Hackborn <hackbod@google.com> | 2009-07-18 17:13:29 -0700 |
---|---|---|
committer | Dianne Hackborn <hackbod@google.com> | 2009-07-18 17:13:29 -0700 |
commit | 2784ff0af88128f66ae690b73d48fb7e4a211e68 (patch) | |
tree | 2e43aaa6e2a67a0924449af42d969b91de2f995d /graphics/java/android | |
parent | a53b828635fce8b6b2d3e3377d74d72070056623 (diff) | |
download | frameworks_base-2784ff0af88128f66ae690b73d48fb7e4a211e68.zip frameworks_base-2784ff0af88128f66ae690b73d48fb7e4a211e68.tar.gz frameworks_base-2784ff0af88128f66ae690b73d48fb7e4a211e68.tar.bz2 |
Fix issue where scaled bitmap sizes could be wrong.
The Bitmap functions to get the scaled width/height couldn't actually
do the right thing because they didn't know the destination they would
be drawing to. Now there are two forms of them, taking an explicit
parameter specifying the destination.
Diffstat (limited to 'graphics/java/android')
-rw-r--r-- | graphics/java/android/graphics/Bitmap.java | 47 |
1 files changed, 43 insertions, 4 deletions
diff --git a/graphics/java/android/graphics/Bitmap.java b/graphics/java/android/graphics/Bitmap.java index 141553e..df659ef 100644 --- a/graphics/java/android/graphics/Bitmap.java +++ b/graphics/java/android/graphics/Bitmap.java @@ -18,6 +18,7 @@ package android.graphics; import android.os.Parcel; import android.os.Parcelable; +import android.util.DisplayMetrics; import java.io.OutputStream; import java.nio.Buffer; @@ -605,22 +606,60 @@ public final class Bitmap implements Parcelable { * Convenience method that returns the width of this bitmap divided * by the density scale factor. * + * @param canvas The Canvas the bitmap will be drawn to. * @return The scaled width of this bitmap, according to the density scale factor. */ - public int getScaledWidth() { + public int getScaledWidth(Canvas canvas) { final float scale = mDensityScale; - return scale == DENSITY_SCALE_UNKNOWN ? getWidth() : (int) (getWidth() / scale); + if (!mAutoScaling || scale < 0) { + return getWidth(); + } + return (int)(getWidth() * canvas.getDensityScale() / scale); + } + + /** + * Convenience method that returns the height of this bitmap divided + * by the density scale factor. + * + * @param canvas The Canvas the bitmap will be drawn to. + * @return The scaled height of this bitmap, according to the density scale factor. + */ + public int getScaledHeight(Canvas canvas) { + final float scale = mDensityScale; + if (!mAutoScaling || scale < 0) { + return getHeight(); + } + return (int)(getHeight() * canvas.getDensityScale() / scale); + } + + /** + * Convenience method that returns the width of this bitmap divided + * by the density scale factor. + * + * @param metrics The target display metrics. + * @return The scaled width of this bitmap, according to the density scale factor. + */ + public int getScaledWidth(DisplayMetrics metrics) { + final float scale = mDensityScale; + if (!mAutoScaling || scale < 0) { + return getWidth(); + } + return (int)(getWidth() * metrics.density / scale); } /** * Convenience method that returns the height of this bitmap divided * by the density scale factor. * + * @param metrics The target display metrics. * @return The scaled height of this bitmap, according to the density scale factor. */ - public int getScaledHeight() { + public int getScaledHeight(DisplayMetrics metrics) { final float scale = mDensityScale; - return scale == DENSITY_SCALE_UNKNOWN ? getWidth() : (int) (getHeight() / scale); + if (!mAutoScaling || scale < 0) { + return getHeight(); + } + return (int)(getHeight() * metrics.density / scale); } /** |