如何模糊布局的背景?

Ana*_*ngh -4 java android

我已经尝试了堆栈溢出的大部分解决方案,但我似乎没有得到模糊效果的正确实现。
请帮帮我

And*_*per 6

试试这个,效果很好。

public Bitmap captureScreenShot(View view) {
/*
 * Creating a Bitmap of view with ARGB_4444.
 * */
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_4444);
    Canvas canvas = new Canvas(bitmap);
    Drawable backgroundDrawable = view.getBackground();
    if (backgroundDrawable != null) {
        backgroundDrawable.draw(canvas);
    } else {
        canvas.drawColor(Color.parseColor("#80000000"));
    }
    view.draw(canvas);
    return bitmap;
}
Run Code Online (Sandbox Code Playgroud)

您可以将任何视图传递给此函数,例如RelativeLayout、LinearLayout、FrameLayout 等。

现在,设置模糊功能。

    public static Bitmap blur(Context context, Bitmap image) {
    float BITMAP_SCALE = 0.4f;
    float BLUR_RADIUS = 7.5f;

    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;
}
Run Code Online (Sandbox Code Playgroud)

感谢 Norman Peitek 提供的模糊功能。您可以点击此链接了解有关此模糊功能的更多信息。

如何使用这个功能?

// relCustomDialog is the Parent object of RelativeLayout.
// relFullScreen is the Main Parent object of RelativeLayout.
relCustomDialog.setBackgroundDrawable(new BitmapDrawable(getResources(), blur
(MainActivity.this, captureScreenShot(relFullScreen))));
Run Code Online (Sandbox Code Playgroud)

您可以点击此链接获取完整的源代码

  • 感谢安迪开发人员......这个解决方案确实帮助我理解了模糊视图的概念 (2认同)

sus*_*vi 5

android中的view有alpha属性,代表不透明度

在 XML 中

android:alpha="0.5" 
Run Code Online (Sandbox Code Playgroud)

以编程方式

yourView.setAlpha(0.5f); 
Run Code Online (Sandbox Code Playgroud)

您也可以根据需要设置背景颜色以供查看

v.setBackgroundColor(0xFF00FF00);
Run Code Online (Sandbox Code Playgroud)