File tree Expand file tree Collapse file tree 1 file changed +18
-1
lines changed
cocos/platform/android/java/src/org/cocos2dx/lib Expand file tree Collapse file tree 1 file changed +18
-1
lines changed Original file line number Diff line number Diff line change @@ -546,6 +546,16 @@ public void run() {
546546 }
547547 }
548548
549+ private static int displayMetricsToDPI (DisplayMetrics metrics )
550+ {
551+ if (metrics .xdpi != metrics .ydpi ) {
552+ Log .w (Cocos2dxHelper .TAG , "xdpi != ydpi, use (xdpi + ydpi)/2 instead." );
553+ return (int ) ((metrics .xdpi + metrics .ydpi ) / 2.0 );
554+ } else {
555+ return (int )metrics .xdpi ;
556+ }
557+ }
558+
549559 public static int getDPI ()
550560 {
551561 if (sActivity != null )
@@ -557,8 +567,15 @@ public static int getDPI()
557567 Display d = wm .getDefaultDisplay ();
558568 if (d != null )
559569 {
570+ try {
571+ Method getRealMetrics = d .getClass ().getMethod ("getRealMetrics" , metrics .getClass ());
572+ getRealMetrics .invoke (d , metrics );
573+ return displayMetricsToDPI (metrics );
574+ } catch (Exception e ) {
575+ e .printStackTrace ();
576+ }
560577 d .getMetrics (metrics );
561- return ( int )( metrics . density * 160.0f );
578+ return displayMetricsToDPI ( metrics );
562579 }
563580 }
564581 }
You can’t perform that action at this time.
0 commit comments