修改CompassPage.ets,通过点击“旋转屏幕”模拟屏幕横竖屏。
import { common } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
import { CompassView } from '../component/CompassView';
import { CompassController } from '../controller/CompassController';
@Entry
@Component
struct CompassPage {
context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext;
windowClass = (this.context as common.UIAbilityContext).windowStage.getMainWindowSync();
@State angle: number = 0;
@State angleShow: ResourceStr = $r('app.string.N', 0);
private orientation: boolean = false;
private controller: CompassController = new CompassController();
aboutToAppear(): void {
this.windowClass.on('rotationChange', (info: window.RotationChangeInfo) => {
if (info.type === window.RotationChangeType.WINDOW_DID_ROTATE && (info.orientation & 0x01)) {
this.orientation = true;
} else {
this.orientation = false;
}
});
this.controller.setAngleUpdateListener((angle: number) => {
this.angle = angle + (this.orientation ? 90 : 0);
this.angleShow = this.controller.scalAngle(this.angle);
});
this.controller.getAngle();
}
aboutToDisappear(): void {
this.windowClass.off('rotationChange');
}
build() {
Column() {
Row() {
Text(this.angleShow)
.fontSize(45)
.layoutWeight(1)
.textAlign(TextAlign.End);
Row() {
Text($r('app.string.degree', this.angle))
.fontSize(45)
.textAlign(TextAlign.Start);
Text('旋转屏幕')
.fontStyle(FontStyle.Italic)
.onClick(() => {
let orientation = this.windowClass.getPreferredOrientation() !== window.Orientation.LANDSCAPE ?
window.Orientation.LANDSCAPE : window.Orientation.PORTRAIT;
this.windowClass.setPreferredOrientation(orientation);
});
}
.layoutWeight(1);
}.margin({ top: 32 })
.width('100%');
CompassView({
angle: this.angle,
}).width(200).height(200)
.margin({ top: 56 });
}.backgroundColor('#F1F3F5')
.height('100%')
.width('100%')
.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM]);
}
} import { common } from '@kit.AbilityKit';
import { window } from '@kit.ArkUI';
import { CompassView } from '../component/CompassView';
import { CompassController } from '../controller/CompassController';
@Entry
@Component
struct CompassPage {
context: common.UIAbilityContext = this.getUIContext().getHostContext() as common.UIAbilityContext;
windowClass = (this.context as common.UIAbilityContext).windowStage.getMainWindowSync();
@State angle: number = 0;
@State angleShow: ResourceStr = $r('app.string.N', 0);
private controller: CompassController = new CompassController();
aboutToAppear(): void {
this.windowClass.setPreferredOrientation(window.Orientation.PORTRAIT);
this.controller.setAngleUpdateListener((angle: number) => {
this.angle = angle;
this.angleShow = this.controller.scalAngle(this.angle);
});
this.controller.getAngle();
}
build() {
Column() {
Row() {
Text(this.angleShow)
.fontSize(45)
.layoutWeight(1)
.textAlign(TextAlign.End);
Text($r('app.string.degree', this.angle))
.fontSize(45)
.layoutWeight(1)
.textAlign(TextAlign.Start);
}.margin({ top: 32 })
.width('100%');
CompassView({
angle: this.angle,
}).width(270).height(270)
.margin({ top: 56 });
}.backgroundColor('#F1F3F5')
.height('100%')
.width('100%')
.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP, SafeAreaEdge.BOTTOM]);
}
}