網站首頁 編程語言 正文
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
相關推薦
- 2022-09-27 React?Native?中限制導入某些組件和模塊的方法_React
- 2022-07-09 Android開發中Flutter組件實用技巧_Android
- 2023-08-15 antdv Input組件maxLength屬性設置默認值
- 2022-12-13 深入了解Go的HttpClient超時機制_Golang
- 2022-09-23 React深入了解原理_React
- 2022-09-02 Python?如何實時向文件寫入數據(附代碼)_python
- 2022-04-02 Redis快速實現分布式session的方法詳解_Redis
- 2024-02-16 SpringBoot 全局異常處理
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支