crop Center Bitmap Image - Android Graphics

Android examples for Graphics:Bitmap Crop

Description

crop Center Bitmap Image

Demo Code


//package com.java2s;

import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.Paint;

import android.graphics.Bitmap.Config;

public class Main {
    /**//  ww  w  .java 2s . com
     * 
     * @param src
     * @param rate ty le width/height
     * @return
     */
    public static Bitmap cropCenter(Bitmap src, float rate) {
        int width = src.getWidth();
        int height = src.getHeight();
        int newWidth = (int) (height * rate);
        if (newWidth > width) {
            height = (int) (width / rate);
        } else if (newWidth < width) {
            width = newWidth;
        }
        return crop(src, width, height);

        //truong hop width 
    }

    public static Bitmap crop(Bitmap source, int left, int top, int width,
            int height) {
        Bitmap cropBitmap = Bitmap.createBitmap(width, height,
                Config.ARGB_4444);
        Canvas canvas = new Canvas(cropBitmap);
        canvas.drawBitmap(source, -left, -top, new Paint());
        return cropBitmap;
    }

    public static Bitmap crop(Bitmap src, int width, int height) {
        int left = (int) ((src.getWidth() - width) / 2f);
        int top = (int) ((src.getHeight() - height) / 2f);
        return crop(src, left, top, width, height);
    }
}

Related Tutorials