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
3 // found in the LICENSE file.
5 package org.chromium.ui.gfx;
7 import android.content.res.Resources;
8 import android.graphics.Bitmap;
9 import android.graphics.BitmapFactory;
11 import org.chromium.base.CalledByNative;
12 import org.chromium.base.JNINamespace;
15 * Helper class to decode and sample down bitmap resources.
18 public class BitmapHelper {
20 private static Bitmap createBitmap(int width,
22 int bitmapFormatValue) {
23 Bitmap.Config bitmapConfig = getBitmapConfigForFormat(bitmapFormatValue);
24 return Bitmap.createBitmap(width, height, bitmapConfig);
28 * Decode and sample down a bitmap resource to the requested width and height.
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 ratio and dimensions.
34 * that are equal to or greater than the requested width and height.
37 private static Bitmap decodeDrawableResource(String name,
40 Resources res = Resources.getSystem();
41 int resId = res.getIdentifier(name, null, null);
42 if (resId == 0) return null;
44 final BitmapFactory.Options options = new BitmapFactory.Options();
45 options.inJustDecodeBounds = true;
46 BitmapFactory.decodeResource(res, resId, options);
48 options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
49 options.inJustDecodeBounds = false;
50 options.inPreferredConfig = Bitmap.Config.ARGB_8888;
51 return BitmapFactory.decodeResource(res, resId, options);
54 // http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
55 private static int calculateInSampleSize(BitmapFactory.Options options,
58 // Raw height and width of image
59 final int height = options.outHeight;
60 final int width = options.outWidth;
63 if (height > reqHeight || width > reqWidth) {
65 // Calculate ratios of height and width to requested height and width
66 final int heightRatio = Math.round((float) height / (float) reqHeight);
67 final int widthRatio = Math.round((float) width / (float) reqWidth);
69 // Choose the smallest ratio as inSampleSize value, this will guarantee
70 // a final image with both dimensions larger than or equal to the
71 // requested height and width.
72 inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
79 * Provides a matching integer constant for the Bitmap.Config value passed.
81 * @param bitmapConfig The Bitmap Configuration value.
82 * @return Matching integer constant for the Bitmap.Config value passed.
85 private static int getBitmapFormatForConfig(Bitmap.Config bitmapConfig) {
86 switch (bitmapConfig) {
88 return BitmapFormat.ALPHA_8;
90 return BitmapFormat.ARGB_4444;
92 return BitmapFormat.ARGB_8888;
94 return BitmapFormat.RGB_565;
96 return BitmapFormat.NO_CONFIG;
101 * Provides a matching Bitmap.Config for the enum config value passed.
103 * @param bitmapFormatValue The Bitmap Configuration enum value.
104 * @return Matching Bitmap.Config for the enum value passed.
106 private static Bitmap.Config getBitmapConfigForFormat(int bitmapFormatValue) {
107 switch (bitmapFormatValue) {
108 case BitmapFormat.ALPHA_8:
109 return Bitmap.Config.ALPHA_8;
110 case BitmapFormat.ARGB_4444:
111 return Bitmap.Config.ARGB_4444;
112 case BitmapFormat.RGB_565:
113 return Bitmap.Config.RGB_565;
114 case BitmapFormat.ARGB_8888:
116 return Bitmap.Config.ARGB_8888;