全局搜索setPreferredOrientation方法和setWindowLayoutFullScreen方法,发现设置了横竖屏后,又异步设置了沉浸式布局,这会导致WebView在横竖屏切换时未完成重绘,出现短暂布局错位。
async changeOrientation(orientation: window.Orientation) {
if (!this.windowClass) {
return;
}
this.windowClass.setPreferredOrientation(orientation); // 设置横竖屏
if (orientation === window.Orientation.LANDSCAPE) {
this.windowClass.setWindowLayoutFullScreen(true);
} else {
this.windowClass.setWindowLayoutFullScreen(false);
}
} import { UIAbility } from '@kit.AbilityKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { window } from '@kit.ArkUI';
const DOMAIN = 0x0000;
export default class EntryAbility extends UIAbility {
onDestroy(): void {
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy');
}
onWindowStageCreate(windowStage: window.WindowStage): void {
// Main window is created, set main page for this ability
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
windowStage.loadContent('pages/Index', (err) => {
if (err.code) {
return;
}
let windowClass: window.Window = windowStage.getMainWindowSync();
AppStorage.setOrCreate('windowClass', windowClass);
hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.');
});
}
onWindowStageDestroy(): void {
// Main window is destroyed, release UI related resources
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
}
onForeground(): void {
// Ability has brought to foreground
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground');
}
onBackground(): void {
// Ability has back to background
hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground');
}
}; import { display, window } from '@kit.ArkUI';
import { fileIo } from '@kit.CoreFileKit';
import fs from '@ohos.file.fs';
import { webview } from '@kit.ArkWeb';
let context: Context | undefined = undefined;
let uicontext: UIContext | undefined = undefined;
class SourceUtil {
screenWidth() {
let dis = display.getDefaultDisplaySync();
if (uicontext) {
return uicontext?.px2vp(dis.width) * 0.85;
}
return 0;
}
chosePicture() {
// 获取应用上下文和资源管理器
if (!context) {
return;
}
let resMgr = context.resourceManager;
// 读取应用资源
try {
const resource = resMgr.getRawFileContentSync('video.mp4');
// 获取沙箱路径
const targetPath = context.filesDir + '/' + 'video.mp4';
// 创建写入流
const file = fs.openSync(targetPath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
// 写入文件
try {
fileIo.writeSync(file.fd, resource.buffer);
} finally {
fileIo.closeSync(file);
}
} catch (error) {
console.info('error==', error);
}
return context.filesDir + '/video.mp4';
}
}
@Entry
@Component
struct Page1 {
private windowClass: window.Window | undefined = AppStorage.get('windowClass');
private cusAspectRatio: number = 0;
webviewController: webview.WebviewController = new webview.WebviewController();
@State sourceUtil: SourceUtil = new SourceUtil();
@State webWidth: string = '85%';
async changeOrientation(orientation: window.Orientation) {
if (!this.windowClass) {
return;
}
if (orientation === window.Orientation.LANDSCAPE) {
this.webWidth = '100%';
await this.windowClass.setWindowLayoutFullScreen(true);
} else {
this.webWidth = '85%';
await this.windowClass.setWindowLayoutFullScreen(false);
}
this.windowClass.setPreferredOrientation(orientation);
}
aboutToAppear(): void {
context = this.getUIContext().getHostContext();
uicontext = this.getUIContext();
}
build() {
RelativeContainer() {
Web({ src: $rawfile('index.html'), controller: this.webviewController })
.fileAccess(true)
.geolocationAccess(false)
.javaScriptProxy({
object: this.sourceUtil,
name: 'picture',
methodList: ['chosePicture', 'screenWidth'],
controller: this.webviewController
})
.alignRules({
center: { anchor: '__container__', align: VerticalAlign.Center },
middle: { anchor: '__container__', align: HorizontalAlign.Center }
})
.height('100%')
.width(this.webWidth)
.onFullScreenEnter(() => {
this.changeOrientation(window.Orientation.LANDSCAPE);
})
.onFullScreenExit(() => {
this.changeOrientation(window.Orientation.PORTRAIT);
});
}
.aspectRatio(this.cusAspectRatio)
.height('100%')
.width('100%');
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</head>
<script>
function takePicture() {
let s = picture.chosePicture();
document.getElementById('myImage').src = "file:///data/storage/el2/base/haps/entry/files/video.mp4";
document.getElementById('myImage').width = picture.screenWidth()
}
document.addEventListener("DOMContentLoaded",takePicture)
</script>
<body>
<video src="" id="myImage" height="240" controls loop autoplay>
</video>
</body>
</html>