Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import android.graphics.Bitmap;
import android.graphics.Matrix;

public class Main {
    public static Bitmap resize(final Bitmap b, final int newWidth, final int newHeight) {
        if (b == null) {
            return null;
        }

        int oldWidth = b.getWidth();
        int oldHeight = b.getHeight();
        if (oldWidth == newWidth && oldHeight == newHeight) {
            return b;
        }
        float scaleWidth = ((float) newWidth) / oldWidth;
        float scaleHeight = ((float) newHeight) / oldHeight;
        float scaleFactor = Math.min(scaleWidth, scaleHeight);
        Matrix scale = new Matrix();
        scale.postScale(scaleFactor, scaleFactor);

        Bitmap result = Bitmap.createBitmap(b, 0, 0, oldWidth, oldHeight, scale, false);
        b.recycle();
        return result;
    }
}