使用带 android:alpha="x" 的位图在用作 activity 背景时会产生相反的效果

Using a bitmap with android:alpha="x" does the opposite effect when used as activity background

android 设计有些新意。 我正在尝试将图像添加为 activity 的背景。我希望这张图片部分透明,底部有黑色渐变。

为此,我创建了一个可绘制资源 (openscreenbgg.xml) 并创建了一个 <layer-list>,如下所示:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
     <item>
        <bitmap
            android:src="@drawable/bgnew"
            android:gravity="fill"
            android:alpha="0.4">
        </bitmap>
    </item>
    <item>
        <shape android:shape = "rectangle" >
            <gradient
                android:startColor="#000000"
                android:type="linear"
                android:angle="90"/>
        </shape>
    </item>

bgnew 是镜像名称

我为所需的透明效果设置了 android:alpha,并为渐变添加了 <shape>

这个xml的预览非常完美,正是我想要的:

expected

但是当我使用这个可绘制对象作为我的 activity 的背景时,就像这样:

android:background="@drawable/openscreenbgg"

预览更亮:

Actual

我是否误解了 android:alpha 的用法?让它更透明是否意味着更多 "light" 通过它,所以它更亮?

我怀疑问题是您的主题为所有 windows 提供了默认的白色背景,因此您的 semi-transparent 图片覆盖了浅色而不是深色。

解决此问题的一种方法是更改​​构建背景图片的方式。您可以使用 semi-transparent 黑色遮罩整个图像,而不是在位图上使用 alpha。最终结果应该大致相同,但您不必担心 window 背景 "bleeding through" 图像。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <bitmap
            android:src="@drawable/bgnew"
            android:gravity="fill">
        </bitmap>
    </item>
    <item>
        <shape android:shape="rectangle">
            <gradient
                android:startColor="#FF000000"
                android:endColor="#99000000"
                android:type="linear"
                android:angle="90"/>
        </shape>
    </item>
</layer-list>

或者,您可以将 window 背景设置为深色而不是浅色,这样您就会得到预期的效果。但这可能很难在不影响应用中其他活动的情况下实现。