android通过C获取屏幕大小

我希望通过c获得屏幕尺寸。 我知道jni支持从c调用java。 但是有谁知道另一种方法? 我的意思是从低级模块获取屏幕大小而不调用java。

import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Menu; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.v("getWidth", Integer.toString(getWindowManager().getDefaultDisplay().getWidth())); Log.v("getHeight", Integer.toString(getWindowManager().getDefaultDisplay().getHeight())); } } 

我认为/ dev / graphics与我的问题有关

是的,您可以从/ dev / graphics / fb0获得屏幕分辨率,但(至少在我的手机上),读取权限仅限于“图形”组中的root和用户。 无论如何,您可以执行以下操作(为清晰起见,错误检查已删除):

 // ... other standard includes ... #include  #include  //... struct fb_var_screeninfo fb_var; int fd = open("/dev/graphics/fb0", O_RDONLY); ioctl(fd, FBIOGET_VSCREENINFO, &fb_var); close(fd); // screen size will be in fb_var.xres and fb_var.yres 

我不确定这些是否被视为“公共”NDK接口,因为我不知道Google是否认为“Android在Linux上运行并使用Linux Framebuffer接口”是公共API的一部分,但它确实看起来像工作。

如果你想确保它是可移植的,你可能应该有一个调用Java WindowManager API的JNI回退。

您不需要缓冲区,但如果从java传入Surface,则可以使用ANativeWindow类。

 void Java_com_justinbuser_nativecore_NativeMethods_setSurface(JNIEnv * env, jobject this, jobject surface){ ANativeWindow nativeWindow = ANativeWindow_fromSurface(env, surface); int width = ANativeWindow_getWidth(renderEngine->nativeWindow); int height = ANativeWindow_getHeight(renderEngine->nativeWindow); } 

要获得屏幕分辨率,您必须使用struct ANativeWindow_Buffer

 typedef struct ANativeWindow_Buffer { // The number of pixels that are show horizontally. int32_t width; // The number of pixels that are shown vertically. int32_t height; // The number of *pixels* that a line in the buffer takes in // memory. This may be >= width. int32_t stride; // The format of the buffer. One of WINDOW_FORMAT_* int32_t format; // The actual bits. void* bits; // Do not touch. uint32_t reserved[6]; } ANativeWindow_Buffer; 

要使用它来使用函数ANativeWindow_lock

 /** * Lock the window's next drawing surface for writing. */ int32_t ANativeWindow_lock(ANativeWindow* window, ANativeWindow_Buffer* outBuffer, ARect* inOutDirtyBounds); 

这是文档 。

如果你正在寻找这个function的使用,而不是看看称为native-plasma android-ndk样本。

您还可以在本机活动中处理APP_CMD_INIT_WINDOW “app命令”时获取窗口大小:

 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); } 

如果您想知道如何设置engine结构和engine_handle_cmd回调,请查看本native-activity示例NDK项目中的engine_handle_cmd

这对我有用:

 DisplayMetrics displaymetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); int height = displaymetrics.heightPixels; int width = displaymetrics.widthPixels;