往期鸿蒙全套实战精彩文章必看内容:
如何设置屏幕亮度
setWindowBrightness可以用于设置屏幕亮度,屏幕亮度值,值为0-1之间。1表示最亮。
在EntryAbility.ets中的onWindowStageCreate方法中将WindowStage设置一个AppStorage,参考代码如下:
AppStorage.setOrCreate('windowStage',windowStage);
通过setWindowBrightness可以设置屏幕亮度
import { window } from '@kit.ArkUI'
@Component
struct SettingScreenBrightness {
windowStage: window.WindowStage = AppStorage.get('windowStage') as window.WindowStage;
// 获取主窗口的方式
mainWin: window.Window = this.windowStage.getMainWindowSync();
aboutToAppear(): void {
// 修改brightness即可改变屏幕亮度
let brightness = 1;
this.windowStage = AppStorage.get('windowStage') as window.WindowStage;
// 获取主窗口的方式
this.mainWin = this.windowStage.getMainWindowSync();
// 获取最上层窗口的方式
window.getLastWindow(getContext(this));
try {
this.mainWin.setWindowBrightness(brightness, (err) => {
if (err.code) {
console.error('Failed to set the brightness. Cause: ' + JSON.stringify(err));
return;
}
console.info('Succeeded in setting the brightness.');
});
} catch (exception) {
console.error('Failed to set the brightness. Cause: ' + JSON.stringify(exception));
}
}
build() {
Row() {
Column({ space: 10 }) {
Text('屏幕亮度设置demo')
.fontSize(25)
.margin(20)
.fontColor(0x3399FF)
}.width('100%')
}.height('100%').backgroundColor(Color.White)
}
}