mobile/ebitenmobileview: Add DeviceScale and use it at EbitenView

Closes #1482
This commit is contained in:
Hajime Hoshi 2021-04-11 02:30:35 +09:00
parent 8a8700b636
commit 2e6e1ed39d
3 changed files with 8 additions and 16 deletions

View File

@ -350,23 +350,10 @@ import android.view.WindowManager;
import {{.JavaPkg}}.ebitenmobileview.Ebitenmobileview; import {{.JavaPkg}}.ebitenmobileview.Ebitenmobileview;
public class EbitenView extends ViewGroup implements InputManager.InputDeviceListener { public class EbitenView extends ViewGroup implements InputManager.InputDeviceListener {
private double getDeviceScale() { private static double pxToDp(double x) {
if (this.deviceScale == 0.0) { return x / Ebitenmobileview.deviceScale();
WindowManager windowManager = (WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
DisplayMetrics displayMetrics = new DisplayMetrics();
display.getRealMetrics(displayMetrics);
this.deviceScale = displayMetrics.density;
}
return this.deviceScale;
} }
private double pxToDp(double x) {
return x / getDeviceScale();
}
private double deviceScale = 0.0;
public EbitenView(Context context) { public EbitenView(Context context) {
super(context); super(context);
initialize(context); initialize(context);

File diff suppressed because one or more lines are too long

View File

@ -30,6 +30,7 @@ import (
"sync/atomic" "sync/atomic"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/internal/devicescale"
"github.com/hajimehoshi/ebiten/v2/internal/restorable" "github.com/hajimehoshi/ebiten/v2/internal/restorable"
"github.com/hajimehoshi/ebiten/v2/internal/uidriver/mobile" "github.com/hajimehoshi/ebiten/v2/internal/uidriver/mobile"
) )
@ -85,3 +86,7 @@ func Resume() {
func OnContextLost() { func OnContextLost() {
restorable.OnContextLost() restorable.OnContextLost()
} }
func DeviceScale() float64 {
return devicescale.GetAt(0, 0)
}