This source file includes following definitions.
- JNINamespace
- createBitmap
- decodeDrawableResource
- calculateInSampleSize
- getBitmapFormatForConfig
- getBitmapConfigForFormat
package org.chromium.ui.gfx;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import org.chromium.base.CalledByNative;
import org.chromium.base.JNINamespace;
@JNINamespace("gfx")
public class BitmapHelper {
@CalledByNative
private static Bitmap createBitmap(int width,
int height,
int bitmapFormatValue) {
Bitmap.Config bitmapConfig = getBitmapConfigForFormat(bitmapFormatValue);
return Bitmap.createBitmap(width, height, bitmapConfig);
}
@CalledByNative
private static Bitmap decodeDrawableResource(String name,
int reqWidth,
int reqHeight) {
Resources res = Resources.getSystem();
int resId = res.getIdentifier(name, null, null);
if (resId == 0) return null;
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
return BitmapFactory.decodeResource(res, resId, options);
}
private static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth,
int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
@CalledByNative
private static int getBitmapFormatForConfig(Bitmap.Config bitmapConfig) {
switch (bitmapConfig) {
case ALPHA_8:
return BitmapFormat.FORMAT_ALPHA_8;
case ARGB_4444:
return BitmapFormat.FORMAT_ARGB_4444;
case ARGB_8888:
return BitmapFormat.FORMAT_ARGB_8888;
case RGB_565:
return BitmapFormat.FORMAT_RGB_565;
default:
return BitmapFormat.FORMAT_NO_CONFIG;
}
}
private static Bitmap.Config getBitmapConfigForFormat(int bitmapFormatValue) {
switch (bitmapFormatValue) {
case BitmapFormat.FORMAT_ALPHA_8:
return Bitmap.Config.ALPHA_8;
case BitmapFormat.FORMAT_ARGB_4444:
return Bitmap.Config.ARGB_4444;
case BitmapFormat.FORMAT_RGB_565:
return Bitmap.Config.RGB_565;
case BitmapFormat.FORMAT_ARGB_8888:
default:
return Bitmap.Config.ARGB_8888;
}
}
}