【问题标题】:More Blurred With RenderScript In Android在 Android 中使用 RenderScript 更加模糊
【发布时间】:2016-04-14 21:00:59
【问题描述】:

对于我的项目,我想使用模糊背景。当我使用以下方法时,它会模糊我的背景,但它对我来说不够模糊,我想做更多模糊的背景。我将半径设置为其最大值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;


}

【问题讨论】:

  • 你有什么解决办法吗?

标签: android background android-drawable blur blurry


【解决方案1】:

如果使用 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;
}

【讨论】:

  • 谢谢苗,把BITMAP_SCALE减到0.3f,我的图像模糊了很多。
  • 是的,这与缩小其大小相同。我很好奇您是否比较了降低 BITMAP_SCALE 与 RenderScript Resize 的性能和质量?
  • No Miao,我用 1.f/5.f 调整比例尝试了你的解决方案,它并没有那么模糊。甚至什么都没有改变 :)
  • 哦,这只是一个使用 Intrinsic Resize 的示例,它没有使用 Blur。尝试更改 RESIZE_SCALE 并在调整大小后添加一个 IntrinsicBlur。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-19
  • 1970-01-01
  • 1970-01-01
  • 2017-09-24
  • 1970-01-01
相关资源
最近更新 更多