OpenHarmony實(shí)現(xiàn)屏幕亮度動(dòng)態(tài)調(diào)節(jié)方法詳解
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)度的場景。
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)度總長total,默認(rèn)值100符合進(jìn)度條的絕大部分使用場景,如果有需要,可以設(shè)置為其它正整數(shù)的值,最終進(jìn)度條的完成度取決于value/total的結(jié)果,如,將total賦值100,value賦值68,最終結(jié)果就是68/100,也就是68%。
| 參數(shù)名 | 類型 | 必填 | 說明 |
|---|---|---|---|
| value | number | 是 | 屏幕亮度,值為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ù)名 | 類型 | 必填 | 說明 |
|---|---|---|---|
| keepScreenOn | boolean | 是 | 是否保持屏幕常亮。 |
| 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%')
}
}參考資料
到此這篇關(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)文章
Android跨進(jìn)程拋異常的原理的實(shí)現(xiàn)
這篇文章主要介紹了Android跨進(jìn)程拋異常的原理的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11
android開發(fā)環(huán)境搭建詳解(eclipse + android sdk)
這篇文章主要介紹了android開發(fā)環(huán)境搭建詳解(eclipse + android sdk),需要的朋友可以參考下2014-05-05
Android App中使用AudioManager類來編寫音頻播放器
這篇文章主要介紹了Android App中使用AudioManager類來編寫音樂播放器的方法,文中舉了一個(gè)簡單的例子實(shí)現(xiàn)了基礎(chǔ)的播放暫停和靜音等功能,需要的朋友可以參考下2016-04-04
Android?廣播接收器BroadcastReceiver詳解
Android開發(fā)的四大組件分別是:活動(dòng)(activity),用于表現(xiàn)功能;服務(wù)(service),后臺(tái)運(yùn)行服務(wù),不提供界面呈現(xiàn);廣播接受者(Broadcast Receive),勇于接收廣播;內(nèi)容提供者(Content Provider),支持多個(gè)應(yīng)用中存儲(chǔ)和讀取數(shù)據(jù),相當(dāng)于數(shù)據(jù)庫,本篇著重介紹廣播組件2022-07-07
react native android6+拍照閃退或重啟的解決方案
android 6+權(quán)限使用的時(shí)候需要?jiǎng)討B(tài)申請(qǐng),那么在使用rn的時(shí)候要怎么處理拍照權(quán)限問題呢?本文提供的是一攬子rn操作相冊(cè)、拍照的解決方案,需要的朋友可以參考下2017-11-11
Android編程獲取GPS數(shù)據(jù)的方法詳解
這篇文章主要介紹了Android編程獲取GPS數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了Android地理位置操作的相關(guān)函數(shù)與使用技巧,需要的朋友可以參考下2016-10-10
Android編程使用AlarmManager設(shè)置鬧鐘的方法
這篇文章主要介紹了Android編程使用AlarmManager設(shè)置鬧鐘的方法,結(jié)合具體實(shí)例分析了Android基于AlarmManager實(shí)現(xiàn)鬧鐘功能的設(shè)置、取消、顯示等相關(guān)操作技巧,需要的朋友可以參考下2017-03-03
Android實(shí)現(xiàn)快遞物流時(shí)間軸效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)快遞物流時(shí)間軸效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05

