首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Android中模糊背景图片

如何在Android中模糊背景图片
EN

Stack Overflow用户
提问于 2015-07-27 05:08:26
回答 9查看 148.7K关注 0票数 65

如下图所示,模糊背景图像的最佳方法是什么?我看到了一些代码和库,但它们已经有几年的历史了,或者像BlurBehind库一样,但它不会产生同样的效果。提前感谢!

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2015-07-27 05:27:08

要做到这一点,最简单的方法是使用库。看一下这个:https://github.com/wasabeef/Blurry

使用该库,您只需执行以下操作:

代码语言:javascript
复制
Blurry.with(context)
  .radius(10)
  .sampling(8)
  .color(Color.argb(66, 255, 255, 0))
  .async()
  .onto(rootView);
票数 58
EN

Stack Overflow用户

发布于 2017-12-26 13:19:58

您可以使用:

代码语言:javascript
复制
Glide.with(getContext()).load(R.mipmap.bg)
     .apply(bitmapTransform(new BlurTransformation(22)))
     .into((ImageView) view.findViewById(R.id.imBg));

这需要在build.gradle文件中添加以下内容:

代码语言:javascript
复制
implementation 'jp.wasabeef:glide-transformations:4.0.0'
票数 17
EN

Stack Overflow用户

发布于 2017-11-13 18:43:03

实现这一目标的最简单方法如下所示:

i)

代码语言:javascript
复制
Glide.with(context.getApplicationContext())
                        .load(Your Path)   
                        .override(15, 15) // (change according to your wish)
                        .error(R.drawable.placeholder)
                        .into(image.score);

否则,您可以遵循下面的代码。

II)

1.创建一个类。(代码如下)

代码语言:javascript
复制
public class BlurTransformation extends BitmapTransformation {

    private RenderScript rs;

    public BlurTransformation(Context context) {
        super( context );

        rs = RenderScript.create( context );
    }

    @Override
    protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
        Bitmap blurredBitmap = toTransform.copy( Bitmap.Config.ARGB_8888, true );

        // Allocate memory for Renderscript to work with
        Allocation input = Allocation.createFromBitmap(
            rs, 
            blurredBitmap, 
            Allocation.MipmapControl.MIPMAP_FULL, 
            Allocation.USAGE_SHARED
        );
        Allocation output = Allocation.createTyped(rs, input.getType());

        // Load up an instance of the specific script that we want to use.
        ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        script.setInput(input);

        // Set the blur radius
        script.setRadius(10);

        // Start the ScriptIntrinisicBlur
        script.forEach(output);

        // Copy the output to the blurred bitmap
        output.copyTo(blurredBitmap);

        toTransform.recycle();

        return blurredBitmap;
    }

    @Override
    public String getId() {
        return "blur";
    }
}

2.使用Glide将图像设置为ImageView。

例如:

代码语言:javascript
复制
Glide.with(this)
     .load(expertViewDetailsModel.expert.image)
     .asBitmap()
     .transform(new BlurTransformation(this))
     .into(ivBackground);
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31641973

复制
相关文章

相似问题

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