如何使 activity 背景透明并模糊背景

How to make an activity background transparent and blur the background

当我启动我的应用程序时,它会启动一个 Activity,它应该是透明的 header,并且背景中当前显示的任何内容都应该是模糊的。

我能够获得透明度。但我无法弄清楚如何模糊背景。例如,如果我从主屏幕启动应用程序,那么主屏幕应该是可见的但模糊了。

我想使用 Framebuffer 获取当前显示的数据,但如何将其转换为位图,以便在不保存图像和直接使用数据的情况下绘制图像。

我也知道我们可以按电源键和音量键截屏。有谁知道 android 中的代码在哪里?我的应用程序将具有系统访问权限。

我用它来为我的编辑文本背景提供模糊效果,你可以根据自己的喜好修改它,玩弄不透明度:

<?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>

或者,您可能对 this or this

感兴趣

作为另一种选择,您可以使用一个小的模糊位图并重复它:

    <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" />

how to blur the background?

您可以使用支持库

中提供的 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;
    }
}

有关详细信息,请参阅 this link

或者您可以使用 Blurry

Does anyone has an idea where is the code in android to do that?

要获取应用程序屏幕的屏幕截图,请参阅 this link