Skip to content

Commit a0df006

Browse files
authored
[bugfix] Android Device::getDPI / v3 (#20509)
* fix dpi * merge catch cases * add log when xdpi != ydpi
1 parent a96ff32 commit a0df006

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)