• OpenHarmony如何控制屏幕亮度


    大家在拿到dayu之后,都吐槽说,会经常熄屏,不利于调试,那么有没有一种办法,可以让app不熄屏呢,答案是有的,今天我们就来揭秘一下,如何控制屏幕亮度

    1.控制屏幕常亮

    首先导入模块

    import brightness from '@system.brightness';
    

    接下来在项目中使用,首先新建一个项目

    在默认生成的代码里,我们只需要添加生命周期函数onPageShow,并在里面添加

     brightness.setKeepScreenOn({
          //设置保持屏幕常亮
          keepScreenOn: true,
          //接口调用成功的回调函数。
          success: function () {
            console.log('设置成功')
          },
          //接口调用失败的回调函数。
          fail: function (data, code) {
            console.log('设置失败 错误码code:' + code + ', data: ' + data);
          },
        });
    

    就可以实现。

    以下是完整代码:

    /*
     * Copyright (c) 2022 JianGuo Device Co., Ltd.
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     *    http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    /**
     * @ProjectName : AbilityDemo
     * @FileName : brightness
     * @Author : 坚果
     * @Time : 2022/9/29 9:36
     * @Description : 屏幕亮度设置
     */
    import router from '@ohos.router';
    import brightness from '@system.brightness';
    @Entry
    @Component
    struct brightnessSample {
      @State message: string = '亮度调节'
      @State progressValue: number = 0;
      onPageShow(){
        brightness.setKeepScreenOn({
          //设置保持屏幕常亮
          keepScreenOn: true,
          //接口调用成功的回调函数。
          success: function () {
            console.log('设置成功')
          },
          //接口调用失败的回调函数。
          fail: function (data, code) {
            console.log('设置失败 错误码code:' + code + ', data: ' + data);
          },
        });
      }
    
      build() {
        Row() {
          Column() {
            Text(this.message)
              .fontSize(20)
              .fontWeight(FontWeight.Bold).onClick(() => {
              router.back()
            })
          
          }
          .width('100%')
        }
        .height('100%')
      }
    }
    

    完成了屏幕常亮的功能,接下来,我们再结合进度条组件实现一个动态调节亮度的小功能,

    2.动态调节亮度

    需要有两个前置知识

    Progress

    Progress 组件可以精确的设置当前进度条的进度,它主要用在有加载进度的场景。

    Progress定义介绍
    interface ProgressInterface {
      (options: ProgressOptions): ProgressAttribute;
    }
    
    declare interface ProgressOptions {
      value: number; // 必须要指定初始进度
      total?: number;
      style?: ProgressStyle
      type?: ProgressType
    }
    

    参数说明:

    • value:表示当前进度,取值范围[0, 100],当超过 100 时无效。
    • total:表示进度条总进度,默认值为100。
    • typestyle:设置进度条的样式, style 从 API 8 起使用 type 代替, ProgressType 定义了以下 种样式:
      • Linear:进度条样式为条形进度条。
      • Eclipse:进度条样式为圆形进度条。
      • Ring:环形进度条。
      • ScaleRing:环形刻度进度条。
      • Capsule:胶囊样式进度条。

    接口参数中的进度总长total,默认值100符合进度条的绝大部分使用场景,如果有需要,可以设置为其它正整数的值,最终进度条的完成度取决于value/total的结果,如,将total赋值100,value赋值68,最终结果就是68/100,也就是68%。

    参数名类型必填说明
    valuenumber屏幕亮度,值为1-255之间的整数。 - 如果值小于等于0,系统按1处理。 - 如果值大于255,系统按255处理。 - 如果值为小数,系统将处理为整数。例如设置为8.1,系统按8处理。
    success() => void接口调用成功的回调函数。
    fail(data: string, code: number) => void接口调用失败的回调函数。
    complete() => void接口调用结束的回调函数。

    首先设置设备当前的屏幕亮度值。设置brightness.setValue

    brightness.setKeepScreenOn

    setKeepScreenOn(Object): void

    设置屏幕是否保持常亮状态。

    static setKeepScreenOn(options?: SetKeepScreenOnOptions): void;
    

    接下来先看定义介绍

    export interface SetKeepScreenOnOptions {
        /**
         * Whether to always keep the screen on.
         */
        keepScreenOn: boolean;
    
        /**
         * Called when the setting is successful.
         */
        success?: () => void;
    
        /**
         * Called when the setting fails.
         */
        fail?: (data: string, code: number) => void;
    
        /**
         * Called when the execution is completed.
         */
        complete?: () => void
    }
    
    参数名类型必填说明
    keepScreenOnboolean是否保持屏幕常亮。
    success() => void接口调用成功的回调函数。
    fail(data: string, code: number) => void接口调用失败的回调函数。
    complete() => void接口调用结束的回调函数。

    以下是完整源码

    import router from '@ohos.router';
    import brightness from '@system.brightness';
    @Entry
    @Component
    struct brightnessSample {
      @State message: string = '亮度调节'
      @State progressValue: number = 0;
    aboutToAppear(){
    
      setInterval(()=>{
        if(this.progressValue < 100){
          this.progressValue += 5
        }
        brightness.setValue({
          value: this.progressValue *2.5,
          success: function(){
            console.log('handling set brightness success.');
          },
          fail: function(data, code){
            console.log('handling set brightness value fail, code:' + code + ', data: ' + data);
          },
        });
      },500)
      }
      build() {
        Row() {
          Column() {
            Text(this.message)
              .fontSize(20)
              .fontWeight(FontWeight.Bold).onClick(() => {
              router.back()
            })
            Progress({
              value: this.progressValue,           // 设置当前进度
              total: 100,                  // 设置进度总量
              type: ProgressType.Linear
            })
              .style({strokeWidth: 18})      // 设置进度条线宽
              .size({width: '100%', height: 40})
          }
          .width('100%')
        }
        .height('100%')
      }
    }
    

    参考资料

    屏幕亮度

  • 相关阅读:
    虹科技术 | 重磅更新!手持式PCAN-Diag FD现可扩展为J1939监控器
    【Element-UI】CUD(增删改)及form 表单验证(附源码)
    数据结构:复杂度分析
    前端构建效率优化之路
    Debezium系列之:深入理解Kafka的消息代理
    罗茨气体流量计的结构设计
    Fashion MNIST与分类算法
    深度学习·理论篇(2023版)·第006篇高维空间下的维度与体积距离的关系:采样和维度+高维空间下体积与距离+中心极限定律与距离分布(深度学习)
    【BOOST C++ 10 时间数据】(2)本地独立时间
    springboot系列(二十四):如何实现Excel文件导出?这你得会 | 超级详细,建议收藏
  • 原文地址:https://blog.csdn.net/qq_39132095/article/details/127106405