cocos2d / cocos2d-x

Cocos2d-x is a suite of open-source, cross-platform, game-development tools utilized by millions of developers across the globe. Its core has evolved to serve as the foundation for Cocos Creator 1.x & 2.x.
https://www.cocos.com/en/cocos2d-x
18.09k stars 7.05k forks source link

getDpi() failure on Android #18463

Open slackmoehrle opened 6 years ago

slackmoehrle commented 6 years ago

From US forums:

I am trying to get the device ppi but it looks like on android it’s wrong.

I tried a nexus 4 which has 318 ppi but when I do getDpi it returns 320. I tried Samsung galaxy s7 and it returned 640 but it’s suppose to be 577.

It seems to work fine on ios…

Also I think it’s miss leading that it’s called getDpi for actually getting the ppi?

bouc0277 commented 6 years ago

It looks like someone else had a similar issue: https://stackoverflow.com/questions/17703661/how-to-find-the-ppi-of-an-android-screen

My guess is that it's something in here https://github.com/cocos2d/cocos2d-x/blob/f2db054edbd674b03dc84569919f5493744af52a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java#L503

Maybe this is an issue with android DisplayMetrics ?

dumganhar commented 6 years ago

It seems it's because we use getMetrics method in getDPI. But since there are some devices which have virtual buttons in the bottom of screen, getMetrics will not include the size in the area of virtual buttons. I searched some articles on the internet, found that there is a new API called getRealMetrics that will include the size of virtual buttons. Yep, we need to modify the implementation of getDPI. I will try to reproduce it on some devices and try to fix it. Thanks for your feedback, @slackmoehrle .

dumganhar commented 6 years ago

@slackmoehrle , could you please add the link of this thread in the Engine forum?

slackmoehrle commented 6 years ago

Here is the link: http://discuss.cocos2d-x.org/t/cocos2dx-device-getdpi-confusion-on-android/39956

Priority10 commented 6 years ago

Ok, I just hope that these issues (this and https://github.com/cocos2d/cocos2d-x/issues/9419) will be closed together.