【问题标题】:What does top, left, right and bottom mean in Android Rect objectAndroid Rect对象中的top,left,right和bottom是什么意思
【发布时间】:2014-03-23 09:58:03
【问题描述】:

我有一个 Android 项目,我应该让苹果倒下。苹果被画成一个矩形。所以我创建了一个改变 Rect 位置并重新绘制的函数。这是我的功能:

private void updateApplesPosition() {
    for(Rect rect:fallingDownFruitsList)
        rect.set(rect.left, rect.top +10, rect.right, rect.bottom +10);
}

我有一个问题:苹果不会掉下来,而是从右到左。为了让苹果掉下来,我修改了代码:

private void updateApplesPosition() {
    for(Rect rect:fallingDownFruitsList)
        rect.set(rect.left+10, rect.top, rect.right+10, rect.bottom);
}

【问题讨论】:

  • 使用 Rect.offset(dx, dy)
  • 它们不会自己走,你画它们,bug就在那里
  • Rect 工作得很好,因此问题在于如何绘制它们。

标签: android android-canvas android-view android-shape


【解决方案1】:

这张图片会详细解释你:

left矩形左侧的X坐标

top矩形顶部的Y坐标

right矩形右侧的X坐标

bottom矩形底部的Y坐标

【讨论】:

  • 非常感谢@Log,两天来我一直在努力理解左、右和下的含义。不幸的是,Android 开发者的文档对这个类没有太多解释。
  • @LOG_TAG : 值是像素还是整数?
  • rect.top 会给出像素值还是dp值?
  • 数值以像素为单位
【解决方案2】:

From the docs

参数

left矩形左侧的X坐标

top矩形顶部的Y坐标

right矩形右侧的X坐标

bottom矩形底部的Y坐标

【讨论】:

  • 我已经阅读了文档,但是为什么当我做 rect.top +10 时它会向左而不是向下
  • @user230137 setBounds(x-width/2, y-height/2, x+width/2, y+height/2) 其中 int width = getWidth()/10;int height = getHeight()/10;
  • @adneal : 这些值是像素吗?
  • @Aniruddha 是的,这些值以像素为单位
【解决方案3】:

添加重要信息。

文档说:

注意右下坐标是互斥的。

因此,如果您的矩形是位置 10,10 处的 单个 像素

left = 10 : 矩形左侧的 X 坐标

top = 10 : 矩形顶部的 Y 坐标 = 10

right = 11 : 矩形右侧的X坐标加一

bottom = 11 : 矩形底部的Y坐标加一

注意右下坐标是互斥的。

getWidth 方法是这样声明的

public final int width() { return right - left; }

这里将返回 11 - 10 = 1,正如预期的那样。

https://developer.android.com/reference/android/graphics/Rect

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-01
    • 1970-01-01
    相关资源
    最近更新 更多