multiply the pixel values by getResources().getDisplayMetrics().density to make the pixels independent of density. If you draw text, you can use scaledDensity instead.
density = 1 at mdpi, 1.5 at hdpi and 0.75 at ldpi, I suppose.
To execute the answer, some source code:
When working with views or images, use the following:
int px = getResources().getDisplayMetrics().density * dp;
Or when it comes to text:
int px = getResources().getDisplayMetrics().scaledDensity * sp;
Mrjre source share