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; | 7 import android.content.res.Resources; |
8 import android.graphics.Bitmap; | 8 import android.graphics.Bitmap; |
9 import android.graphics.BitmapFactory; | 9 import android.graphics.BitmapFactory; |
10 | 10 |
11 import org.chromium.base.CalledByNative; | 11 import org.chromium.base.CalledByNative; |
12 import org.chromium.base.JNINamespace; | 12 import org.chromium.base.JNINamespace; |
13 | 13 |
14 /** | 14 /** |
15 * Helper class to decode and sample down bitmap resources. | 15 * Helper class to decode and sample down bitmap resources. |
16 */ | 16 */ |
17 @JNINamespace("gfx") | 17 @JNINamespace("gfx") |
18 public class BitmapHelper { | 18 public class BitmapHelper { |
19 @CalledByNative | 19 @CalledByNative |
20 private static Bitmap createBitmap(int width, | 20 private static Bitmap createBitmap(int width, |
21 int height, | 21 int height, |
22 int bitmapFormatValue) { | 22 int bitmapFormatValue) { |
23 Bitmap.Config bitmapConfig = getBitmapConfigForFormat(bitmapFormatValue)
; | 23 Bitmap.Config bitmapConfig = getBitmapConfigForFormat(bitmapFormatValue)
; |
24 return Bitmap.createBitmap(width, height, bitmapConfig); | 24 return Bitmap.createBitmap(width, height, bitmapConfig); |
25 } | 25 } |
26 | 26 |
27 /** | 27 /** |
| 28 * Decode a bitmap resource. |
| 29 * |
| 30 * @param name The resource name of the bitmap to decode. |
| 31 * @return The corresponding bitmap. |
| 32 */ |
| 33 @CalledByNative |
| 34 private static Bitmap decodeDrawableResource(String name) { |
| 35 Resources res = Resources.getSystem(); |
| 36 return decodeDrawableResource(res.getIdentifier(name, null, null)); |
| 37 } |
| 38 |
| 39 /** |
| 40 * Decode a bitmap resource. |
| 41 * |
| 42 * @param resId The resource id of the bitmap to decode. |
| 43 * @return The corresponding bitmap. |
| 44 */ |
| 45 @CalledByNative |
| 46 private static Bitmap decodeDrawableResource(int resId) { |
| 47 final BitmapFactory.Options options = new BitmapFactory.Options(); |
| 48 options.inJustDecodeBounds = false; |
| 49 options.inPreferredConfig = Bitmap.Config.ARGB_8888; |
| 50 return BitmapFactory.decodeResource(Resources.getSystem(), resId, option
s); |
| 51 } |
| 52 |
| 53 /** |
28 * Decode and sample down a bitmap resource to the requested width and heigh
t. | 54 * Decode and sample down a bitmap resource to the requested width and heigh
t. |
29 * | 55 * |
30 * @param name The resource name of the bitmap to decode. | 56 * @param name The resource name of the bitmap to decode. |
31 * @param reqWidth The requested width of the resulting bitmap. | 57 * @param reqWidth The requested width of the resulting bitmap. |
32 * @param reqHeight The requested height of the resulting bitmap. | 58 * @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. | 59 * @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. | 60 * that are equal to or greater than the requested width and height. |
35 */ | 61 */ |
36 @CalledByNative | 62 @CalledByNative |
37 private static Bitmap decodeDrawableResource(String name, | 63 private static Bitmap decodeAndDownsampleDrawableResource(String name, |
38 int reqWidth, | 64 int reqWidth, |
39 int reqHeight) { | 65 int reqHeight) { |
40 Resources res = Resources.getSystem(); | 66 Resources res = Resources.getSystem(); |
41 int resId = res.getIdentifier(name, null, null); | 67 int resId = res.getIdentifier(name, null, null); |
42 if (resId == 0) return null; | 68 if (resId == 0) return null; |
43 | 69 |
44 final BitmapFactory.Options options = new BitmapFactory.Options(); | 70 final BitmapFactory.Options options = new BitmapFactory.Options(); |
45 options.inJustDecodeBounds = true; | 71 options.inJustDecodeBounds = true; |
46 BitmapFactory.decodeResource(res, resId, options); | 72 BitmapFactory.decodeResource(res, resId, options); |
47 | 73 |
48 options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeigh
t); | 74 options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeigh
t); |
49 options.inJustDecodeBounds = false; | 75 options.inJustDecodeBounds = false; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 return Bitmap.Config.ARGB_4444; | 137 return Bitmap.Config.ARGB_4444; |
112 case BitmapFormat.FORMAT_RGB_565: | 138 case BitmapFormat.FORMAT_RGB_565: |
113 return Bitmap.Config.RGB_565; | 139 return Bitmap.Config.RGB_565; |
114 case BitmapFormat.FORMAT_ARGB_8888: | 140 case BitmapFormat.FORMAT_ARGB_8888: |
115 default: | 141 default: |
116 return Bitmap.Config.ARGB_8888; | 142 return Bitmap.Config.ARGB_8888; |
117 } | 143 } |
118 } | 144 } |
119 | 145 |
120 } | 146 } |
OLD | NEW |