center Square Scale Bitmap - Android Graphics

Android examples for Graphics:Bitmap Scale

Description

center Square Scale Bitmap

Demo Code


//package com.java2s;

import android.graphics.Bitmap;

public class Main {

    public static Bitmap centerSquareScaleBitmap(Bitmap bitmap,
            int edgeLength) {
        if (null == bitmap || edgeLength <= 0) {
            return null;
        }//from  w  ww  . ja v a2s .  c o m
        Bitmap result = bitmap;
        int widthOrg = bitmap.getWidth();
        int heightOrg = bitmap.getHeight();

        int xTopLeft = (widthOrg - edgeLength) / 2;
        int yTopLeft = (heightOrg - edgeLength) / 2;

        if (xTopLeft == 0 && yTopLeft == 0)
            return result;

        try {
            result = Bitmap.createBitmap(bitmap, xTopLeft, yTopLeft,
                    edgeLength, edgeLength);
            bitmap.recycle();
        } catch (OutOfMemoryError e) {
            return result;
        }

        return result;
    }
}

Related Tutorials