我正在使用Java SDK在HarmonyOS中创建一个自定义组件,其中我需要在运行时更改元素颜色。
在Android中,我们有setTint()应用程序接口来在运行时改变可绘制的颜色。
对于ex:
drawable.setTint(Color.BLUE); //Require Api level 21
OR
DrawableCompat.setTint(drawable, Color.BLUE);但是,在HMOS中,我看到没有任何像setTint()或setColor()这样的api来改变元素的颜色。
发布于 2021-07-28 17:35:41
您可以使用Element类中的setColorMatrix更改图标的颜色。
public static void setIconColor(Element icon, Color color) {
int iColor = color.getValue();
int red = (iColor & 0xFF0000) / 0xFFFF;
int green = (iColor & 0xFF00) / 0xFF;
int blue = iColor & 0xFF;
float[] matrix = {
0, 0, 0, 0, red,
0, 0, 0, 0, green,
0, 0, 0, 0, blue,
0, 0, 0, 1, 0 };
ColorMatrix colorMatrix = new ColorMatrix();
colorMatrix.setMatrix(matrix);
icon.setColorMatrix(colorMatrix);
}发布于 2021-07-29 09:01:49
您还可以通过参考以下代码来设置元素颜色:
ShapeElement shapeElementWhite = new ShapeElement();
shapeElementWhite.setRgbColor(new RgbColor(255,255,255)); // Set Color Using Numbers
shapeElementWhite.setRgbColor(RgbColor.fromArgbInt(0xADD8E6)); // Set Color Using Hexadecimal
Component component = findComponentById(ResourceTable.Id_buy_train);
component.setBackground(shapeElementWhite);https://stackoverflow.com/questions/68554832
复制相似问题