crop Bitmap Image by offset - Android Graphics

Android examples for Graphics:Bitmap Crop

Description

crop Bitmap Image by offset

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 {
    public static Bitmap crop(Bitmap source, int left, int top, int width,
            int height) {
        Bitmap cropBitmap = Bitmap.createBitmap(width, height,
                Config.ARGB_4444);//from  ww w.  j  ava2s  .  c o  m
        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