简体   繁体   中英

Is `canvas.drawCircle` metho's radius in dp units?

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?

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM