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

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

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

1.控制屏幕常亮

首先導(dǎo)入模塊

import brightness from '@system.brightness';

接下來在項(xiàng)目中使用,首先新建一個(gè)項(xiàng)目

在默認(rèn)生成的代碼里,我們只需要添加生命周期函數(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è)置失敗 錯(cuò)誤碼code:' + code + ', data: ' + data);
      },
    });

就可以實(shí)現(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 : 堅(jiān)果
 * @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è)置失敗 錯(cuò)誤碼code:' + code + ', data: ' + data);
      },
    });
  }
  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(20)
          .fontWeight(FontWeight.Bold).onClick(() => {
          router.back()
        })
      }
      .width('100%')
    }
    .height('100%')
  }
}

完成了屏幕常亮的功能,接下來,我們?cè)俳Y(jié)合進(jìn)度條組件實(shí)現(xiàn)一個(gè)動(dòng)態(tài)調(diào)節(jié)亮度的小功能,

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

需要有兩個(gè)前置知識(shí)

Progress

Progress 組件可以精確的設(shè)置當(dāng)前進(jìn)度條的進(jìn)度,它主要用在有加載進(jìn)度的場(chǎng)景。

Progress定義介紹

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

參數(shù)說明:

value:表示當(dāng)前進(jìn)度,取值范圍[0, 100],當(dāng)超過 100 時(shí)無效。

total:表示進(jìn)度條總進(jìn)度,默認(rèn)值為100。

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

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

接口參數(shù)中的進(jìn)度總長(zhǎng)total,默認(rèn)值100符合進(jìn)度條的絕大部分使用場(chǎng)景,如果有需要,可以設(shè)置為其它正整數(shù)的值,最終進(jìn)度條的完成度取決于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è)備當(dāng)前的屏幕亮度值。設(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è)置當(dāng)前進(jìn)度
          total: 100,                  // 設(shè)置進(jìn)度總量
          type: ProgressType.Linear
        })
          .style({strokeWidth: 18})      // 設(shè)置進(jìn)度條線寬
          .size({width: '100%', height: 40})
      }
      .width('100%')
    }
    .height('100%')
  }
}

參考資料

api官網(wǎng)

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

相關(guān)文章

最新評(píng)論

河西区| 林口县| 历史| 万宁市| 鹤壁市| 会泽县| 吐鲁番市| 荃湾区| 黄龙县| 甘洛县| 合肥市| 调兵山市| 庄浪县| 东山县| 安多县| 辽源市| 儋州市| 广灵县| 涟源市| 金川县| 富锦市| 弋阳县| 淮南市| 南昌县| 石家庄市| 江西省| 金溪县| 辽中县| 郓城县| 济阳县| 太和县| 吉木乃县| 娄烦县| 上林县| 襄城县| 巫溪县| 秭归县| 江阴市| 榆社县| 双峰县| 吴江市|