resize Bitmap Image by matrix - Android Graphics

Android examples for Graphics:Bitmap Resize

Description

resize Bitmap Image by matrix

Demo Code


//package com.java2s;

import android.content.Context;

import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import android.util.DisplayMetrics;
import android.view.Display;
import android.view.WindowManager;

public class Main {

    @SuppressWarnings("deprecation")
    public static Drawable resizeImage(Bitmap bitmap, Context context) {
        Bitmap BitmapOrg = bitmap;/*  w ww  .j  a v a2 s .  c o  m*/
        int width = BitmapOrg.getWidth();
        int height = BitmapOrg.getHeight();
        int newWidth = getScreenWidth(context) - width;
        int newHeight = getScreenHeight(context) * 4 / 5 - height;

        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // if you want to rotate the Bitmap
        matrix.postRotate(180);
        Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0, width,
                height, matrix, true);
        return new BitmapDrawable(resizedBitmap);
    }

    public static int getScreenWidth(Context context) {
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        DisplayMetrics screenSize = new DisplayMetrics();
        display.getMetrics(screenSize);
        return screenSize.widthPixels;
    }

    public static int getScreenHeight(Context context) {
        WindowManager windowManager = (WindowManager) context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        DisplayMetrics screenSize = new DisplayMetrics();
        display.getMetrics(screenSize);
        return screenSize.heightPixels;
    }
}

Related Tutorials