農林漁牧網

您現在的位置是:首頁 > 畜牧業

autojs顏色漸變效果

2022-07-21由 牙叔教程 發表于 畜牧業

js按鈕怎麼變顏色

牙叔教程 簡單易學

autojs顏色漸變效果

使用場景

顏色漸變

效果展示

00:09

autojs版本

autojs顏色漸變效果

原理

使用androidx自帶的顏色工具類來混合顏色

程式碼講解

1. 佈局

ui。layout(

);

2. 初始化變數

let view;

view = ui。view1;

let color1 = “#ff0000”;

let color2 = “#00ff00”;

view。attr(“bg”, color1);

3. 設定按鈕點選事件

ui。漸變色。click(start);

具體函式

1. 建立handler

let mHandler = new Handler({

handleMessage: function (msg) {

let a = msg。arg1;

if (a <= 500) {

let message = mHandler。obtainMessage();

message。arg1 = a + 1;

mHandler。sendMessageDelayed(message, 1);

let fraction = a / 500;

let color = blendColors(colors。parseColor(color1), colors。parseColor(color2), fraction);

view。attr(“bg”, colors。toString(color));

}

mHandler。handleMessage(msg);

return true;

},

});

2. handler傳送訊息

function start() {

let msg = new Message();

msg。arg1 = 0;

mHandler。sendMessageDelayed(msg, 1);

}

3. 混合顏色

/**

* 顏色漸變

*

* @param color1 起始顏色

* @param color2 終止顏色

* @param ratio 顏色變化頻率 從0-1

* @return 顏色值

*/

function blendColors(color1, color2, ratio) {

return ColorUtils。blendARGB(color1, color2, ratio);

}

注意事項

blendColors的顏色引數範圍, 必須在Integer範圍之內, 不可以用這種

0xffff0000

,可能java裡面能用, 但是js裡面不可以

ui。view。setBackgroundColor(color); 不管用,要用 view。attr(“bg”, colors。toString(color));代替

控制元件的id名字不可以是

view

, 因為ui。view是預設的根控制元件測試程式碼如下

“ui”;

ui。layout(

);

ui。post(function () {

log(ui。view。getWidth());

log(ui。view。getHeight());

ui。view。attr(“bg”, “#ff00ff”);

});

參考文章

1.

Android顏色漸變效果

宣告

部分內容來自網路

autojs顏色漸變效果