#DAYU200#如何动态调节屏幕亮度

54 篇文章 19 订阅
21 篇文章 2 订阅

大家在拿到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%')
  }
}

参考资料

屏幕亮度大家在拿到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%')
  }
}

参考资料

屏幕亮度

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值