I’m facing a peculiar issue with screen pixel density and screen orientation in Cocos Creator on Android, and I’m seeking some guidance from the community.
The Problem:
I’ve been using screen.devicePixelRatio and view.devicePixelRatio to obtain the device pixel ratio (DPR) value of the screen. It’s been working perfectly on iOS, returning accurate values. However, for Android builds, these methods consistently return a DPR of 1, even though my Android device’s screen has a DPR of 2.75. Additionally, I’ve noticed that view.getVisibleSize() doesn’t return the actual screen resolution. For instance, my Android device has a screen resolution of 393px x 873px, but view.getVisibleSize() returns 1080px x 2400px.
Environment:
Cocos Creator Version: 3.7.3
Device: Android (iOS works correctly)
Example:
const payload = JSON.stringify({
width: view.getVisibleSize().width, // Android returns 1080, correct is 393.
density: screen.devicePixelRatio, // Android returns 1, correct is 2.75.
height: screen.devicePixelRatio.height // Android returns 2400, correct is 873.
});
On iOS, it returns the expected values:
{
width: 375, // Correct width of device: 375.
density: 2, // Correct density of device: 2.
height: 667 // Correct height of device: 667.
}
Locking Screen Orientation:
I’m also looking to lock the screen orientation at runtime. While Cocos Creator’s view.setOrientation(macro.ORIENTATION_PORTRAIT) works for setting a fixed orientation, it’s mentioned in the documentation that in native builds, we have to use the native project settings. However, my use case requires runtime orientation locking. Here’s an example of what I’ve attempted:
if (this.isShowingInterstitial) {
if (view.getOrientation() === macro.ORIENTATION_PORTRAIT) {
view.setOrientation(macro.ORIENTATION_PORTRAIT);
}
if (view.getOrientation() === macro.ORIENTATION_LANDSCAPE) {
view.setOrientation(macro.ORIENTATION_LANDSCAPE);
}
} else {
view.setOrientation(macro.ORIENTATION_AUTO);
}
Conclusion:
I would greatly appreciate any insights, tips, or guidance on resolving these issues. If you’ve encountered similar challenges or have any suggestions, please share your expertise. Your assistance would be immensely valuable.