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

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

问题描述

我试图让一个视图,将有一个背景,不仅透明,而且也将有一个模糊的效果。这样的观点下显得失焦。我希望它看起来像按住电源键后屏幕一样。任何想法?

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?

推荐答案

现在,窗口标志德precated,你得模糊自己。我回答这个在其他地方,但这里是你如何模糊一个​​观点:

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:

现在您可以使用<一个href="http://developer.android.com/reference/android/renderscript/ScriptIntrinsicBlur.html">ScriptIntrinsicBlur从RenderScript库迅速模糊。 这里是如何进入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:

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.要使用启用的摇篮包含这些行到您的摇篮文件(此<一个HREF =htt​​p://stackoverflow.com/a/22976675/1582714>答案),并使用Renderscript从包中的 android.support.v8.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天全站免登陆