Calculate Bitmap Options - Android Graphics

Android examples for Graphics:Bitmap Option

Description

Calculate Bitmap Options

Demo Code


//package com.book2s;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;

public class Main {

    public static BitmapFactory.Options calculateOptions(Bitmap bm,
            int reqWidth, int reqHeight) {
        Options op = new Options();
        op.outHeight = bm.getHeight();//from w w  w  . ja  v  a  2  s  . com
        op.outWidth = bm.getWidth();
        op.inJustDecodeBounds = true;
        op.inSampleSize = calculateInSampleSize(op, reqWidth, reqHeight);
        op.inJustDecodeBounds = false;
        return op;
    }

    public static int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {
            final int heightRatio = Math.round((float) height
                    / (float) reqHeight);
            final int widthRatio = Math.round((float) width
                    / (float) reqWidth);
            inSampleSize = heightRatio < widthRatio ? widthRatio
                    : heightRatio;
        }
        return inSampleSize;
    }
}

Related Tutorials