開(kāi)源項(xiàng)目 OpenHarmony是每個(gè)人的 OpenHarmony

徐建國(guó)(堅(jiān)果)
江蘇潤(rùn)開(kāi)鴻數(shù)字科技有限公司 生態(tài)技術(shù)專(zhuān)家
前言
在日常開(kāi)發(fā)中,大多APP可能根據(jù)實(shí)際情況直接將APP的界面方向固定,或豎屏或橫屏。但在使用過(guò)程中,我們還是會(huì)遇到橫豎屏切換的功能需求,可能是通過(guò)物理重力感應(yīng)觸發(fā),也有可能是用戶(hù)手動(dòng)觸發(fā)。所以本文主要帶大家了解在OpenAtom OpenHarmony(以下簡(jiǎn)稱(chēng)“OpenHarmony”)應(yīng)用開(kāi)發(fā)的過(guò)程中,如何在Stage模型和FA模型下使用對(duì)應(yīng)的接口去完成橫豎屏的切換。 本文中OpenHarmony版本為3.2 Beta4,API版本為9。開(kāi)發(fā)板為DAYU200。FA模型
FA模型下,setDisplayOrientation和setDisplayOrientation是切換橫豎屏的接口。文檔:
https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-inner-app-context.md#contextsetdisplayorientation7
context.setDisplayOrientation setDisplayOrientation(orientation: bundle.DisplayOrientation, callback: AsyncCallback): void 設(shè)置當(dāng)前能力的顯示方向(callback形式)。 系統(tǒng)能力: SystemCapability.Ability.AbilityRuntime.Core 參數(shù):
?
示例:
import featureAbility from '@ohos.ability.featureAbility';
import bundle from '@ohos.bundle';
//FA模型下獲取context
var context = featureAbility.getContext();
var orientation = bundle.DisplayOrientation.UNSPECIFIED;
context.setDisplayOrientation(orientation, (err) => {
console.info("setDisplayOrientation err: " + JSON.stringify(err));
});
完整代碼
import bundle from '@ohos.bundle';
import featureAbility from '@ohos.ability.featureAbility';
struct Index {
message: string = '橫豎屏切換 '
portrait: boolean = true
build() {
Row() {
Column() {
Text(this.message)
.fontSize(30)
.fontWeight(FontWeight.Bold).onClick(() => {
var context = featureAbility.getContext();
if (this.portrait) {
// 橫屏
var orientation = bundle.DisplayOrientation.LANDSCAPE;
context.setDisplayOrientation(orientation, (err) => {
this.portrait = !this.portrait
console.info("setDisplayOrientation err: " + JSON.stringify(err));
});
} else {
//豎屏
var orientation = bundle.DisplayOrientation.PORTRAIT;
context.setDisplayOrientation(orientation, (err) => {
this.portrait = !this.portrait
console.info("setDisplayOrientation err: " + JSON.stringify(err));
});
}
})
}
.width('100%')
}
.height('100%')
}
}
上面這樣寫(xiě)太亂了,我們可以封裝一下:
import bundle from '@ohos.bundle';
import featureAbility from '@ohos.ability.featureAbility';
struct Index {
message: string = '橫豎屏切換 '
portrait: boolean = true
private changePortrait() {
var context = featureAbility.getContext();
if (this.portrait) {
// 橫屏
var orientation = bundle.DisplayOrientation.LANDSCAPE;
context.setDisplayOrientation(orientation, (err) => {
this.portrait = !this.portrait
console.info("setDisplayOrientation err: " + JSON.stringify(err));
});
} else {
//豎屏
var orientation = bundle.DisplayOrientation.PORTRAIT;
context.setDisplayOrientation(orientation, (err) => {
this.portrait = !this.portrait
console.info("setDisplayOrientation err: " + JSON.stringify(err));
});
}
}
build() {
Row() {
Column() {
Text(this.message)
.fontSize(30)
.fontWeight(FontWeight.Bold).onClick(() => {
this.changePortrait()
})
}
.width('100%')
}
.height('100%')
}
}
Stage模型
從API 9開(kāi)始,可以使用setPreferredOrientation來(lái)切換橫豎屏。文檔:
https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/reference/apis/js-apis-window.md#setpreferredorientation9
在Stage模型中,使用到的主要是Window(窗口)。在設(shè)置橫豎屏切換的時(shí)候,需要先使用getLastWindow()、createWindow()、findWindow()中的任一方法獲取到Window實(shí)例,再通過(guò)此實(shí)例調(diào)用對(duì)應(yīng)的方法,本文使用的是getLastWindow。 Window.getLastWindow getLastWindow(ctx: BaseContext): Promise獲取當(dāng)前應(yīng)用內(nèi)最后顯示的窗口,使用Promise異步回調(diào)。 系統(tǒng)能力: SystemCapability.WindowManager.WindowManager.Core 參數(shù):
?
返回值:
?
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn)窗口錯(cuò)誤碼。
let windowClass = null;
try {
let promise = window.getLastWindow(this.context);
promise.then((data)=> {
windowClass = data;
console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data));
}).catch((err)=>{
console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(err));
});
} catch (exception) {
console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(exception));
}
然后就可以使用setPreferredOrientation屬性。
setPreferredOrientation
setPreferredOrientation(orientation: Orientation): Promise
設(shè)置窗口的顯示方向?qū)傩裕褂肞romise異步回調(diào)。
系統(tǒng)能力:
SystemCapability.WindowManager.WindowManager.Core
參數(shù):
?
返回值:
?
錯(cuò)誤碼:
以下錯(cuò)誤碼的詳細(xì)介紹請(qǐng)參見(jiàn)窗口錯(cuò)誤碼。
let orientation = window.Orientation.AUTO_ROTATION;
try {
let promise = windowClass.setPreferredOrientation(orientation);
promise.then(()=> {
console.info('Succeeded in setting the window orientation.');
}).catch((err)=>{
console.error('Failed to set the window orientation. Cause: ' + JSON.stringify(err));
});
} catch (exception) {
console.error('Failed to set window orientation. Cause: ' + JSON.stringify(exception));
}
完整代碼
importWindowfrom'@ohos.window'
import common from '@ohos.app.ability.common';
struct ArkUIClubTest {
private portrait: boolean = true
build() {
Stack() {
Button("橫豎屏切換")
.onClick(() => {
this.changeOrientation()
})
}
.width('100%')
.height('100%')
}
private changeOrientation() {
let windowClass = null;
//獲取上下文
//var context = getContext(this) as any
// 獲取上下文,使用common模塊
var context = getContext(this) as common.UIAbilityContext;
let promise = Window.getLastWindow(context);
promise.then((data) => {
windowClass = data;
if (this.portrait) {
//切換成橫屏
let orientation = Window.Orientation.LANDSCAPE;
windowClass.setPreferredOrientation(orientation, (err) => {
});
this.portrait = !this.portrait
console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data));
}
else {
//切換成豎屏
let orientation = Window.Orientation.PORTRAIT;
windowClass.setPreferredOrientation(orientation, (err) => {
});
this.portrait = !this.portrait
console.info('Succeeded in obtaining the top window. Data: ' + JSON.stringify(data));
}
}).catch((err) => {
console.error('Failed to obtain the top window. Cause: ' + JSON.stringify(err));
});
}
}
總結(jié)
本文帶大家使用對(duì)應(yīng)的接口,在Stage模型和FA模型下完成了橫豎屏的切換。其中還涉及到了上下文的獲取:Stage模型用(getContext(this) as any),F(xiàn)A模型(featureAbility.getContext()),大家可以在此基礎(chǔ)上利用生命周期的回調(diào),在合適的地方完成對(duì)應(yīng)的操作。
原文標(biāo)題:OpenHarmony如何切換橫豎屏?
文章出處:【微信公眾號(hào):OpenAtom OpenHarmony】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
-
鴻蒙
+關(guān)注
關(guān)注
60文章
2979瀏覽量
46007 -
OpenHarmony
+關(guān)注
關(guān)注
33文章
3960瀏覽量
21173
原文標(biāo)題:OpenHarmony如何切換橫豎屏?
文章出處:【微信號(hào):gh_e4f28cfa3159,微信公眾號(hào):OpenAtom OpenHarmony】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。
發(fā)布評(píng)論請(qǐng)先 登錄
【原創(chuàng)】OpenHarmony系統(tǒng)投屏工具軟件 - OpenHarmony_OHScrcpy使用推薦
【OpenHarmony快速入門(mén)】本期視頻將介紹應(yīng)用開(kāi)發(fā)初學(xué)者如何構(gòu)建一個(gè)簡(jiǎn)單的應(yīng)用。
OpenHarmony年度課題管理辦法
基于RK3568開(kāi)發(fā)板顯示屏調(diào)試適配方法(1)-如何在Uboot界面切換顯示屏
液晶屏 智能顯示模塊有多個(gè)畫(huà)面時(shí)怎么切換到另一個(gè)畫(huà)面?
兩款搭載KaihongOS的開(kāi)鴻開(kāi)發(fā)板被評(píng)為“OpenHarmony 明星開(kāi)發(fā)板”
HarmonyOS折疊屏鏡頭切換應(yīng)用實(shí)踐
泰克示波器MSO58B光標(biāo)橫豎切換操作指南與實(shí)用技巧
OpenHarmony如何切換橫豎屏?
評(píng)論