使线性布局半透明和模糊

6
我想要做的是,将一个线性布局设置为半透明模糊的背景。目前,我有一个完全黑色的线性布局,覆盖了一些需要购买钥匙才能查看的信息。然而,我希望它被模糊处理,而不是完全覆盖,因为这会破坏布局。它需要保持在那里,只是模糊和不可读。谢谢您的帮助!

我不确定,你是否尝试将其放置在清单文件中的<activity>标签中?android:theme="@android:style/Theme.Dialog" - April Smith
3个回答

2

我不确定LinearLayout。但是对于你的活动,你可以尝试这个。

getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

并使用setContentView(R.layout.your_layout);方法。


1
是的,那个可以用于窗口,但在布局本身上不行。更明确地说,它是两个并排的线性布局(横向),右侧需要模糊处理,但仍然存在,并且背景不是实心的,因此不能只是覆盖,因为我仍然想显示其中的内容,但却难以辨认。 - Samuel
我找不到申请线性布局的方法。但是我有一个想法。您可以使用窗口进行半透明背景。在需要背景的任何地方都可以应用背景。对于您不想要的部分,请不要应用背景。例如,在您的情况下,您可以为左侧的一个线性布局应用背景,并且不为另一个线性布局应用任何背景。我认为这可能有效。虽然不是正确的方法。 - san
窗口有一个非实心图像作为背景。 - Samuel
所以你的窗口已经有一个背景。你的线性布局位于窗口顶部。在这种情况下,即使您将线性布局设置为半透明,您最终会显示当前窗口而不是之前的窗口。对我来说这毫无意义。 - san
它显示了窗口,而且窗口是半透明的,可以显示它下面的内容,对吧?现在,我想让它显示的所有内容(底部)都模糊,所以我需要使其中一层模糊,或者基本上任何使视图模糊的方法都可以,如果可能的话,可以是其他层。 - Samuel

0

试试GLSurfaceView怎么样:

http://developer.android.com/resources/articles/glsurfaceview.html

在Android SDK中,有一个关于获取半透明表面的示例(app/TranslucentActivity.java),本质上是设置alpha通道:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Create our Preview view and set it as the content of our
    // Activity
    mGLSurfaceView = new GLSurfaceView(this);
    // We want an 8888 pixel format because that's required for
    // a translucent window.
    // And we want a depth buffer.
    mGLSurfaceView.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
    // Tell the cube renderer that we want to render a translucent version
    // of the cube:
    mGLSurfaceView.setRenderer(new CubeRenderer(true));
    // Use a surface format with an Alpha channel:
    mGLSurfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT);
    setContentView(mGLSurfaceView);
}

关于使用 Alpha 通道的其他线程,请参考以下内容:

Alpha 通道模糊

如何将图像模糊和变暗以用作活动背景?

在 Android 上模糊图像

另一个示例是应用程序/TranslucentBlurActivity.java(来自 Android SDK):

public class TranslucentBlurActivity extends Activity {
    /**
     * Initialization of the Activity after it is first created.  Must at least
     * call {@link android.app.Activity#setContentView setContentView()} to
     * describe what is to be displayed in the screen.
     */
    @Override
    protected void onCreate(Bundle icicle) {
        // Be sure to call the super class.
        super.onCreate(icicle);

        // Have the system blur any windows behind this one.
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
                WindowManager.LayoutParams.FLAG_BLUR_BEHIND);

        // See assets/res/any/layout/translucent_background.xml for this
        // view layout definition, which is being set here as
        // the content of our screen.
        setContentView(R.layout.translucent_background);
    }
}

在第二个例子中,标志 FLAG_BLUR_BEHIND 已被弃用。 - Ayoub

0
如果您想使任何视图背景半透明,请使用以下代码。
 android:background="@null" 

对于EditText,它对我有效。据我所知,它应该适用于任何视图。因此,请尝试一下这个。


半透明不等于透明。 - Graham

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