4

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.

alain.janinm
  • 19,951
  • 10
  • 65
  • 112
C0deAttack
  • 24,419
  • 18
  • 73
  • 81

2 Answers2

2

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.

C0deAttack
  • 24,419
  • 18
  • 73
  • 81
  • 2
    So how did you end up drawing a link from these 2 points if you couldn't use pixels? – Ben Mc Feb 07 '11 at 18:21
  • This is what I do: I figure out the size in px for a specific resolution and then calculate the ratio to the resolution. I can then use this value to calculate the actual px size for all resolutions. – T3rm1 Feb 17 '13 at 11:44
-1

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.

Community
  • 1
  • 1
user432209
  • 20,007
  • 10
  • 56
  • 75