C0deAttack
C0deAttack

Reputation: 24667

Android: How to handle drawing with canvas methods on different screen sizes?

I've been following the best practices for creating layouts for differing screen sizes as per this guide: http://developer.android.com/guide/practices/screens_support.html

I've now come to doing some drawing work using methods of the Canvas class:

eg. canvas.drawLine(60, 320, 120, 320, linePaint);

Can any one help with how I am supposed to make those pixel values (60/120/etc) in to density pixels so that the line is scaled correctly? Otherwise the line will not appear as I intend, it'll be too far down and too long on smaller screens for example.

Thanks for any replies.

Upvotes: 4

Views: 6219

Answers (2)

C0deAttack
C0deAttack

Reputation: 24667

I missed it the first time going through the Android document; the necessary info is in section "3. Do not use hard-coded pixel values in your code" of the link in my original post.

Upvotes: 2

user432209
user432209

Reputation: 20167

You can find a lot on this topic with Google, in fact here are two questions asked already that pretty much answer your question.

You want to use dips.

You also will probably want to convert dips to pixels.

Upvotes: -1

Related Questions