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 final int SCALE_TYPE_FIT_XY = 0;
    public static final int SCALE_TYPE_FIT_START = 1;

    /**
     * create scaled bitmap with required width and height
     *
     * @param srcBitmap
     * @param reqWidth
     * @param reqHeight
     * @param recycleOrig
     * @param scaleType
     * @return
     */
    public synchronized static Bitmap createBitmap(Bitmap srcBitmap, int reqWidth, int reqHeight,
            boolean recycleOrig, int scaleType) {
        int bitmapWidth = srcBitmap.getWidth();
        int bitmapHeight = srcBitmap.getHeight();
        if (reqWidth == 0)
            reqWidth = bitmapWidth;
        if (reqHeight == 0)
            reqHeight = bitmapHeight;

        //        final Rect srcRect = new Rect(0, 0, srcBitmap.getWidth(), srcBitmap.getHeight());
        //        final Rect dstRect = new Rect(0, 0, reqWidth, reqHeight);
        float scaleWidth = 1;
        float scaleHeight = 1;
        if (scaleType == SCALE_TYPE_FIT_START) {
            scaleWidth = (reqWidth / bitmapWidth < reqHeight / bitmapHeight)
                    ? (float) reqWidth / (float) bitmapWidth
                    : (float) reqHeight / (float) bitmapHeight;
            scaleHeight = scaleWidth;
        } else if (scaleType == SCALE_TYPE_FIT_XY) {
            scaleWidth = (float) reqWidth / (float) bitmapWidth;
            scaleHeight = (float) reqHeight / (float) bitmapHeight;
        }

        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);

        Bitmap resizedBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, bitmapWidth, bitmapHeight, matrix, true);

        if (recycleOrig) {
            srcBitmap.recycle();
        }

        return resizedBitmap;
    }
}