android - Android 中的 RenderScript 更加模糊

标签 android background android-drawable blur blurry

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

相关文章:

android - 从 android 中的字符串数组引用可绘制对象

android - 使用 ViewPagerIndicator 的 FragmentPagerAdapter 不工作

css - 加载 CSS 时背景属性的 URL 消失

CSS背景图片重复问题

ios - css ipad 2 网站内容在背景上滚动固定

android - 如何在 Picasso 中为 resize() 值选择值

android - 平板电脑需要哪些可绘制资源?

c# - "Error: SendFailure (Error writing headers)"

android - 根据选择微调器的哪个元素更改选项卡内容

android - 如果我给出像文件夹名称/图片名称这样的图像路径,它会显示在 Windows 平台上,但不会显示在 xamarin 的 Android 中