OpenGL surface view scaling for different DPIs
Asked Answered
L

2

8

I have a device with 800x480 res. When I create GLSurfaceView, I get an onSurfaceChanged call with 533x320 (apparently with 1.5 HDPI modifier applied) and surface is upscaled. So when I draw 1 pixel thick line is looks really bad, and I can't have pixel-perfect rendering.

What I want to have is native resolution surface (800x480).

View is created in this manner (like in NDK OpenGL samples), in Activity's onCreate:

    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
            WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
            WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

    view = new MyGLSurfaceView(this);
    setContentView(view);

I don't use any layouts etc.

Loosing answered 4/2, 2012 at 19:52 Comment(2)
Can you post some code, like the layout this GLSurface view is used in? I can't pull answers from thin air.Dossier
Sure, I'll add it to question.Loosing
L
5

I found the solution: when I add

<uses-sdk android:targetSdkVersion="8" android:minSdkVersion="8"/>

to AndroidManifest.xml, I get proper resolution.To be honest, Android is very strange platform for me after few years with iOS...

Solution found here: MonoDroid apps don't use correct density drawables

Loosing answered 8/2, 2012 at 15:44 Comment(1)
Oh well, that's androids device fragmentation for you at work... Anyway, good call on the manifest file.Yahiya
Y
0

The root of the problem seems to lie with the onSurfaceChanged command passing down the (presumably altered) width and height.

There may be nothing wrong with the code constructing the view, but we want to make sure it's constructed at the actual device resolution. Perhaps calling setLayout (int width, int height) explicitly with the desired width and height may get you better mileage...

Obviously, you'd have to determine the actual physical screen size before you do so. And your best bet to get the actual physical pixels is with widthPixels and heightPixels from DisplayMetrics : http://developer.android.com/reference/android/util/DisplayMetrics.html

If that doesn't work, perhaps trying your code on a different device may yield a correct result. In which case it may be a device-dependant issue...

Yahiya answered 7/2, 2012 at 21:44 Comment(1)
I've tried, but surprisingly DisplayMetrics also seem to be wrong. Results for HTC Desire Z: widthPixels 533 heightPixels 320 xdpi 254.000000 ydpi 254.000000 density 1.500000 densDPI 240 (device has native resolution 800x480)Loosing

© 2022 - 2024 — McMap. All rights reserved.