鸿蒙NEXT版实战开发:UI框架-如何设置屏幕亮度?

往期鸿蒙全套实战精彩文章必看内容:


如何设置屏幕亮度

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) 
  } 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值