2

I'm writing some code that needs to draw some Canvas class drawing primitives consistently on devices with many different DPI's and screen resolutions. But I can't seem to find what units the drawLine() method's points are in.

It doesn't say in the canvas class documentation: http://developer.android.com/reference/android/graphics/Canvas.html#drawLine(float, float, float, float, android.graphics.Paint)

On S.O. This poster thinks it's in px: how to draw lines with drawLine method in multiple density screens

... and the answerer to this S.O. question did some experiments and determined that the units varied according to a setting in the manifest!

I also looked in Google's Canvas and Drawables documentation and the word "units" doesn't even appear there.

What units are the canvas drawing primitives like drawLine() in and where does Google/Android officially document this?

Community
  • 1
  • 1
user316117
  • 7,971
  • 20
  • 83
  • 158

1 Answers1

3

It's pixels. As with all the Canvas graphical functions (drawXYZ()).
Reference: http://developer.android.com/reference/android/graphics/Canvas.html

Mostly because it's all about drawing on a Bitmap.
And a Bitmap measure unit is the pixel.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • I read that document and I didn't see any place where they said the **units** are in px. They use the _the word_ "pixels" but only in a generic context to mean a point drawn on a display. What did you see to suggest they meant it as the px unit? (i.e., as opposed to dp, sp, etc) – user316117 Apr 20 '15 at 15:28
  • It's always talking about **pixels**. It **never mentions other units**. And it begins with: `The Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels`. Therefore, it should be clear that it's all in pixels. Also because a Bitmap measure unit is the **pixel**. – Phantômaxx Apr 20 '15 at 15:30
  • It doesn't mention **any** units. Android doesn't have a unit called "pixels"; it has a unit called "px". You're assuming that 1 px = 1 pixel, which is not unreasonable but one of the links I mention in my OP said otherwise. I looked at the BitMap class [http://developer.android.com/reference/android/graphics/Bitmap.html] documentation and it also doesn't mention units, but since bitmaps are scalable I don't think we can **assume** that a bitmap pixel reliably equals 1 px (i.e., that 10 pixels takes up 1/48th of a 480 pixel-wide display). – user316117 Apr 20 '15 at 16:10
  • 1
    Well... does it take a genius to understand that **px** is the short for **p**i**x**el? Are you kidding me? It's like saying that `in and inch` are two different units. Or `dp and dip`. Or `mm and millimeter`... http://angrytools.com/android/pixelcalc/ I guess you don't have clear the difference between dp and px. – Phantômaxx Apr 20 '15 at 16:37
  • 5
    Android framework engineer here. It's pixels. The `px` unit is physical display pixels. – alanv Apr 20 '15 at 18:42