gpt4 book ai didi

android - 如何将 alpha channel 添加到 xml 中的现有 Android 颜色

转载 作者:行者123 更新时间:2023-12-04 23:53:45 25 4
gpt4 key购买 nike

我在 values/colors.xml 中有以下颜色:

<color name="grey_1">#0F0E10</color>

我想在渐变中引用这个颜色:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="linear"
android:angle="-90"
android:startColor="#000F0E10"
android:endColor="#990F0E10"/>
</shape>

但是,这复制了 RGB 颜色定义。理想情况下,我想写这样的东西:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="linear"
android:angle="-90"
android:startColor="alpha(00, @color/grey_1)"
android:endColor="alpha(99, @color/grey_1)"/>
</shape>

或者这个:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="linear"
android:angle="-90"
android:startColor="@color/grey_1"
android:startTransparency="#00"
android:endColor="@color/grey_1"
android:endTransparency="#99"/>
</shape>

这可能吗?

最佳答案

您可以在 API 23 或更高版本中使用 ColorStateList 执行此操作.

来自文档:

Starting with API 23, items may optionally define an android:alpha attribute to modify the base color's opacity. This attribute takes a either floating-point value between 0 and 1 or a theme attribute that resolves as such. The item's overall color is calculated by multiplying by the base color's alpha channel by the alpha value. For example, the following item represents the theme's accent color at 50% opacity:

<item android:state_enabled="false"
android:color="?android:attr/colorAccent"
android:alpha="0.5" />

因此,就我而言,我会这样做:

color/gradient_start_color.xml:

<item android:color="@color/grey_1"
android:alpha="0" />

color/gradient_end_color.xml:

<item android:color="@color/grey_1"
android:alpha="0.6" />

drawable/gradient.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:type="linear"
android:angle="-90"
android:startColor="@color/gradient_start_color"
android:endColor="@color/gradient_end_color" />
</shape>

关于android - 如何将 alpha channel 添加到 xml 中的现有 Android 颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53163354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com