Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.graphics.Bitmap;

import android.graphics.Matrix;

public class Main {
    public static Bitmap createCroppedScaleBitmap(Bitmap src, int reqWidth, int reqHeight) {
        final int bWidth = src.getWidth();
        final int bHeight = src.getHeight();
        Matrix matrix = new Matrix();
        int maxSize = Math.max(reqHeight, reqWidth);
        float scaleX;
        if (bWidth * bHeight < reqWidth * reqHeight)
            scaleX = 0;
        else {
            if (bWidth > bHeight) {
                scaleX = (float) maxSize / bWidth;
            } else
                scaleX = (float) maxSize / bHeight;
        }
        Bitmap sourceBitmap;
        if (scaleX > 0 && scaleX != 1) {
            matrix.setScale(scaleX, scaleX);
            sourceBitmap = Bitmap.createBitmap(src, 0, 0, bWidth, bHeight, matrix, true);
            if (sourceBitmap != src && !src.isRecycled())
                src.recycle();
        } else
            sourceBitmap = src;
        return sourceBitmap;
    }
}