如何使 ViewGroup 半透明(不透明)

How to make ViewGroup translucent (NOT transparent)

我找到了关于透明和半透明的区别的描述。这些是重要的部分:

What does translucent mean? If you were writing about a pane of glass, but that pane of glass was frosty, darkly colored, or very dirty, you would use translucent.

What does transparent mean? If you were writing about a pane of glass, and that piece of glass was perfectly clear, you would use transparent. Something that is transparent allows all light to pass through it. Clean air and the windshield of a car are transparent.

如果你想深入了解这一点,请看here

我想在 Android 中制作一个半透明的 ViewGroup。在 video 中,我找到了如何使 Activity 半透明。他们所做的是定义以下样式和颜色:

<color name="transparent_green_color">#8800ff00</color>

<style name="TranslucentGreen" parent="android:Theme.Translucent">
<item name="android:windowBackground">@color/transparent_green_color</item>
</style>

然后在应用程序标签内的清单中,他们将这种新样式添加为主题:

<application
    android:theme="@style/TranslucentGreen"
    ...>
...
</application>

我想要做的是 不是 使 Activity 半透明,而是 ViewGroup 半透明。

我的布局结构如下:

<FrameLayout>
    <RelativeLayout>
        ...
    </RelativeLayout>
    <RelativeLayout>
        ...
    </RelativeLayout>
</FrameLayout>

第二个 RelativeLayout 应该是半透明的,第一个应该通过半透明 RelativeLayout 稍微可见。怎么做?

使用 Blurry 库解决了我的问题。因此,如果第二个 ViewGroup(在我的例子中是第二个 RelativeLayout)必须位于顶部并且第一个必须通过半透明发光,那么您需要调用如下内容:

Blurry.with(context).radius(25).sampling(2).onto(viewGroup);

然后你就得到了想要的效果。要返回旧视图状态 没有 半透明,您可以使用此方法:

Blurry.delete(viewGroup)

在所需的视图上。我喜欢那个图书馆,因为它非常紧凑。


如果您不能使用外部库,您或许应该从 this Medium article. It works with RenderScript and Bitmap class 开始。 Bu 应该可以从 ViewGroups 创建位图。