4

In Canvas, drawing an rectangle with RectF, need set top and left in dp or px?

Integer padding = 10;
Integer width = 100; // It is dp or px?
Integer height = 50;

RectF position = new RectF();
position.top = 0 + padding;
position.bottom = position.top + height;
position.left = 0 + padding;
position.right = position.left + width;

http://developer.android.com/intl/es/reference/android/graphics/RectF.html It does not indicate if the values are represented in px or dp.

e-info128
  • 3,727
  • 10
  • 40
  • 57

3 Answers3

7

As has already been pointed out, Canvas and RectF use px and not dp. As for the documentation, the documentation for Canvas (http://developer.android.com/intl/es/reference/android/graphics/Canvas.html) and RectF only mention pixels.

Since it is not explicitly pointed out that they are dp and both classes are directly derived from java.lang.Object, one can only conclude that it must be "normal" pixels.

If, for some reason, you need to convert from dp to px and vice versa, have a look at this document: http://developer.android.com/intl/es/guide/practices/screens_support.html

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
0

It uses pixels, not density independent pixels.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
0

It using pixel but if you want must convert it.

private int convertDpToPx(int dp){
    return Math.round(dp * (getResources().getDisplayMetrics().xdpi / DisplayMetrics.DENSITY_DEFAULT));
}

I hope it helps.

saeid aslami
  • 121
  • 1
  • 4