代码如下:
private static float BITMAP_SCALE = 0.1f; private static float BLUR_RADIUS = 12.0f; public static Bitmap Blur(Context ctx, Bitmap image) { int width =(int) Math.Round(image.Width * BITMAP_SCALE); int height =(int) Math.Round(image.Height * BITMAP_SCALE); Bitmap inputBitmap = Bitmap.CreateScaledBitmap(image, width, height, false); Bitmap outputBitmap = Bitmap.CreateBitmap(inputBitmap); RenderScript rs = RenderScript.Create(ctx); ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.Create(rs, Element.U8_4(rs)); Allocation tmpIn = Allocation.CreateFromBitmap(rs, inputBitmap); Allocation tmpOut = Allocation.CreateFromBitmap(rs, outputBitmap); theIntrinsic.SetRadius(BLUR_RADIUS); theIntrinsic.SetInput(tmpIn); theIntrinsic.ForEach(tmpOut); tmpOut.CopyTo(outputBitmap); return outputBitmap; }
var bitmap = ((BitmapDrawable)Resources.GetDrawable(Resource.Drawable.hero)).Bitmap; var blur = Blur(this, bitmap); var viewImage = FindViewById<ImageView>(Resource.Id.imageView1); viewImage.SetImageBitmap(blur);
效果如下: