更新时间:2022-01-24 08:37:31浏览次数:542+次
在鸿蒙HarmonyOS应用开发中,如何获取当前是横屏还是竖屏?
网友指点
displayOrientation == AbilityInfo.DisplayOrientation.LANDSCAPE
DisplayOrientation,可以获取当前屏幕显示方向,LANDSCAPE就是横屏,用if判断,有四个值。
public static enum DisplayOrientation {
UNSPECIFIED,
LANDSCAPE,
PORTRAIT,
FOLLOWRECENT;
}
不过经过实际操作,通过这个getDisplayOrientation()函数获取到的值一直为-1。
解答
以下回答供参考:
可以通过onOrientationChanged方法来实现监听手机屏幕旋转状态。
1、在config.json对应的"abilities"中设置"configChanges"属性为"orientation",屏幕方向"orientation"属性为"unspecified"。
"abilities": [
{
"skills": [...],
"configChanges": ["orientation"],
"orientation": "unspecified",
"name": "com.huawei.faqjavademo.MainAbility",
...
},
...
]
2、在对应的AbilitySlice中重写onOrientationChanged方法。
@Override
protected void onOrientationChanged(AbilityInfo.DisplayOrientation displayOrientation) {
HiLog.info(LABEL_LOG,"displayOrientation==" + displayOrientation);
super.onOrientationChanged(displayOrientation);
}
注:以上是监听屏幕旋转状态,并不是获取。
可继续看如下:
Optional<Display> defaultDisplay = DisplayManager.getInstance().getDefaultDisplay(this);
int rotation = defaultDisplay.get().getRotation();
相关资讯