【问题标题】:How to blur the edges of bitmap to fit the square like blur square or iphone inbuilt feature如何模糊位图的边缘以适合正方形,如模糊正方形或 iphone 内置功能
【发布时间】:2015-10-02 13:01:40
【问题描述】:

我想实现下图所示的模糊类型。我有我的位图,我想在侧面实现这种模糊,使其像 iPhone 的内置功能一样成为方形,以适应 WhatsApp 或 Instagram 显示图片。到目前为止,我已经能够使用 Color.Blue 等在这些边缘填充颜色。

欲了解更多信息:How to create a square bitmap from a rectangular bitmap in Android

我想用模糊部分填充该问题图像中的那些白色边。

【问题讨论】:

  • 这不过是:对整个图像进行模糊处理,然后将未模糊但已裁剪的版本(高度)的叠加层添加到其中。
  • Divide et impera 应该是每个开发者都熟悉的概念。简而言之,每个问题都可以变成更小的问题。查找快速模糊教程、裁剪教程和图像叠加教程。把它们放在一起,你就完成了。
  • 1 - 模糊原件(副本)。 2 - 裁剪原件(副本)。 3 - 将裁剪后的图像(居中)覆盖在模糊图像上。 I gave you the algorithm - now you write the code.
  • 然后只使用同一图像的 2 个版本。一大一小。较大的则模糊。就是这样。您可以叠加图像并在 ImageView 中设置结果,或者在 RelativeLayout 中使用 2 个 ImageView。
  • @DeepinderBains 如果您实际查看您发布的图片,您可以清楚地看出这里使用了 2 张图片,因为模糊部分与图片不对齐

标签: java android image-manipulation blur


【解决方案1】:

它是背景中显示的同一图像的模糊版本。以下代码为您提供了相同图像的模糊效果。调整 BLUR_RADIUS 的效果幅度。

public class BlurBuilder {
    private static final float BITMAP_SCALE = 0.4f;
    private static final float BLUR_RADIUS = 20f;

    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;
    }
}

Source

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-17
    • 2017-07-24
    • 1970-01-01
    • 1970-01-01
    • 2013-06-14
    • 2014-10-20
    • 1970-01-01
    相关资源
    最近更新 更多