android get screen size via C(android通过C获取屏幕大小)
问题描述
我想通过 c 获得屏幕尺寸.我知道 jni 支持从 c 调用 java.但是有没有人知道另一种方法?我的意思是从低级模块获取屏幕大小而不调用 java.
i want to get screen size via c. i know that jni support calling java from c. but is there any person who knows another method? i mean get screen size from lowlevel modules without calling 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 与我的问题有关
i think /dev/graphics is related to my questions
推荐答案
是的,你可以从/dev/graphics/fb0 获取屏幕分辨率,但是(至少在我的手机上),读取权限仅限于 root 和用户在图形"组中.无论如何,您都可以这样做(为了清楚起见,删除了错误检查):
Yes, you can get the screen resolution from /dev/graphics/fb0, but (at least on my phone), read access is restricted to root and users in the 'graphics' group. At any rate, you can do something like this (error checking removed for clarity):
// ... other standard includes ...
#include <sys/ioctl.h>
#include <linux/fb.h>
//...
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 的一部分,但它似乎确实有效.
I'm not sure if these are considered "public" NDK interfaces, since I don't know if Google considers "Android runs on Linux and uses the Linux Framebuffer interface" to be a part of the public API, but it does appear to work.
如果您确实想确保它是可移植的,您可能应该有一个调用 Java WindowManager API 的 JNI 回退.
If you do want to make sure it's portable, you should probably have a JNI fallback that calls into the Java WindowManager API.
这篇关于android通过C获取屏幕大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:android通过C获取屏幕大小


- Eclipse 的最佳 XML 编辑器 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 获取数字的最后一位 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 转换 ldap 日期 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01