Android examples for User Interface:View
apply Blur to a View
//package com.java2s; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.drawable.BitmapDrawable; import android.renderscript.Allocation; import android.renderscript.RenderScript; import android.renderscript.ScriptIntrinsicBlur; import android.util.Log; import android.view.View; import android.view.ViewTreeObserver; public class Main { public static void applyBlur(final Context context, final View view) { view.getViewTreeObserver().addOnPreDrawListener( new ViewTreeObserver.OnPreDrawListener() { @Override//from w ww . ja v a2 s . c om public boolean onPreDraw() { view.getViewTreeObserver().removeOnPreDrawListener( this); view.buildDrawingCache(); Bitmap bmp = view.getDrawingCache(); blur(context, bmp, view); return true; } }); } @SuppressLint("NewApi") public static void blur(Context context, Bitmap bkg, View view) { long startMs = System.currentTimeMillis(); float radius = 20; Bitmap overlay = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(overlay); canvas.translate(-view.getLeft(), -view.getTop()); canvas.drawBitmap(bkg, 0, 0, null); RenderScript rs = RenderScript.create(context); Allocation overlayAlloc = Allocation.createFromBitmap(rs, overlay); ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, overlayAlloc.getElement()); blur.setInput(overlayAlloc); blur.setRadius(radius); blur.forEach(overlayAlloc); overlayAlloc.copyTo(overlay); view.setBackground(new BitmapDrawable(context.getResources(), overlay)); rs.destroy(); Log.d("", "cost " + (System.currentTimeMillis() - startMs) + "ms"); } }