对于我的项目,我想使用模糊背景。当我使用以下方法时,它会模糊我的背景,但对我来说还不够模糊,我想做更多模糊背景。我将半径设置为其最大值25.有人可以帮助我吗?
private static final float BITMAP_SCALE = 0.9f;
private static final float BLUR_RADIUS = 25.0f;
public static Bitmap blur(Context context, 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(context);
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;
}
最佳答案
如果使用 25 的模糊半径仍然不够,一种廉价的模糊方法是先缩小背景图像,然后再放大。
private static final float BITMAP_SCALE = 0.9f;
private static final float RESIZE_SCALE = 1.f/5.f;
private static RenderScript rs;
public static Bitmap blur(Context context, 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);
if (rs == null) {
// Creating a RS context is expensive, better reuse it.
rs = RenderScript.create(context);
}
Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
Type t = Type.createXY(mRS, tmpIn.getElement(), width*RESIZE_SCALE, height*RESIZE_SCALE);
Allocation tmpScratch = Allocation.createTyped(rs, t);
ScriptIntrinsicResize theIntrinsic = ScriptIntrinsicResize.create(rs);
// Resize the original img down.
theIntrinsic.setInput(tmpIn);
theIntrinsic.forEach_bicubic(tmpScratch);
// Resize smaller img up.
theIntrinsic.setInput(tmpScratch);
theIntrinsic.forEach_bicubic(tmpOut);
tmpOut.copyTo(outputBitmap);
return outputBitmap;
}
关于android - Android 中的 RenderScript 更加模糊,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36633916/