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;

public class Main {
    public static final int INT_FULL_SCLAE = 0;
    public static final int INT_UN_FULL_SCLAE = 1;
    public static final int INT_NO_SCLAE = 2;

    public static Bitmap scaleBitmap(int idstWidth, int idstHeight, Bitmap srcBitmap, int scaleStyle) {
        if (null == srcBitmap) {
            return null;
        }

        int width = srcBitmap.getWidth();
        int height = srcBitmap.getHeight();

        Bitmap bitmap = null;

        try {
            switch (scaleStyle) {
            case INT_NO_SCLAE: {
                bitmap = srcBitmap;
                break;
            }
            case INT_FULL_SCLAE: {
                bitmap = Bitmap.createScaledBitmap(srcBitmap, idstWidth, idstHeight, true);
                break;
            }
            case INT_UN_FULL_SCLAE: {
                if (width > idstWidth && height > idstHeight) {
                    float scale = 0;
                    float scaleWidth = (float) width / (float) idstWidth;
                    float scaleHeight = (float) height / (float) idstHeight;
                    if (scaleWidth > scaleHeight) {
                        scale = scaleWidth;
                    } else {
                        scale = scaleHeight;
                    }
                    int tmpWidth = (int) ((float) width / scale);
                    int tmpHigth = (int) ((float) height / scale);
                    bitmap = Bitmap.createScaledBitmap(srcBitmap, tmpWidth, tmpHigth, true);
                } else if (width <= idstWidth && height > idstHeight) {
                    int tmpWidth = (int) ((float) idstHeight * (float) width / (float) height);
                    bitmap = Bitmap.createScaledBitmap(srcBitmap, tmpWidth, idstHeight, true);
                } else if (width > idstWidth && height <= idstHeight) {
                    int tmpHeight = (int) ((float) idstWidth * (float) height / (float) width);
                    bitmap = Bitmap.createScaledBitmap(srcBitmap, idstWidth, tmpHeight, true);
                } else {
                    float scale = 0;
                    float scaleWidth = (float) idstWidth / (float) width;
                    float scaleHeight = (float) idstHeight / (float) height;
                    if (scaleWidth > scaleHeight) {
                        scale = scaleHeight;
                    } else {
                        scale = scaleWidth;
                    }
                    int tmpWidth = (int) ((float) width * scale);
                    int tmpHigth = (int) ((float) height * scale);
                    bitmap = Bitmap.createScaledBitmap(srcBitmap, tmpWidth, tmpHigth, true);
                }
                break;
            }
            default: {
                break;
            }
            }
        } catch (OutOfMemoryError e) {
        }

        return bitmap;
    }
}