Android Bitmap Crop createCenterCropBitmap(Bitmap srcBitmap, int dstWidth, int dstHeight, boolean tryRecycleSource)

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

Description

create Center Crop Bitmap

Declaration

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

Method Source Code

//package com.java2s;

import android.graphics.Bitmap;

public class Main {
    public static Bitmap createCenterCropBitmap(Bitmap srcBitmap,
            int dstWidth, int dstHeight, boolean tryRecycleSource) {
        if (srcBitmap == null || dstWidth == 0 || dstHeight == 0) {
            return srcBitmap;
        }/*from  w w  w  .  j  a  v a2 s .c  o m*/
        int srcWidth = srcBitmap.getWidth();
        int srcHeight = srcBitmap.getHeight();
        Bitmap dstBitmap = srcBitmap;
        try {
            if ((dstHeight / dstWidth) - (srcHeight / srcWidth) > 0) {
                int newWidth = (srcHeight * dstWidth) / dstHeight;
                dstBitmap = Bitmap.createBitmap(srcBitmap,
                        (srcWidth - newWidth) / 2, 0, newWidth, srcHeight);
            } else {
                int newHeight = (dstHeight * srcWidth) / dstWidth;
                dstBitmap = Bitmap.createBitmap(srcBitmap, 0,
                        (srcHeight - newHeight) / 2, srcWidth, newHeight);
            }
            if (dstBitmap != srcBitmap && tryRecycleSource) {
                srcBitmap.recycle();
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return dstBitmap;
    }
}

Related

  1. crop(Bitmap bitmap, float newWidth, float newHeight)
  2. crop(Bitmap bitmap, int x, int y, float newWidth, float newHeight)
  3. cropBitmapToSquare(Bitmap bitmap, int squareLength)
  4. cropBitmapToSquare(String bitmapPath, int squareLength)