RectF use dp or px?

China☆狼群 提交于 2021-01-28 01:54:18

问题


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.


回答1:


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




回答2:


It uses pixels, not density independent pixels.




回答3:


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.



来源:https://stackoverflow.com/questions/32540253/rectf-use-dp-or-px

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!