diff options
author | jdduke@chromium.org <jdduke@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-08-01 00:11:55 +0000 |
---|---|---|
committer | jdduke@chromium.org <jdduke@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-08-01 00:11:55 +0000 |
commit | 796cecfd7c22764cb7dc1ae9ae8db3acd03e1f41 (patch) | |
tree | f5ae05471bf0f6dc3fc24908a46fea7cd2f71fb8 /ui/android | |
parent | 2b637b698c76e43c9cf729e868cf7618d5327cc8 (diff) | |
download | chromium_src-796cecfd7c22764cb7dc1ae9ae8db3acd03e1f41.zip chromium_src-796cecfd7c22764cb7dc1ae9ae8db3acd03e1f41.tar.gz chromium_src-796cecfd7c22764cb7dc1ae9ae8db3acd03e1f41.tar.bz2 |
[Android] Subsample overscroll resources when first loading
Subsampling the overscroll images when they are first loaded reduces both the
their initial memory footprint and the time required to perform the final
resize.
BUG=245903
Review URL: https://chromiumcodereview.appspot.com/21206003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@214874 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui/android')
-rw-r--r-- | ui/android/java/src/org/chromium/ui/gfx/BitmapHelper.java | 48 |
1 files changed, 45 insertions, 3 deletions
diff --git a/ui/android/java/src/org/chromium/ui/gfx/BitmapHelper.java b/ui/android/java/src/org/chromium/ui/gfx/BitmapHelper.java index 5e237f7..97511a1 100644 --- a/ui/android/java/src/org/chromium/ui/gfx/BitmapHelper.java +++ b/ui/android/java/src/org/chromium/ui/gfx/BitmapHelper.java @@ -17,11 +17,53 @@ public class BitmapHelper { return Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); } + /** + * Decode and sample down a bitmap resource to the requested width and height. + * + * @param name The resource name of the bitmap to decode. + * @param reqWidth The requested width of the resulting bitmap. + * @param reqHeight The requested height of the resulting bitmap. + * @return A bitmap sampled down from the original with the same aspect ratio and dimensions. + * that are equal to or greater than the requested width and height. + */ @CalledByNative - public static Bitmap decodeDrawableResource(String name) { + private static Bitmap decodeDrawableResource(String name, + int reqWidth, + int reqHeight) { Resources res = Resources.getSystem(); - int resource_id = res.getIdentifier(name, null, null); + int resId = res.getIdentifier(name, null, null); - return BitmapFactory.decodeResource(res, resource_id); + final BitmapFactory.Options options = new BitmapFactory.Options(); + options.inJustDecodeBounds = true; + BitmapFactory.decodeResource(res, resId, options); + + options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); + + options.inJustDecodeBounds = false; + return BitmapFactory.decodeResource(res, resId, options); + } + + // http://developer.android.com/training/displaying-bitmaps/load-bitmap.html + private static int calculateInSampleSize(BitmapFactory.Options options, + int reqWidth, + int reqHeight) { + // Raw height and width of image + final int height = options.outHeight; + final int width = options.outWidth; + int inSampleSize = 1; + + if (height > reqHeight || width > reqWidth) { + + // Calculate ratios of height and width to requested height and width + final int heightRatio = Math.round((float) height / (float) reqHeight); + final int widthRatio = Math.round((float) width / (float) reqWidth); + + // Choose the smallest ratio as inSampleSize value, this will guarantee + // a final image with both dimensions larger than or equal to the + // requested height and width. + inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; + } + + return inSampleSize; } } |