Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to natively access android DPI, without using JNI?

My android game uses OpenGL and Android NDK. I am currently passing the necessary dpi values as parameters, by making JNI native call from my Activity/GLSurfaceView.

Currently I am using :

    // 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

I was just wondering if one can access the android environment parameters without using JNI to receive them (the parameters). May be like, importing some low level NDK header files (.h) that will help me communicate directly with the UI system or something?

I mean like:

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

Note: This is a question out of curiosity, and I am aware that such a mechanism might not be a good practice.

like image 604
Hari Krishna Ganji Avatar asked Oct 07 '22 03:10

Hari Krishna Ganji


1 Answers

With your setup with Java Activity / GLSurfaceView passing DPI as parameters looks like the best solution.

If you would use NativeActivity you could get DisplayMetrics.densityDpi using:

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

Unfortunately there is no API to get DisplayMetrics.xdpi and DisplayMetrics.ydpi.

like image 180
Pavulon Avatar answered Nov 24 '22 04:11

Pavulon