Mental Jetsam

By Peter Finch

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);
...
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: