dudul
dudul

Reputation: 43

android: how to get display resolution in native code

Is it possible in android to get display dimensions in the native code?

I see that there are EGL related function:

EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);

surface = eglCreateWindowSurface(display, config, engine->app->window, NULL);

eglQuerySurface(display, surface, EGL_WIDTH, &w);

eglQuerySurface(display, surface, EGL_HEIGHT, &h);

But I need dimension without creating any surface.

Upvotes: 1

Views: 3746

Answers (3)

Tamas
Tamas

Reputation: 3442

You can get the window size while processing the APP_CMD_INIT_WINDOW "app command" like this in your native activity:

static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
    struct engine* engine = (struct engine*)app->userData;
    switch (cmd) {
    case APP_CMD_INIT_WINDOW:
        if (engine->app->window != NULL) {
            int width = ANativeWindow_getWidth(engine->app->window);
            int height = ANativeWindow_getHeight(engine->app->window);
        }

Check out main.c in the native-activity sample NDK project if you want to know how to set up the engine struct & the engine_handle_cmd callback.

Upvotes: 4

user2940187
user2940187

Reputation: 9

Below is the relevant code taken from the Google source for screen capture http://code.google.com/p/androidscreenshot/source/browse/trunk/Binary/screenshot.c?r=3

the vinfo structure contains fields xres and yres.

int framebufferHandle = -1;

struct fb_var_screeninfo vinfo;
framebufferHandle = open("/dev/graphics/fb0", O_RDONLY);
if(framebufferHandle < 0) 
{
    printf("failed to open /dev/graphics/fb0\n");
    // handle error
}

if(ioctl(framebufferHandle, FBIOGET_VSCREENINFO, &vinfo) < 0) 
{
    printf("failed to open ioctl\n");
    // handle error
}
fcntl(framebufferHandle, F_SETFD, FD_CLOEXEC);

Upvotes: 0

chris-tulip
chris-tulip

Reputation: 1830

if you're looking for the pixel values height_pixels and width_pixels are probs what you're looking for. See the developer docs for more info:

http://developer.android.com/reference/android/util/DisplayMetrics.html

Upvotes: -1

Related Questions