检查页面布局,页面底部图标使用Canvas绘制。根据官方文档,建议检查代码中Canvas绘制的时机,以及页面是否监听了平板横竖屏变化。页面横竖屏切换时,会触发Canvas的onReady事件将画布清空。
监听屏幕方向变化,并重新渲染Canvas画布。简单示例:
import display from '@ohos.display';
@Entry
@Component
struct RotationCanvas {
private context: CanvasRenderingContext2D = new CanvasRenderingContext2D();
aboutToAppear() {
// 同步获取屏幕信息
this.updateCanvasByDisplay();
this.registerRotationListener();
}
// 同步更新画布
private updateCanvasByDisplay() {
try {
this.drawCanvas();
} catch (err) {
console.error(`获取屏幕信息失败: ${err}`);
}
}
// 注册旋转监听
private registerRotationListener() {
display.on('change', () => {
this.context.reset(); // 清空画布
this.updateCanvasByDisplay(); // 同步更新
});
}
// 绘制逻辑保持不变
private drawCanvas() {
this.context.fillStyle = '#D1D1D6';
this.context.fillRect(50, 50, 200, 200);
// 绘制Canvas的业务代码
}
build() {
Column() {
Canvas(this.context)
.width('100%')
.height('80%')
.onReady(() => this.updateCanvasByDisplay()); // 同步初始化
};
}
aboutToDisappear() {
display.off("change");
}
}