Android Bitmap Resize createFitCenterBitmap(Bitmap srcBitmap, int dstWidth, int dstHeight, boolean tryRecycleSource)

Here you can find the source of createFitCenterBitmap(Bitmap srcBitmap, int dstWidth, int dstHeight, boolean tryRecycleSource)

Description

create Fit Center Bitmap

Declaration

public static Bitmap createFitCenterBitmap(Bitmap srcBitmap,
            int dstWidth, int dstHeight, boolean tryRecycleSource) 

Method Source Code

//package com.java2s;

import android.graphics.Bitmap;

public class Main {
    public static Bitmap createFitCenterBitmap(Bitmap srcBitmap,
            int dstWidth, int dstHeight, boolean tryRecycleSource) {
        if (srcBitmap == null || dstWidth <= 0 || dstHeight <= 0) {
            return srcBitmap;
        }//from   w ww.j a va  2  s  .  com

        int srcWidth = srcBitmap.getWidth();
        int srcHeight = srcBitmap.getHeight();
        int newWidth = srcWidth;
        int newHeight = srcHeight;

        if (newWidth > dstWidth) {
            newHeight = dstWidth * newHeight / newWidth;
            newWidth = dstWidth;
        }

        if (newHeight > dstHeight) {
            newWidth = dstHeight * newWidth / newHeight;
            newHeight = dstHeight;
        }

        return createFitXYBitmap(srcBitmap, newWidth, newHeight,
                tryRecycleSource);
    }

    public static Bitmap createFitXYBitmap(Bitmap srcBitmap, int dstWidth,
            int dstHeight, boolean tryRecycleSource) {
        if (srcBitmap == null || dstWidth <= 0 || dstHeight <= 0) {
            return srcBitmap;
        }

        Bitmap dstBitmap = srcBitmap;
        try {
            dstBitmap = Bitmap.createScaledBitmap(srcBitmap, dstWidth,
                    dstHeight, true);
            if (dstBitmap != srcBitmap && tryRecycleSource) {
                srcBitmap.recycle();
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return dstBitmap;
    }
}

Related

  1. calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
  2. calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
  3. createFitXYBitmap(Bitmap srcBitmap, int dstWidth, int dstHeight, boolean tryRecycleSource)
  4. getSampledBitmap(String filePath, int reqWidth, int reqHeight)
  5. manageBitmapRotatio(int photoW, int photoH, Bitmap bitMap, int rotation)
  6. readBitmapAutoSize(String filePath, int outWidth, int outHeight)