1. 程式人生 > 其它 >HarmonyOS鴻蒙動態設定背景漸變色

HarmonyOS鴻蒙動態設定背景漸變色

技術標籤:HarmonyOSapp

Button btn = (Button) findComponentById(ResourceTable.Id_tv1);
//動態設定背景漸變色
ShapeElement shapeElement = new ShapeElement();
//設定漸變的起始結束方向
shapeElement.setGradientOrientation(ShapeElement.Orientation.LEFT_TO_RIGHT);
RgbColor[] rgbColors = new RgbColor[2];
rgbColors[0] = RgbColor.fromArgbInt(0xFF0000E3);
rgbColors[1] = RgbColor.fromArgbInt(0xFFB9B9FF);
//設定漸變色
shapeElement.setRgbColors(rgbColors);
//設定圓角(鴻蒙目前只有同時設定四個角,沒有單獨設定某個角的方法,如果有知道的可以下方評論)
shapeElement.setCornerRadius(20);
//設定到背景上
btn.setBackground(shapeElement);