了解drawRect或绘图坐标在Android中的实际工作方式

时间:2013-10-19 10:22:00

标签: android android-layout android-canvas

我正在尝试在画布上绘制一个矩形,我正在面对麻烦来理解Android的矩形绘制的深度。我已经阅读了教程,并且每一个可能,但我被卡住了。

在图片中,红色矩形是我的目标。 enter image description here

无论任何矩形大小,我都需要在基座上方和矩形中间绘制红色矩形。我在这里遇到的最糟糕的噩梦是理解X,Y宽度和高度坐标。

任何人都可以解释数学是如何工作的,有时我们上升,Y达到非常小但相同的宽度坐标更高。并且我永远无法正确地证明红色内部矩形。在某些屏幕中,它在其他一些屏幕上运行良好。红色矩形有时会出现在父矩形中。

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

根据一个例子得到解释会很棒。我正在使用 -

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

绘制矩形

5 个答案:

答案 0 :(得分:32)

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

在此

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

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

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

答案 1 :(得分:21)

这是有道理的。

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);

答案 2 :(得分:16)

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

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

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

答案 3 :(得分:1)

这是我的简单方法

            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);
}

效果很好

答案 4 :(得分:0)

希望我的注释如下,可以帮助您了解相对性分别是矩形,画布和视图。

/**
 * 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)
 */