创建模糊的透明背景效果 [英] Create blurry transparent background effect

查看:33
本文介绍了创建模糊的透明背景效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个视图,它的背景不仅是透明的,而且还有模糊效果.这样,下面的视图似乎没有聚焦.我希望它在按住电源按钮后看起来像屏幕一样.有什么想法吗?

I am trying to make a view that will have a background that is not only transparent, but will also have a blur effect. That way the views underneath appear to be out of focus. I want it to look like the screen does after holding down the power button. Any ideas?

推荐答案

既然不推荐使用窗口标志,您必须模糊自己.我在别处回答了这个问题,但这里是如何模糊视图:

Now that the window flag is deprecated, you've got to blur yourself. I answered this elsewhere but here is how you can blur a view:

您现在可以使用 RenderScript 库中的 ScriptIntrinsicBlur 来快速模糊.此处介绍了如何访问 RenderScript API.以下是我用来模糊视图和位图的类:

You can now use ScriptIntrinsicBlur from the RenderScript library to blur quickly. Here is how to access the RenderScript API. The following is a class I made to blur Views and Bitmaps:

import android.support.v8.renderscript.*;

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

    public static Bitmap blur(View v) {
        return blur(v.getContext(), getScreenshot(v));
    }

    public static Bitmap blur(Context ctx, 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(ctx);
        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;
    }

    private static Bitmap getScreenshot(View v) {
        Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        v.draw(c);
        return b;
    }
}

要将其应用于片段,请将以下内容添加到 onCreateView:

To apply this to a fragment, add the following to onCreateView:

final Activity activity = getActivity();
final View content = activity.findViewById(android.R.id.content).getRootView();
if (content.getWidth() > 0) {
    Bitmap image = BlurBuilder.blur(content);
    window.setBackgroundDrawable(new BitmapDrawable(activity.getResources(), image));
} else {
    content.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            Bitmap image = BlurBuilder.blur(content);
            window.setBackgroundDrawable(new BitmapDrawable(activity.getResources(), image));
        }
    });
}

注意:此解决方案要求最小 sdk 为 API 17

NOTE: This solution requires min sdk to be API 17

Renderscript 包含在支持 v8 中,使这个答案能够下降到 api 8.要使用 gradle 启用它,请将这些行包含到您的 gradle 文件中(从这个 answer) 并使用 android.support.v8.renderscript 包中的 Renderscript:

Renderscript is included into support v8 enabling this answer down to api 8. To enable it using gradle include these lines into your gradle file (from this answer) and use Renderscript from package android.support.v8.renderscript:

android {
  ...
  defaultConfig {
    ...
    renderscriptTargetApi *your target api*
    renderscriptSupportModeEnabled true
  }
  ...
}

这篇关于创建模糊的透明背景效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆