我想在ImageView中模糊背景。
我有这个,但一切都模糊了,无论是形象还是背景:
...
imageView.setImageBitmap(bitmapWithoutBlur);
Bitmap bitmapBlur = blur(getApplicationContext(), bitmapWithoutBlur);
BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmapBlur);
imageView.setBackgroundDrawable(bitmapDrawable);
...
public static Bitmap blur(Context context, Bitmap bitmap) {
int width = Math.round(bitmap.getWidth());
int height = Math.round(bitmap.getHeight());
Bitmap inputBitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
RenderScript rs = RenderScript.create(context);
ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
theIntrinsic.setRadius(25f);
theIntrinsic.setInput(tmpIn);
theIntrinsic.forEach(tmpOut);
tmpOut.copyTo(outputBitmap);
return outputBitmap;
}
答案 0 :(得分:0)
//In here you have a bitmap without blur
//delete this setting bitmap
//imageView.setImageBitmap(bitmapWithoutBlur);
// Edited lines from here
Bitmap bitmapToBlur = bitmapWithoutBlur;
Bitmap bitmapBlur = blur(getApplicationContext(), bitmapToBlur);
Canvas canvas = new Canvas(BitmapBlur);
canvas.drawBitmap(bitmapToBlur,0f,0f,null);
imageView.setImageBitmap(bitmapToBlur);
在你的代码中查看我写的推荐行。在第二行中,你使两个图像都模糊。只需将bitmapwithoutblur发送到方法,然后在发送之前,使用临时值来进行无模糊版本。
答案 1 :(得分:0)
使用RenderScript库中的ScriptIntrinsicBlur快速模糊。然后按照如何访问RenderScript API http://developer.android.com/guide/topics/renderscript/compute.html#access-rs-apis。以下是我用来模糊视图和位图的课程:
public class BlurBuilder {
private static final float BITMAP_SCALE = 0.4f;
private static final float BLUR_RADIUS = 7.5f;
public static Bitmap blur(View v) {
return blur(v.getContext(), getScreenshot(v));
}
public static Bitmap blur(Context ctx, Bitmap image) {
int width = Math.round(image.getWidth() * BITMAP_SCALE);
int height = Math.round(image.getHeight() * 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;
}
private static Bitmap getScreenshot(View v) {
Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.draw(c);
return b;
}
}