如何将活动背景设为透明并模糊背景

9
当我启动我的应用程序时,它会启动一个Activity,该活动应具有透明的标题栏,并且当前显示在背景中的任何内容应该被模糊处理。

enter image description here

我能够获取透明度。但是我不知道如何模糊背景。例如,如果我从主屏幕启动应用程序,则主屏幕应该可见但被模糊。
我有一个想法,可以使用帧缓冲来获取当前显示的数据,但是如何将其转换为位图,以便我可以使用数据直接绘制图像而无需保存图像。
我也知道我们可以通过按下电源和音量按钮来截屏。有人知道在Android中执行此操作的代码吗?我的应用将具有系统访问权限。
2个回答

6
如何模糊背景?
您可以使用支持库中提供的RenderScript
public class BlurBuilder {
    private static final float BITMAP_SCALE = 0.4f;
    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;
    }
}

请参阅此链接以获取更多详细信息。
或者,您可以使用Blurry
“有人知道在Android中执行此操作的代码吗?”请参见此链接以拍摄应用程序屏幕截图。

6
输入应该采用位图格式。我的问题是,无论我的活动背后是什么,例如主屏幕,都应该变得模糊。我尝试从我的活动根视图获取位图,但这只会返回我的视图的背景(我将其设置为带有图像的透明)。而不是主屏幕。我想要一些类似于WindowManager.LayoutParams.FLAG_BLUR_BEHIND 的东西,它可以使背景中的任何内容都变得模糊。我该怎么做? - VinayChoudhary99

4

我用这个方法给我的编辑框背景添加了模糊效果。你可以根据自己的喜好进行修改,尝试调整不透明度:

<?xml version="1.0" encoding="utf-8"?><!--  res/drawable/rounded_edittext.xml -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:centerColor="#33FFFFFF"
        android:endColor="#33FFFFFF"
        android:gradientRadius="270"
        android:startColor="#33FFFFFF"
        android:type="radial" />
    <corners
        android:bottomLeftRadius="25dp"
        android:bottomRightRadius="25dp"
        android:topLeftRadius="25dp"
        android:topRightRadius="25dp" />
</shape>

作为另一种选择,您可能会对这个这个感兴趣。

还有一种选择是使用一个小的模糊位图并重复它:

    <xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    android:id="@+id/MainLayout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/backrepeat"
    >

接下来需要准备:

    <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/back" 
        android:tileMode="repeat" />

那个<形状>不会模糊任何东西。 - Kebab Krabby

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接