android通过C获取屏幕大小

时间:2023-03-17
本文介绍了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获取屏幕大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:NDK 支持不同的 Product Flavor 下一篇:如何在 Android NDK 上使用 JNI 在 C 和 Java 之间传递

相关文章

最新文章