I'm currently developing a game. I'm using DP as measurement of all the objects, calculated back to PX via Metrics. The problem is, for example on my Nexus 7 tablet, all elements look good, they have the perfect sizes, but on my Nexus 4 they are simply too big - this is obviously because of the DP.
How can I achieve something like this:
The object is ALWAYS 1/5th of the screen's width?
Do I have to calculate the new width programmatically? If so, how would I do this? I would have to keep the density and the resolution of the display in mind, but I can't imagine how I could calculate this right now. Or is there any other way to make this happen?
BTW: I'm drawing on a Canvas, so I have to re-calculate all this into pixels.
This is what I currently use:
public int convertDpToPx(int dp) {
DisplayMetrics displayMetrics = mActivity.getResources().getDisplayMetrics();
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, displayMetrics);
}
public int convertPxToDp(int px) {
DisplayMetrics displayMetrics = mActivity.getResources().getDisplayMetrics();
return (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_PX, px , displayMetrics);
}
And the Object #1 has for example a width of 100dp, but it takes a much bigger part of the screen on my 4.7 inch device than on my 7 inch tablet.