日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

OpenHarmony實現屏幕亮度動態調節方法詳解_Android

作者:堅果的博客 ? 更新時間: 2022-12-21 編程語言

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%')
  }
}

參考資料

api官網

原文鏈接:https://jianguo.blog.csdn.net/article/details/127106405

欄目分類
最近更新