crop Bitmap To Square - Android Graphics

Android examples for Graphics:Bitmap Crop

Description

crop Bitmap To Square

Demo Code


//package com.java2s;

import android.graphics.Bitmap;

public class Main {
    public static Bitmap cropBitmapToSquare(Bitmap bitmap, boolean recycle) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        Bitmap result = null;/*w  w  w. j  av  a2s . co m*/
        if (height > width) {
            result = Bitmap.createBitmap(bitmap, 0, height / 2 - width / 2,
                    width, width);
        } else {
            result = Bitmap.createBitmap(bitmap, width / 2 - height / 2, 0,
                    height, height);
        }
        if (recycle) {
            bitmap.recycle();
        }
        return result;
    }
}

Related Tutorials