首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获得模糊图像的效果与颜色阴影(如深灰色)类似iOS,但在安卓

问获得模糊图像的效果与颜色阴影(如深灰色)类似iOS,但在安卓
EN

Stack Overflow用户
提问于 2017-10-13 15:00:19
回答 3查看 1.3K关注 0票数 0

如何创建像你在iOS中看到的效果;当对话框打开时,背景屏幕也会变得模糊,就像安卓中的色调一样。

我已经关注了this link来获得这个效果,但是我不能像这样得到合适的效果。

对于模糊效果,我使用当前视图的屏幕截图并模糊该图像,但我需要一个颜色效果,如下面的演示图像所示。

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

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

​

EN

回答 3

Stack Overflow用户

发布于 2019-03-15 17:16:23

使用ColorFilter。以下代码取自Blurry

代码语言:javascript
复制
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...
票数 0
EN

Stack Overflow用户

发布于 2020-02-26 18:08:54

使用任何方法来模糊您的图像。您可以找到各种类型库来做到这一点。

我用过Blur Image View。

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

代码语言:javascript
复制
BlurImageView.setColorFilter(Color.argb(155, 0, 0, 0), PorterDuff.Mode.SRC_ATOP);

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

票数 0
EN

Stack Overflow用户

发布于 2017-10-13 15:08:46

在类下面使用它

代码语言:javascript
复制
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;
    }
}

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

代码语言:javascript
复制
Bitmap blurredBitmap = BlurBuilder.blur(mContext, icn);
rootlay.setBackgroundDrawable(new BitmapDrawable(getResources(), blurredBitmap));
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46724209

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档