Mental Jetsam

By Peter Finch

Archive for July 26th, 2012

Calculating Android canvas device pixels sizes

Posted by pcfinch on July 26, 2012

When drawing on a Android Canvas, it’s important to take into consideration the different pixel densities when rendering lines and text. Using “pixels” can make things too small on high resolution screens. Using DIP instead of pixels is a good way to keep things a similar size physical size on the screen and the following code can be used to map DP values to pixel based values.

int dpSize = 5 ;
DisplayMetrics dm = getResources().getDisplayMetrics() ;
float inPixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpSize , dm);
paint.setStrokeWidth(inPixels) ;
paint.setTextSize(inPixels * 3);

Posted in Programming | Leave a Comment »