【问题标题】:Understanding how actually drawRect or drawing coordinates work in Android了解 drawRect 或绘图坐标在 Android 中的实际工作方式
【发布时间】:2019-07-31 19:54:38
【问题描述】:

我正在尝试在画布上绘制一个矩形,但在深入了解 Android 的矩形绘制时遇到了麻烦。我已经阅读了所有可能的教程,但我被卡住了。

在图像中,红色矩形是我的目标。

无论矩形大小如何,我都需要在底部上方和矩形中间绘制红色矩形。我在这里面临的最糟糕的噩梦是了解 X、Y 宽度和高度坐标。

任何人都可以解释这个数学是如何工作的,有时我们上升,Y 达到非常小但相同宽度的坐标更高。而且我永远无法正确证明红色内部矩形的合理性。在某些屏幕上它运行良好,而在另一些屏幕上却失败了。红色矩形有时会从父矩形中出来。

议程是了解坐标如何工作并确保内部红色矩形的完整性

如果能根据示例获得解释,那就太好了。我正在使用-

void drawRect(float left, float top, float right, float bottom, Paint paint)

绘制矩形

【问题讨论】:

  • 矩形(topleftx,toplefty,bottomrightx,bottomrighty)
  • 我知道这些参数,但我无法理解 X , Y 值,如何设置它.. 任何示例解释都会有所帮助
  • @JRowan 在我发布的矩形中,这些参数值设置在哪里??
  • 画布左上角x=0和y=0,画布右下角是x = canvas.getWidth()和y = canvas.getHeight()
  • 是这样的矩形与角落坐标的样子,这是书上的问题还是什么

标签: android android-layout android-canvas


【解决方案1】:

canvas.drawRect(left,top,right,bottom,paint);

在这

  1. left:矩形左侧到矩形左侧的距离 画布。

  2. top:矩形顶边距矩形顶边的距离 画布

  3. right:矩形右侧到矩形左侧的距离 画布。
  4. bottom:矩形底边与画布顶边的距离。

【讨论】:

  • 我认为 2 和 4 不正确。它们需要是: 2. top:矩形 TOP 侧与画布顶部的距离 4. bottom:矩形 BOTTOM 侧与画布顶部的距离。
  • savan : 这些值是像素吗?
  • 简单完美的解释。正是我真正理解这一点所需要的。
  • 这就是我所需要的
【解决方案2】:

这是有道理的。

float left = 100, top = 100; // basically (X1, Y1)

float right = left + 100; // width (distance from X1 to X2)
float bottom = top + 100; // height (distance from Y1 to Y2)

这样

RectF myRectum = new RectF(left, top, right, bottom);
canvas.drawRect(myRectum, myPaint);

【讨论】:

  • 我认为 Rectangle 变量名是我在 StackOverflow 上见过的最有趣的东西。 XD
【解决方案3】:

X 从左到右水平运行。 Y 从上到下垂直运行。它与您的图形完全相同。所以 (0/0) 在左上角。

当你“向上”时,Y 当然会变小,因为它是从上到下增长的。

你必须注意布局像 ListViews 这样的元素,它们会给你绘制的视图提供部分(或新的,你无法分辨的)画布。这些视图将在它们自己的顶部/左侧位置具有 0x0。如果您需要绝对值,则必须随后调用 View.getLocationOnScreen() 并自己计算偏移量。

【讨论】:

  • meredrica : 这些值以像素为单位?
【解决方案4】:

希望我下面的笔记能帮助你理解相对论属于矩形、画布和视图。

/**
 * Rect holds four integer coordinates for a rectangle.
 * The rectangle is represented by the coordinates of its 4 edges (left, top, right bottom).
 * These fields can be accessed directly. Use width() and height() to retrieve the rectangle's width and height.
 *
 * Note that the right and bottom coordinates are exclusive.
 * This means a Rect being drawn untransformed onto a Canvas will draw into the column and row described by its left and top coordinates
 * , but not those of its bottom and right.
 *
 * With regard to calling to Canvas#drawRect(left,top,right,bottom,paint)
 *
 * left: Distance of the left side of rectangle from left side of canvas.
 * top: Distance of top side of rectangle from the top side of canvas
 * right: Distance of the right side of rectangle from left side of canvas.
 * bottom: Distance of the bottom side of rectangle from top side of canvas.
 * __________________________________
 *|
 *|
 *|   __l_______________________r__
 *|  |         view group A        |
 *| t|  0______________________w   |
 *|  |  | **** view group B *** |  |
 *|  |  | **** canvas of B **** |  |
 *|  |  | ********************* |  |
 *|  |  | ********************* |  |
 *|  |  | ********************* |  |
 *|  |  | ***** __________ **** |  |
 *|  |  | *****|## rect ##|**** |  |
 *|  |  | *****|##########|**** |  |
 *|  |  | *****|##########|**** |  |
 *|  |  | *****|##########|**** |  |
 *|  |  | *****|##########|**** |  |
 *|  |  | ***** ---------- **** |  |
 *|  |  | ********************* |  |
 *| b|  h-----------------------   |
 *|  |                             |
 *|  |                             |
 *|   -----------------------------
 *|
 * -----------------------------------
 *
 * 1. l, t, r, b are coordinates of view group B (PastryChart) relative to view group A (parent of PastryChart).
 * 2. The size of canvas of B is same as the size of the view group B
 *    , which means canvas of B is a canvas which the view group B is rendered to.
 * 3. The coordinates of rect is relative to a canvas, here is the canvas of B
 *    , which means the coordinates of rect going to represent child of view group B are relative to the canvas of B.
 *    ex. for a rect holding left = 0, the position of its left is located on the same position of the left of view group B
 *    ex. for a rect holding right = w, the position of its right is located on the same position of the right of view group B
 *    ex. for a rect holding top = 0, the position of its top is located on the same position of the top of view group B
 *    ex. for a rect holding bottom = h, the position of its bottom is located on the same position of the bottom of view group B
 * 4. The rect is used to stored the child measurement computed in measure pass
 *    for forward positioning child view (PastryView) in the layout pass taken by parent view (PastryChart).
 * 5. All of them are in pixels (px)
 */

【讨论】:

    【解决方案5】:

    这是我的简单方法

                int x = 100;  //position coordinate from left
                int y = 100;  //position coordinate from top
                int w = 100; //width of the rectangle
                int h = 100; //height of the rectangle
                drawRectangle(x, y, w, h, canvas, paint);
    

    这是我的功能

        public void drawRectangle(int left, int top, int right, int bottom, Canvas canvas, Paint paint) {
        right = left + right; // width is the distance from left to right
        bottom = top + bottom; // height is the distance from top to bottom
        canvas.drawRect(left, top, right, bottom, paint);
    }
    

    效果很好

    【讨论】:

      猜你喜欢
      • 2019-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-27
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 2014-01-25
      相关资源
      最近更新 更多