diff options
author | Shih-chia Cheng <shihchia@google.com> | 2011-06-23 11:06:39 +0800 |
---|---|---|
committer | Shih-chia Cheng <shihchia@google.com> | 2011-06-23 11:57:24 +0800 |
commit | cff9e72a5a70a074be896cc3ed54b4696c0614f5 (patch) | |
tree | d0e49932bf0bf2e4a9b3af7556aa879e5316cac9 /src/com/android/videoeditor/util | |
parent | 3fad22764165a85c6c29e188f924c8372fffe991 (diff) | |
download | VideoEditor-cff9e72a5a70a074be896cc3ed54b4696c0614f5.tar.gz |
Refactor scaleImage method in ImageUtils.
Change-Id: If4a99e11551291eefef94407123f5179ce6e8cff
Diffstat (limited to 'src/com/android/videoeditor/util')
-rwxr-xr-x | src/com/android/videoeditor/util/ImageUtils.java | 81 |
1 files changed, 33 insertions, 48 deletions
diff --git a/src/com/android/videoeditor/util/ImageUtils.java b/src/com/android/videoeditor/util/ImageUtils.java index e4ffab4..58a5e76 100755 --- a/src/com/android/videoeditor/util/ImageUtils.java +++ b/src/com/android/videoeditor/util/ImageUtils.java @@ -81,55 +81,40 @@ public class ImageUtils { final int nativeHeight = dbo.outHeight; final Bitmap srcBitmap; - float bitmapWidth, bitmapHeight; + float scaledWidth, scaledHeight; + final BitmapFactory.Options options = new BitmapFactory.Options(); if (nativeWidth > width || nativeHeight > height) { - float dx = ((float)nativeWidth) / ((float)width); - float dy = ((float)nativeHeight) / ((float)height); - if (match == MATCH_SMALLER_DIMENSION) { // Match smaller dimension - if (dx > dy) { - bitmapWidth = width; - bitmapHeight = nativeHeight / dx; - } else { - bitmapWidth = nativeWidth / dy; - bitmapHeight = height; - } - } else { // Match larger dimension - if (dx > dy) { - bitmapWidth = nativeWidth / dy; - bitmapHeight = height; - } else { - bitmapWidth = width; - bitmapHeight = nativeHeight / dx; - } - } - - // Create the bitmap from file - if (nativeWidth / bitmapWidth > 1) { - final BitmapFactory.Options options = new BitmapFactory.Options(); - options.inSampleSize = nativeWidth / (int)bitmapWidth; - srcBitmap = BitmapFactory.decodeFile(filename, options); - } else { - srcBitmap = BitmapFactory.decodeFile(filename); - } - } else { - bitmapWidth = width; - bitmapHeight = height; - srcBitmap = BitmapFactory.decodeFile(filename); - } - - if (srcBitmap == null) { - throw new IOException("Cannot decode file: " + filename); - } - - // Create the canvas bitmap - final Bitmap bitmap = Bitmap.createBitmap((int)bitmapWidth, (int)bitmapHeight, - Bitmap.Config.ARGB_8888); - final Canvas canvas = new Canvas(bitmap); - canvas.drawBitmap(srcBitmap, new Rect(0, 0, srcBitmap.getWidth(), srcBitmap.getHeight()), - new Rect(0, 0, (int)bitmapWidth, (int)bitmapHeight), sResizePaint); - // Release the source bitmap - srcBitmap.recycle(); - return bitmap; + float dx = ((float) nativeWidth) / ((float) width); + float dy = ((float) nativeHeight) / ((float) height); + float scale = (match == MATCH_SMALLER_DIMENSION) ? Math.max(dx,dy) : Math.min(dx,dy); + scaledWidth = nativeWidth / scale; + scaledHeight = nativeHeight / scale; + // Create the bitmap from file. + options.inSampleSize = (scale > 1.0f) ? ((int) scale) : 1; + } else { + scaledWidth = width; + scaledHeight = height; + options.inSampleSize = 1; + } + + srcBitmap = BitmapFactory.decodeFile(filename, options); + if (srcBitmap == null) { + throw new IOException("Cannot decode file: " + filename); + } + + // Create the canvas bitmap. + final Bitmap bitmap = Bitmap.createBitmap(Math.round(scaledWidth), + Math.round(scaledHeight), + Bitmap.Config.ARGB_8888); + final Canvas canvas = new Canvas(bitmap); + canvas.drawBitmap(srcBitmap, + new Rect(0, 0, srcBitmap.getWidth(), srcBitmap.getHeight()), + new Rect(0, 0, Math.round(scaledWidth), Math.round(scaledHeight)), + sResizePaint); + + // Release the source bitmap + srcBitmap.recycle(); + return bitmap; } /** |