get Max Scaled Bitmap - Android Graphics

Android examples for Graphics:Bitmap Scale

Description

get Max Scaled Bitmap

Demo Code


//package com.java2s;

import android.graphics.Bitmap;

import android.util.Log;

public class Main {
    private static final int MAX_WIDTH = 720;

    public static Bitmap getMaxScaledBitmap(Bitmap bitmap) {
        if (bitmap != null) {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();

            //            Log.e("jsp", "original bitmap width = " + width);
            //            Log.e("jsp", "original bitmap height = " + height);

            float ratio = height / (float) width;

            if (width > MAX_WIDTH) {
                //                Log.d("jsp", "width > MAX_WIDTH");
                width = MAX_WIDTH;//from w  w  w  .ja v a  2s.  c  o  m
                height = (int) (width * ratio);
                Bitmap newBitmap = getResizeBitmap(bitmap, width, height);
                Log.d("jsp",
                        "original bitmap width = " + newBitmap.getWidth());
                Log.d("jsp",
                        "original bitmap height = " + newBitmap.getHeight());
                bitmap.recycle();
                return newBitmap;
            }
        }

        return bitmap;
    }

    public static Bitmap getResizeBitmap(Bitmap bitmap, int width,
            int height) {
        return bitmap != null ? bitmap.createScaledBitmap(bitmap, width,
                height, true) : null;
    }
}

Related Tutorials