This method is used to create a sample size for a bitmap given the required size and the Options class for the bitmap. - Android Graphics

Android examples for Graphics:Bitmap Sample Size

Description

This method is used to create a sample size for a bitmap given the required size and the Options class for the bitmap.

Demo Code

/**//from  w w  w  . j  av  a2  s . com
     * This method is used to create a sample size for a bitmap given the
     * required size and the Options class for the bitmap.
     * 
     * Run this method after first running
     * 
     * <pre>
     * <code>
     * final BitmapFactory.Options foo = new BitmapFactory.Options();
     * foo.inJustDecodeBounds = true; 
     * BitmapFactory.decodeResource(Resources, int, foo);
     * </code>
     * </pre>
     * 
     * Then set the output to <code>foo.inSampleSize</code> and then decode the
     * image.
     * 
     * (If using the same BitmapFactory, remember to change
     * <code>inJustDecodeBounds</code> back to false.)
     * 
     * This method was taken from the Developer tutorial on the android website
     * (licensed under Creative Commons) The original source can be found here:
     * {@link http
     * ://developer.android.com/training/displaying-bitmaps/load-bitmap.html}
     * 
     * @param options
     *            A bitmap options class created with
     * @param reqWidth
     *            The preferred width of the image.
     * @param reqHeight
     *            The preferred height of the image.
     * @return The sample size (to be used set to options.inSampleSize)
     */
//package com.java2s;

import android.graphics.BitmapFactory;

import android.util.Log;

public class Main {
    private static String TAG = "BitmapHelper";

    /**
     * This method is used to create a sample size for a bitmap given the
     * required size and the Options class for the bitmap.
     * 
     * Run this method after first running
     * 
     * <pre>
     * <code>
     * final BitmapFactory.Options foo = new BitmapFactory.Options();
     * foo.inJustDecodeBounds = true; 
     * BitmapFactory.decodeResource(Resources, int, foo);
     * </code>
     * </pre>
     * 
     * Then set the output to <code>foo.inSampleSize</code> and then decode the
     * image.
     * 
     * (If using the same BitmapFactory, remember to change
     * <code>inJustDecodeBounds</code> back to false.)
     * 
     * This method was taken from the Developer tutorial on the android website
     * (licensed under Creative Commons) The original source can be found here:
     * {@link http
     * ://developer.android.com/training/displaying-bitmaps/load-bitmap.html}
     * 
     * @param options
     *            A bitmap options class created with
     * @param reqWidth
     *            The preferred width of the image.
     * @param reqHeight
     *            The preferred height of the image.
     * @return The sample size (to be used set to options.inSampleSize)
     */
    public static int calculateInSampleSize(BitmapFactory.Options options,
            float reqWidth, float reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            // Calculate ratios of height and width to requested height and
            // width
            final int heightRatio = Math.round(height / reqHeight);
            final int widthRatio = Math.round(width / reqWidth);

            // Choose the smallest ratio as inSampleSize value, this will
            // guarantee
            // a final image with both dimensions larger than or equal to the
            // requested height and width.
            inSampleSize = heightRatio < widthRatio ? heightRatio
                    : widthRatio;
        }
        Log.v(TAG, "inSampleSize = " + inSampleSize);
        return inSampleSize;
    }
}

Related Tutorials