| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.ui.gfx; | 5 package org.chromium.ui.gfx; |
| 6 | 6 |
| 7 import android.content.res.Resources; | |
| 8 import android.graphics.Bitmap; | 7 import android.graphics.Bitmap; |
| 9 import android.graphics.BitmapFactory; | |
| 10 | 8 |
| 11 import org.chromium.base.CalledByNative; | 9 import org.chromium.base.CalledByNative; |
| 12 import org.chromium.base.JNINamespace; | 10 import org.chromium.base.JNINamespace; |
| 13 | 11 |
| 14 /** | 12 /** |
| 15 * Helper class to decode and sample down bitmap resources. | 13 * Helper class to decode and sample down bitmap resources. |
| 16 */ | 14 */ |
| 17 @JNINamespace("gfx") | 15 @JNINamespace("gfx") |
| 18 public class BitmapHelper { | 16 public class BitmapHelper { |
| 19 @CalledByNative | 17 @CalledByNative |
| 20 private static Bitmap createBitmap(int width, | 18 private static Bitmap createBitmap(int width, |
| 21 int height, | 19 int height, |
| 22 int bitmapFormatValue) { | 20 int bitmapFormatValue) { |
| 23 Bitmap.Config bitmapConfig = getBitmapConfigForFormat(bitmapFormatValue)
; | 21 Bitmap.Config bitmapConfig = getBitmapConfigForFormat(bitmapFormatValue)
; |
| 24 return Bitmap.createBitmap(width, height, bitmapConfig); | 22 return Bitmap.createBitmap(width, height, bitmapConfig); |
| 25 } | 23 } |
| 26 | 24 |
| 27 /** | 25 /** |
| 28 * Decode and sample down a bitmap resource to the requested width and heigh
t. | |
| 29 * | |
| 30 * @param name The resource name of the bitmap to decode. | |
| 31 * @param reqWidth The requested width of the resulting bitmap. | |
| 32 * @param reqHeight The requested height of the resulting bitmap. | |
| 33 * @return A bitmap sampled down from the original with the same aspect rati
o and dimensions. | |
| 34 * that are equal to or greater than the requested width and height. | |
| 35 */ | |
| 36 @CalledByNative | |
| 37 private static Bitmap decodeDrawableResource(String name, | |
| 38 int reqWidth, | |
| 39 int reqHeight) { | |
| 40 Resources res = Resources.getSystem(); | |
| 41 int resId = res.getIdentifier(name, null, null); | |
| 42 if (resId == 0) return null; | |
| 43 | |
| 44 final BitmapFactory.Options options = new BitmapFactory.Options(); | |
| 45 options.inJustDecodeBounds = true; | |
| 46 BitmapFactory.decodeResource(res, resId, options); | |
| 47 | |
| 48 options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeigh
t); | |
| 49 options.inJustDecodeBounds = false; | |
| 50 options.inPreferredConfig = Bitmap.Config.ARGB_8888; | |
| 51 return BitmapFactory.decodeResource(res, resId, options); | |
| 52 } | |
| 53 | |
| 54 // http://developer.android.com/training/displaying-bitmaps/load-bitmap.html | |
| 55 private static int calculateInSampleSize(BitmapFactory.Options options, | |
| 56 int reqWidth, | |
| 57 int reqHeight) { | |
| 58 // Raw height and width of image | |
| 59 final int height = options.outHeight; | |
| 60 final int width = options.outWidth; | |
| 61 int inSampleSize = 1; | |
| 62 | |
| 63 if (height > reqHeight || width > reqWidth) { | |
| 64 | |
| 65 // Calculate ratios of height and width to requested height and widt
h | |
| 66 final int heightRatio = Math.round((float) height / (float) reqHeigh
t); | |
| 67 final int widthRatio = Math.round((float) width / (float) reqWidth); | |
| 68 | |
| 69 // Choose the smallest ratio as inSampleSize value, this will guaran
tee | |
| 70 // a final image with both dimensions larger than or equal to the | |
| 71 // requested height and width. | |
| 72 inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; | |
| 73 } | |
| 74 | |
| 75 return inSampleSize; | |
| 76 } | |
| 77 | |
| 78 /** | |
| 79 * Provides a matching integer constant for the Bitmap.Config value passed. | 26 * Provides a matching integer constant for the Bitmap.Config value passed. |
| 80 * | 27 * |
| 81 * @param bitmapConfig The Bitmap Configuration value. | 28 * @param bitmapConfig The Bitmap Configuration value. |
| 82 * @return Matching integer constant for the Bitmap.Config value passed. | 29 * @return Matching integer constant for the Bitmap.Config value passed. |
| 83 */ | 30 */ |
| 84 @CalledByNative | 31 @CalledByNative |
| 85 private static int getBitmapFormatForConfig(Bitmap.Config bitmapConfig) { | 32 private static int getBitmapFormatForConfig(Bitmap.Config bitmapConfig) { |
| 86 switch (bitmapConfig) { | 33 switch (bitmapConfig) { |
| 87 case ALPHA_8: | 34 case ALPHA_8: |
| 88 return BitmapFormat.ALPHA_8; | 35 return BitmapFormat.ALPHA_8; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 111 return Bitmap.Config.ARGB_4444; | 58 return Bitmap.Config.ARGB_4444; |
| 112 case BitmapFormat.RGB_565: | 59 case BitmapFormat.RGB_565: |
| 113 return Bitmap.Config.RGB_565; | 60 return Bitmap.Config.RGB_565; |
| 114 case BitmapFormat.ARGB_8888: | 61 case BitmapFormat.ARGB_8888: |
| 115 default: | 62 default: |
| 116 return Bitmap.Config.ARGB_8888; | 63 return Bitmap.Config.ARGB_8888; |
| 117 } | 64 } |
| 118 } | 65 } |
| 119 | 66 |
| 120 } | 67 } |
| OLD | NEW |