Android Utililty Methods Bitmap Resize

List of utility methods to do Bitmap Resize

Description

The list of methods to do Bitmap Resize are organized into topic(s).

Method

BitmapresizeBitmap(Context context, int resId, int w, int h)
resize Bitmap
Bitmap source = decodeFromResource(context.getResources(), resId);
return ThumbnailUtils.extractThumbnail(source, w, h);
BitmapresizeBitmapByScale(Bitmap bitmap, float scale, boolean recycle)
resize Bitmap By Scale
int width = Math.round(bitmap.getWidth() * scale);
int height = Math.round(bitmap.getHeight() * scale);
if (width == bitmap.getWidth() && height == bitmap.getHeight())
    return bitmap;
Bitmap target = Bitmap.createBitmap(width, height,
        getConfig(bitmap));
Canvas canvas = new Canvas(target);
canvas.scale(scale, scale);
...
BitmapresizeDownBySideLength(Bitmap bitmap, int maxLength, boolean recycle)
resize Down By Side Length
int srcWidth = bitmap.getWidth();
int srcHeight = bitmap.getHeight();
float scale = Math.min((float) maxLength / srcWidth,
        (float) maxLength / srcHeight);
if (scale >= 1.0f)
    return bitmap;
return resizeBitmapByScale(bitmap, scale, recycle);
DrawableresizeDrawable(Context context, int resId, int w, int h)
resize Drawable
Bitmap source = resizeBitmap(context, resId, w, h);
return new BitmapDrawable(context.getResources(), source);
BitmapresizePreviewBitmap(Bitmap bm, int width, int height)
resize Preview Bitmap
try {
    if (bm != null) {
        Bitmap resizebm = Bitmap.createScaledBitmap(bm, width,
                height, true);
        return resizebm;
    } else {
        return null;
} catch (OutOfMemoryError e) {
    return null;
BitmapsmallImage(Bitmap bitmap, int Width, int Height)
small Image
int oldWidth = bitmap.getWidth();
int oldHeight = bitmap.getHeight();
float scaleWidth = ((float) Width) / oldWidth;
float scaleHeight = ((float) Height) / oldHeight;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
return Bitmap.createBitmap(bitmap, 0, 0, oldWidth, oldHeight,
        matrix, true);
...
BitmapzoomBitmap(Bitmap bgimage, int newWidth, int newHeight)
zoom Bitmap
int width = bgimage.getWidth();
int height = bgimage.getHeight();
Matrix matrix = new Matrix();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
matrix.postScale(scaleWidth, scaleHeight);
Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, width, height,
        matrix, true);
...
BitmapzoomBitmap(Bitmap bitmap, int width, int height)
zoom Bitmap
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidth = ((float) width / w);
float scaleHeight = ((float) height / h);
matrix.postScale(scaleWidth, scaleHeight);
Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix,
        true);
...
BitmapzoomBitmapAdjustScreen(Activity activity, String imagePath)
zoom Bitmap Adjust Screen
Bitmap bm = null;
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inJustDecodeBounds = true;
bm = BitmapFactory.decodeFile(imagePath, opt);
int picWidth = opt.outWidth;
int picHeight = opt.outHeight;
WindowManager windowManager = activity.getWindowManager();
Display display = windowManager.getDefaultDisplay();
...
BitmapzoomImage(Bitmap bgimage, int newWidth, int newHeight)
zoom Image
int width = bgimage.getWidth();
int height = bgimage.getHeight();
Matrix matrix = new Matrix();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
matrix.postScale(scaleWidth, scaleHeight);
Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, width, height,
        matrix, true);
...