38

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

在图像中,红色矩形是我的目标。 在此处输入图像描述

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

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

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

根据示例获得解释会很棒。我在用-

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

绘制矩形

4

5 回答 5

42

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

在这个

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

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

  3. right:矩形右侧到画布左侧的距离。
  4. 底部:矩形底部到画布顶部的距离。
于 2014-01-04T09:19:57.280 回答
30

这将是有道理的。

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);
于 2015-10-20T19:41:31.247 回答
16

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

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

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

于 2013-10-19T11:32:58.137 回答
2

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

/**
 * 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)
 */
于 2019-02-12T08:22:30.533 回答
2

这是我的方法简单易行

            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-07-31T11:54:38.957 回答