【问题标题】:Get effect of Blur image with color shade (like Dark Gray) like iOS, but in Android像 iOS 一样获得带有颜色阴影(如深灰色)的模糊图像效果,但在 Android 中
【发布时间】:2017-10-13 07:00:18
【问题描述】:

我怎样才能创建像你在 iOS 中看到的效果;当对话框打开时,背景屏幕也会变得模糊,就像 Android 中的颜色阴影一样。

我已经关注this link 来获得这种效果,但我无法获得这样的正确效果。

对于模糊效果,我截取当前视图并模糊该图像,但我需要像下面演示图像中那样的颜色效果。

我想要的效果如图所示。我怎样才能得到这个效果?

演示图像(带颜色阴影的模糊)

【问题讨论】:

标签: android


【解决方案1】:

使用颜色过滤器。 以下代码摘自Blurry

Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

Canvas canvas = new Canvas(bitmap);

//...omitted...

Paint paint = new Paint();
paint.setFlags(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);

//replace factor.color with your desired color, e.g Color.argb(200,200,200,200)
PorterDuffColorFilter filter =
    new PorterDuffColorFilter(factor.color, PorterDuff.Mode.SRC_ATOP);
paint.setColorFilter(filter);
canvas.drawBitmap(source, 0, 0, paint);

//blur
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
  try {
    bitmap = Blur.rs(context, bitmap, factor.radius);
  } catch (RSRuntimeException e) {
    bitmap = Blur.stack(bitmap, factor.radius, true);
  }
} else {
  bitmap = Blur.stack(bitmap, factor.radius, true);
}

//...omitted...

【讨论】:

  • 但它会为您使用的位图元素着色,而不是模糊的颜色。
【解决方案2】:

使用任何方法来模糊您的图像。您可以找到各种类型的库来执行此操作。

我用过Blur Image View

当图像变得模糊时,您只需添加一行,即

BlurImageView.setColorFilter(Color.argb(155, 0, 0, 0), PorterDuff.Mode.SRC_ATOP);

这将为您的图像视图提供阴影 根据您的需要更改 Color.argb 的参数

【讨论】:

    【解决方案3】:

    在下面使用这个类

    public class BlurBuilder {
        private static final float BITMAP_SCALE = 0.05f;
        private static final float BLUR_RADIUS = 7.5f;
    
        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;
        }
    }
    

    也可以在您需要的地方使用它,如下所示:

    Bitmap blurredBitmap = BlurBuilder.blur(mContext, icn);
    rootlay.setBackgroundDrawable(new BitmapDrawable(getResources(), blurredBitmap));
    

    【讨论】:

    • 这使图像只模糊而不像图像中的颜色阴影(深灰色阴影)
    猜你喜欢
    • 1970-01-01
    • 2018-05-31
    • 2017-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-18
    相关资源
    最近更新 更多