简体   繁体   English

如何将半透明颜色设置为 Android 可绘制对象

[英]How to set a semi-transparent color to an Android drawable

I have some android drawables as icons.我有一些 android drawables 作为图标。 I'm trying to change the drawable color to a semi-transparent one.我正在尝试将可绘制颜色更改为半透明颜色。

I've tried to play with PorterDuff.Mode a bit.我试着玩一下 PorterDuff.Mode。 It works fine with solid colors, but I want the whole drawable to be transparent then change colors.它适用于实心 colors,但我希望整个可绘制对象是透明的,然后更改 colors。 Here is what I have这是我所拥有的

Drawable file可绘制文件

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24">
<path
    android:fillColor="@android:color/white"
    android:pathData="M19.1,12.9a2.8,2.8 0,0 0,0.1 -0.9,2.8 2.8,0 0,0 -0.1,-0.9l2.1,-1.6a0.7,0.7 0,0 0,0.1 -0.6L19.4,5.5a0.7,0.7 0,0 0,-0.6 -0.2l-2.4,1a6.5,6.5 0,0 0,-1.6 -0.9l-0.4,-2.6a0.5,0.5 0,0 0,-0.5 -0.4H10.1a0.5,0.5 0,0 0,-0.5 0.4L9.3,5.4a5.6,5.6 0,0 0,-1.7 0.9l-2.4,-1a0.4,0.4 0,0 0,-0.5 0.2l-2,3.4c-0.1,0.2 0,0.4 0.2,0.6l2,1.6a2.8,2.8 0,0 0,-0.1 0.9,2.8 2.8,0 0,0 0.1,0.9L2.8,14.5a0.7,0.7 0,0 0,-0.1 0.6l1.9,3.4a0.7,0.7 0,0 0,0.6 0.2l2.4,-1a6.5,6.5 0,0 0,1.6 0.9l0.4,2.6a0.5,0.5 0,0 0,0.5 0.4h3.8a0.5,0.5 0,0 0,0.5 -0.4l0.3,-2.6a5.6,5.6 0,0 0,1.7 -0.9l2.4,1a0.4,0.4 0,0 0,0.5 -0.2l2,-3.4c0.1,-0.2 0,-0.4 -0.2,-0.6ZM12,15.6A3.6,3.6 0,1 1,15.6 12,3.6 3.6,0 0,1 12,15.6Z"/>

Java Java

icon.setColorFilter(Color.parseColor("#41F44336"), PorterDuff.Mode.SRC);

Tried to change drawable color to white/black/fully transparent combined with different color mods (ADD/OVERLY/SRC/DST) with no hope.尝试将可绘制颜色更改为白色/黑色/完全透明并结合不同的颜色模式(添加/过度/SRC/DST),但没有希望。

You can use the drawables setAlpha() method to change the transparency.您可以使用 drawables setAlpha()方法来更改透明度。

Try using setAlpha().尝试使用 setAlpha()。 It takes an int from 0 to 255, 0 being fully transparent, and 255 being fully opaque.它需要一个从 0 到 255 的 int,0 表示完全透明,255 表示完全不透明。

You can use the android:alpha property of the vector tag.您可以使用矢量标签的 android:alpha 属性。

The answer to this after trials and errors is to use white colored icons with PorterDuff.Mode.MULTIPLY经过反复试验后,答案是使用带有PorterDuff.Mode.MULTIPLY的白色图标

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM