问题
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