Android Bitmap Crop cropCenter(Bitmap bitmap, boolean recycle)

Here you can find the source of cropCenter(Bitmap bitmap, boolean recycle)

Description

crop Center

Declaration

public static Bitmap cropCenter(Bitmap bitmap, boolean recycle) 

Method Source Code

//package com.java2s;

import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.Paint;

public class Main {
    public static Bitmap cropCenter(Bitmap bitmap, boolean recycle) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        if (width == height)
            return bitmap;
        int size = Math.min(width, height);

        Bitmap target = Bitmap.createBitmap(size, size, getConfig(bitmap));
        Canvas canvas = new Canvas(target);
        canvas.translate((size - width) / 2, (size - height) / 2);
        Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
        canvas.drawBitmap(bitmap, 0, 0, paint);
        if (recycle)
            bitmap.recycle();/* ww  w.ja v  a  2s  .com*/
        return target;
    }

    private static Bitmap.Config getConfig(Bitmap bitmap) {
        Bitmap.Config config = bitmap.getConfig();
        if (config == null) {
            config = Bitmap.Config.ARGB_8888;
        }
        return config;
    }
}

Related

  1. getTrimmedTop(Bitmap img)
  2. getTrimmedTop(Bitmap img, int border)
  3. cutImg(File file, int newWidth, int newHeight)
  4. cutStretchImage(Bitmap image, int xsize, int ysize)
  5. cutImg(Bitmap bitmap, int newWidth, int newHeight)
  6. cropCenterBitmap(Bitmap bitmap, int newWidth, int newHeight)
  7. resizeDownAndCropCenter(Bitmap bitmap, int size, boolean recycle)
  8. resizeDownIfTooBig(Bitmap bitmap, int targetSize, boolean recycle)
  9. resizeDownToPixels(Bitmap bitmap, int targetPixels, boolean recycle)