blur Bitmap with renderscript - Android Graphics

Android examples for Graphics:Bitmap Blur

Description

blur Bitmap with renderscript

Demo Code


//package com.java2s;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.renderscript.Allocation;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
import android.view.View;

public class Main {
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    private static void blur(Context context, Bitmap bkg, View view,
            float radius) {
        Bitmap overlay = Bitmap.createBitmap(view.getMeasuredWidth(),
                view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(overlay);
        RectF rectF = new RectF(view.getLeft(), view.getTop(),
                view.getRight(), view.getBottom());
        canvas.drawBitmap(bkg, null, rectF, null);
        RenderScript rs = RenderScript.create(context);
        Allocation overlayAlloc = Allocation.createFromBitmap(rs, overlay);
        ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs,
                overlayAlloc.getElement());
        blur.setInput(overlayAlloc);//from w  ww  .  j  av a 2 s . c  o  m
        blur.setRadius(radius);
        blur.forEach(overlayAlloc);
        overlayAlloc.copyTo(overlay);
        view.setBackground(new BitmapDrawable(context.getResources(),
                overlay));
        rs.destroy();
    }
}

Related Tutorials