Pixels vs DP / SP

I’m curious how people are handling drawing for different screen densities. I have an app where I’m using pixels for height/width of objects, but realized on certain phones the objects become way too small (same for fonts) since the pixel densities are much higher.

Is there a simple way to start using dp instead of pixels without having to manually go in and multiply all the pixel numbers to density conversions? Is there a method in OF for returning the density of the device? This is something that also is an issue on iOS. So a more general solution would be great in the long run.

This is the exact problem I’m facing now. what I think will be the nice start with android is to create an instance of http://developer.android.com/reference/android/util/DisplayMetrics.html in the java part. This should allow to scale correctly.

I’d like to think of it as a chance to start to start to measure display things in the real-world scale, cause it’s not about pixels anymore.

Yeah, for now, I ended up writing a function in the ofActivity to return the device density.

Then I wrote a utility function in c++ that’s included in all classes:

  
  
float ofDpToPx(float dp){  
    return Settings::getDensity() * dp;  
}  

So any place I use pixels to draw, I wrap the value in ofDpToPx(value) and it’ll convert it to the pixel amounts that OF uses. A better solution is definitely needed though and at minimum a method in core to return device density so that it’s accessible without having to save it oursevles.

I think it needs to be a little more flexible for things like applying do to PX conversion to ofRectangle.

Also, how do you think, is it possible to insert just the same thing into iOS application?