如何将 alpha 通道添加到 xml 中现有的 Android 颜色
How do I add an alpha channel to an existing Android color in xml
我在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>
这可能吗?
您必须将两种不同的颜色设置为起始颜色和结束颜色。
请记住,颜色是这样定义的:#AARRGGBB 代表 Alpha、红色、绿色和蓝色。
应用程序启动后,资源处于只读模式。您无法以正确的方式以编程方式更改它们。
你必须用代码来做。你可以获得这样的颜色,
int color = getResources().getColor(R.color.<the color>);
你可以像这样把它变成ARGB:
int a = Color.alpha(color);
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
现在您可以使用您想要的任何 alpha 值重新创建颜色:
color = Color.argb(<new alpha>, r, g, b);
这当然意味着您需要从代码构建可绘制对象。不太干净,但可能。
您可以在 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>
我在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>
这可能吗?
您必须将两种不同的颜色设置为起始颜色和结束颜色。
请记住,颜色是这样定义的:#AARRGGBB 代表 Alpha、红色、绿色和蓝色。
应用程序启动后,资源处于只读模式。您无法以正确的方式以编程方式更改它们。
你必须用代码来做。你可以获得这样的颜色,
int color = getResources().getColor(R.color.<the color>);
你可以像这样把它变成ARGB:
int a = Color.alpha(color);
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
现在您可以使用您想要的任何 alpha 值重新创建颜色:
color = Color.argb(<new alpha>, r, g, b);
这当然意味着您需要从代码构建可绘制对象。不太干净,但可能。
您可以在 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>