最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

OpenHarmony實現(xiàn)屏幕亮度動態(tài)調(diào)節(jié)方法詳解

 更新時間:2022年11月23日 09:13:36   作者:堅果的博客  
大家在拿到dayu之后,都吐槽說,會經(jīng)常熄屏,不利于調(diào)試,那么有沒有一種辦法,可以讓app不熄屏呢,答案是有的,今天我們就來揭秘一下,如何控制屏幕亮度

1.控制屏幕常亮

首先導入模塊

import brightness from '@system.brightness';

接下來在項目中使用,首先新建一個項目

在默認生成的代碼里,我們只需要添加生命周期函數(shù)onPageShow,并在里面添加

 brightness.setKeepScreenOn({
      //設(shè)置保持屏幕常亮
      keepScreenOn: true,
      //接口調(diào)用成功的回調(diào)函數(shù)。
      success: function () {
        console.log('設(shè)置成功')
      },
      //接口調(diào)用失敗的回調(diào)函數(shù)。
      fail: function (data, code) {
        console.log('設(shè)置失敗 錯誤碼code:' + code + ', data: ' + data);
      },
    });

就可以實現(xiàn)。

以下是完整代碼:

/*
 * 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 : 屏幕亮度設(shè)置
 */
import router from '@ohos.router';
import brightness from '@system.brightness';
@Entry
@Component
struct brightnessSample {
  @State message: string = '亮度調(diào)節(jié)'
  @State progressValue: number = 0;
  onPageShow(){
    brightness.setKeepScreenOn({
      //設(shè)置保持屏幕常亮
      keepScreenOn: true,
      //接口調(diào)用成功的回調(diào)函數(shù)。
      success: function () {
        console.log('設(shè)置成功')
      },
      //接口調(diào)用失敗的回調(diào)函數(shù)。
      fail: function (data, code) {
        console.log('設(shè)置失敗 錯誤碼code:' + code + ', data: ' + data);
      },
    });
  }
  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(20)
          .fontWeight(FontWeight.Bold).onClick(() => {
          router.back()
        })
      }
      .width('100%')
    }
    .height('100%')
  }
}

完成了屏幕常亮的功能,接下來,我們再結(jié)合進度條組件實現(xiàn)一個動態(tài)調(diào)節(jié)亮度的小功能,

2.動態(tài)調(diào)節(jié)亮度

需要有兩個前置知識

Progress

Progress 組件可以精確的設(shè)置當前進度條的進度,它主要用在有加載進度的場景。

Progress定義介紹

interface ProgressInterface {
  (options: ProgressOptions): ProgressAttribute;
}
declare interface ProgressOptions {
  value: number; // 必須要指定初始進度
  total?: number;
  style?: ProgressStyle
  type?: ProgressType
}

參數(shù)說明:

value:表示當前進度,取值范圍[0, 100],當超過 100 時無效。

total:表示進度條總進度,默認值為100。

type、style:設(shè)置進度條的樣式, style 從 API 8 起使用 type 代替, ProgressType 定義了以下 種樣式:

  • Linear:進度條樣式為條形進度條。
  • Eclipse:進度條樣式為圓形進度條。
  • Ring:環(huán)形進度條。
  • ScaleRing:環(huán)形刻度進度條。
  • Capsule:膠囊樣式進度條。

接口參數(shù)中的進度總長total,默認值100符合進度條的絕大部分使用場景,如果有需要,可以設(shè)置為其它正整數(shù)的值,最終進度條的完成度取決于value/total的結(jié)果,如,將total賦值100,value賦值68,最終結(jié)果就是68/100,也就是68%。

參數(shù)名類型必填說明
valuenumber屏幕亮度,值為1-255之間的整數(shù)。 - 如果值小于等于0,系統(tǒng)按1處理。 - 如果值大于255,系統(tǒng)按255處理。 - 如果值為小數(shù),系統(tǒng)將處理為整數(shù)。例如設(shè)置為8.1,系統(tǒng)按8處理。
success() => void接口調(diào)用成功的回調(diào)函數(shù)。
fail(data: string, code: number) => void接口調(diào)用失敗的回調(diào)函數(shù)。
complete() => void接口調(diào)用結(jié)束的回調(diào)函數(shù)。

首先設(shè)置設(shè)備當前的屏幕亮度值。設(shè)置brightness.setValue

brightness.setKeepScreenOn

setKeepScreenOn(Object): void

設(shè)置屏幕是否保持常亮狀態(tài)。

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
}
參數(shù)名類型必填說明
keepScreenOnboolean是否保持屏幕常亮。
success() => void接口調(diào)用成功的回調(diào)函數(shù)。
fail(data: string, code: number) => void接口調(diào)用失敗的回調(diào)函數(shù)。
complete() => void接口調(diào)用結(jié)束的回調(diào)函數(shù)。

以下是完整源碼

import router from '@ohos.router';
import brightness from '@system.brightness';
@Entry
@Component
struct brightnessSample {
  @State message: string = '亮度調(diào)節(jié)'
  @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,           // 設(shè)置當前進度
          total: 100,                  // 設(shè)置進度總量
          type: ProgressType.Linear
        })
          .style({strokeWidth: 18})      // 設(shè)置進度條線寬
          .size({width: '100%', height: 40})
      }
      .width('100%')
    }
    .height('100%')
  }
}

參考資料

api官網(wǎng)

到此這篇關(guān)于OpenHarmony實現(xiàn)屏幕亮度動態(tài)調(diào)節(jié)方法詳解的文章就介紹到這了,更多相關(guān)OpenHarmony屏幕亮度調(diào)節(jié)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

呈贡县| 古田县| 马公市| 札达县| 桓台县| 甘德县| 延津县| 泸州市| 桑日县| 富宁县| 长宁县| 开江县| 磐安县| 武义县| 博客| 清镇市| 中江县| 邵武市| 龙泉市| 普兰店市| 卫辉市| 金坛市| 乌鲁木齐市| 南和县| 西乌| 牟定县| 陵川县| 渭源县| 米林县| 巨野县| 和平区| 同德县| 乌拉特后旗| 民县| 依兰县| 华蓥市| 伊宁市| 和顺县| 宣恩县| 永寿县| 积石山|