get Crop Bitmap By GPU - Android Graphics

Android examples for Graphics:Bitmap Crop

Description

get Crop Bitmap By GPU

Demo Code


//package com.java2s;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;

import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;

import android.graphics.RectF;
import android.graphics.Shader;

public class Main {
    private static Bitmap getCropBitmapByGPU(Bitmap source, RectF cropRectF) {
        Bitmap resultBitmap = Bitmap.createBitmap((int) cropRectF.width(),
                (int) cropRectF.height(), Config.ARGB_8888);
        Canvas cavas = new Canvas(resultBitmap);

        // draw background
        Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
        paint.setColor(Color.WHITE);
        cavas.drawRect(/*w  ww .j ava  2s.  c o  m*/
                new RectF(0, 0, cropRectF.width(), cropRectF.height()),
                paint);

        // draw picture
        Matrix matrix = new Matrix();
        matrix.postTranslate(-cropRectF.left, -cropRectF.top);
        final BitmapShader shader = new BitmapShader(source,
                Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        shader.setLocalMatrix(matrix);
        paint.setShader(shader);
        cavas.drawRect(
                new RectF(0, 0, cropRectF.width(), cropRectF.height()),
                paint);
        if (source != null && !source.isRecycled()) {
            source.recycle();
        }

        return resultBitmap;
    }
}

Related Tutorials