2

Just a quick question. In android, the code for drawing a circle using Canvas is as below.

canvas.drawCircle (float cx, float cy, float radius, Paint paint)

I read the documentation to find out whether this float radius is dp units or any other unit. There is no such mentioning.

So, what is the unit this float radius is mentioned in? Is it dp units?

PeakGen
  • 21,894
  • 86
  • 261
  • 463

2 Answers2

5

In android, the code for drawing a circle using Canvas the float argument is in pixels.

you say float, is the float a pixel representation.

Below is the sample code for converting dp to px and px to dp.

To convert dp to pixel.

public static int dp2px(Resources resource, int dp) {
    return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,   dp,resource.getDisplayMetrics());
}

To convert pixel to dp.

 public static float px2dp(Resources resource, float px)  {
    return (float) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, px,resource.getDisplayMetrics());
}

Conversion Code is from here.

Community
  • 1
  • 1
Maheshwar Ligade
  • 6,709
  • 4
  • 42
  • 59
0

No. Float values will be in pixels. If you want (recommended) you can specify dp units in /res folder using dimen attribute

<dimen name="radius">12dp</dimen>