【问题标题】:RectF use dp or px?RectF 使用 dp 还是 px?
【发布时间】:2019-11-05 10:44:19
【问题描述】:

在Canvas中,用RectF画一个矩形,需要在dp或px中设置top和left吗?

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 它不指示这些值是以 px 还是 dp 表示的。

【问题讨论】:

  • canvas 始终使用 px
  • 记录在哪里?

标签: android canvas


【解决方案1】:

正如已经指出的,Canvas 和 RectF 使用 px 而不是 dp。 至于文档,Canvas 的文档 (http://developer.android.com/intl/es/reference/android/graphics/Canvas.html) 而 RectF 只提到像素。

由于没有明确指出它们是dp,而且这两个类都是直接从java.lang.Object派生的,所以只能断定它一定是“正常”的像素。

如果由于某种原因,您需要从 dp 转换为 px,反之亦然,请查看此文档: http://developer.android.com/intl/es/guide/practices/screens_support.html

【讨论】:

    【解决方案2】:

    它使用像素,而不是与密度无关的像素。

    【讨论】:

    • 谢谢,定义它的文档?
    • 不确定。 Android 中的大多数(如果不是全部)图形 API 都使用像素
    【解决方案3】:

    它使用像素,但如果你想必须转换它。

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

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 2020-01-09
      • 2012-01-08
      • 2015-06-22
      • 2011-04-03
      • 2014-08-21
      • 1970-01-01
      • 2011-01-25
      • 2010-12-28
      相关资源
      最近更新 更多