有没有办法在不使用 JNI 的情况下本地访问 android DPI?

Har*_*nji 5 android opengl-es dpi android-ndk

我的 android 游戏使用 OpenGL 和 Android NDK。我目前正在通过从我的 Activity/GLSurfaceView 进行 JNI 本机调用传递必要的 dpi 值作为参数。

目前我正在使用:

    // request an instance of DisplayMetrics, say 'dm' from android.
    float densityFactor = dm.density;

    // pass this as a parameter using JNI call
    myCustomNativeMethod(densityFactor); # implementation in a 'game.c' file
Run Code Online (Sandbox Code Playgroud)

我只是想知道是否可以在不使用 JNI 接收它们(参数)的情况下访问 android 环境参数。可能是,导入一些低级 NDK 头文件 (.h) 来帮助我直接与 UI 系统通信或其他什么?

我的意思是:

    #import <android_displayparams.h>
    ...        
    float densityfactor = getDisplayParamDensity(); // <- Is this possible?
Run Code Online (Sandbox Code Playgroud)

注意:这是一个出于好奇的问题,我知道这样的机制可能不是一个好的做法。

Pav*_*lon 4

通过 Java Activity / GLSurfaceView 的设置,传递 DPI 作为参数看起来是最好的解决方案。

如果您使用 NativeActivity,您可以使用以下方法获取DisplayMetrics.密度Dpi

#include <android/configuration.h>
#include <android_native_app_glue.h>
#include <android/native_activity.h>

int32_t getDensityDpi(android_app* app) {
    AConfiguration* config = AConfiguration_new();
    AConfiguration_fromAssetManager(config, app->activity->assetManager);
    int32_t density = AConfiguration_getDensity(config);
    AConfiguration_delete(config);
    return density;
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,没有 API 可以获取 DisplayMetrics.xdpi 和 DisplayMetrics.ydpi。