【问题标题】:How can I add a blur effect to a background in an app?如何在应用程序的背景中添加模糊效果?
【发布时间】:2021-05-17 03:05:55
【问题描述】:

如何在 Android 应用中为背景添加模糊效果?我想知道一些资源的来源和添加此效果的步骤。我知道我必须添加一个过滤器,但我不知道我要在哪里添加它请有人帮助我?我将放置我要更改的部分的布局,如您所见,它具有普通背景,但我想在该部分上放置模糊效果

<?xml version="1.0" encoding="utf-8" ?>

【问题讨论】:

    标签: android filter background drawable blur


    【解决方案1】:

    您可以尝试使用 RenderScript 来模糊图像的 Bitmap:

    public Bitmap blurBitmap(Bitmap bitmap,Context context){
        //Let's create an empty bitmap with the same size of the bitmap we want to blur
        Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        //Instantiate a new Renderscript
        RenderScript rs = RenderScript.create(context);
        //Create an Intrinsic Blur Script using the Renderscript
        ScriptIntrinsicBlur blurScript = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
            blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
            //Create the Allocations (in/out) with the Renderscript and the in/out bitmaps
            Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
            Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
            //Set the radius of the blur
            blurScript.setRadius(25.f);
            //Perform the Renderscript
            blurScript.setInput(allIn);
            blurScript.forEach(allOut);
            //Copy the final bitmap created by the out Allocation to the outBitmap
            allOut.copyTo(outBitmap);
        }
        //recycle the original bitmap
        bitmap.recycle();
        //After finishing everything, we destroy the Renderscript.
        rs.destroy();
        return outBitmap;
    }
    
    public Bitmap drawableToBitmap(Drawable drawable) {
        int width = drawable.getIntrinsicWidth();
        int heigh = drawable.getIntrinsicHeight();
        drawable.setBounds(0, 0, width, heigh);
        Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                : Bitmap.Config.RGB_565;
        Bitmap bitmap = Bitmap.createBitmap(width, heigh, config);
        Canvas canvas = new Canvas(bitmap);
        drawable.draw(canvas);
        return bitmap;
    }
    
     public void blurImage(View imageView, Context context){
        Drawable drawable = imageView.getBackground();
        Bitmap bitmap = drawableToBitmap(drawable);
        Bitmap out = blurBitmap(bitmap,context);
        BitmapDrawable bitmapDrawable = new BitmapDrawable(context.getResources(),
                out);
        imageView.setBackground(bitmapDrawable);
    }
    

    将它与视图 id 一起使用是 Activity 中的视图:

    View view = findViewById(R.id.view);
    blurImage(view,this);   
    

    【讨论】:

      【解决方案2】:

      这里有一些库供您参考:

      1. https://github.com/Manabu-GT/EtsyBlur
      2. https://github.com/Manabu-GT/GlassView

      通常,我们将此功能称为 玻璃视图,因此您也可以 google 一下。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-06
        • 2014-06-02
        • 2017-07-17
        • 1970-01-01
        • 2020-01-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多