Creates a centered bitmap of the desired size. - Android Graphics

Android examples for Graphics:Bitmap Size

Description

Creates a centered bitmap of the desired size.

Demo Code


//package com.java2s;

import android.graphics.Bitmap;

import android.graphics.Matrix;

public class Main {
    /**/*  www  .java 2  s  .  com*/
     * Creates a centered bitmap of the desired size.
     * @param source original bitmap source.
     * @param width targeted width.
     * @param height targeted height.
     * @param recycle whether to recycle the source bitmap if it's not returned as is.
     * @return the output bitmap.
     * @source ThumbnailUtils.java in Android Open Source Project
     */
    public static Bitmap extractThumbnail(Bitmap source, int width,
            int height, boolean recycle) {
        if (source == null) {
            return null;
        }
        if ((source.getWidth() == 0) || (source.getHeight() == 0)) {
            return source;
        }

        float scale;
        if (source.getWidth() < source.getHeight()) {
            scale = width / (float) source.getWidth();
        } else {
            scale = height / (float) source.getHeight();
        }

        Matrix matrix = new Matrix();
        matrix.setScale(scale, scale);
        Bitmap thumbnail = transform(matrix, source, width, height, recycle);
        return thumbnail;
    }

    /**
     * Transform source Bitmap to targeted width and height.
     * @source ThumbnailUtils.java in Android Open Source Project
     */
    private static Bitmap transform(Matrix scaler, Bitmap source,
            int targetWidth, int targetHeight, boolean recycle) {
        float bitmapWidthF = source.getWidth();
        float bitmapHeightF = source.getHeight();

        float bitmapAspect = bitmapWidthF / bitmapHeightF;
        float viewAspect = (float) targetWidth / targetHeight;

        if (bitmapAspect > viewAspect) {
            float scale = targetHeight / bitmapHeightF;
            if (scale < .9F || scale > 1F) {
                scaler.setScale(scale, scale);
            } else {
                scaler = null;
            }
        } else {
            float scale = targetWidth / bitmapWidthF;
            if (scale < .9F || scale > 1F) {
                scaler.setScale(scale, scale);
            } else {
                scaler = null;
            }
        }

        Bitmap b1;
        if (scaler != null) {
            // this is used for minithumb and crop, so we want to filter here.
            b1 = Bitmap.createBitmap(source, 0, 0, source.getWidth(),
                    source.getHeight(), scaler, true);
        } else {
            b1 = source;
        }

        if (recycle && b1 != source) {
            source.recycle();
        }

        int dx1 = Math.max(0, b1.getWidth() - targetWidth);
        int dy1 = Math.max(0, b1.getHeight() - targetHeight);

        Bitmap b2 = Bitmap.createBitmap(b1, dx1 / 2, dy1 / 2, targetWidth,
                targetHeight);

        if (b2 != b1) {
            if (recycle || b1 != source) {
                b1.recycle();
            }
        }

        return b2;
    }
}

Related Tutorials