#DAYU200#如何动态调节屏幕亮度 原创
润和软件HiHope
 发布于 2022-10-14 09:35
 浏览
 0收藏
大家在拿到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。
 - type、style:设置进度条的样式, 
style从 API 8 起使用type代替,ProgressType定义了以下 种样式:- Linear:进度条样式为条形进度条。
 - Eclipse:进度条样式为圆形进度条。
 - Ring:环形进度条。
 - ScaleRing:环形刻度进度条。
 - Capsule:胶囊样式进度条。
 
 
接口参数中的进度总长total,默认值100符合进度条的绝大部分使用场景,如果有需要,可以设置为其它正整数的值,最终进度条的完成度取决于value/total的结果,如,将total赋值100,value赋值68,最终结果就是68/100,也就是68%。
| 参数名 | 类型 | 必填 | 说明 | 
|---|---|---|---|
| value | number | 是 | 屏幕亮度,值为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
}
| 参数名 | 类型 | 必填 | 说明 | 
|---|---|---|---|
| keepScreenOn | boolean | 是 | 是否保持屏幕常亮。 | 
| 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%')
  }
}
参考资料
©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
  标签 
   
        赞
        2
 
        收藏 
      
 回复
  相关推荐
 




















不能生效