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获取屏幕大小
基础教程推荐
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01