我想在画布上绘制一个矩形,我现在面临的麻烦了解的Android的矩形绘制的深入。我读过的教程和每一个可能的,但我坚持。
在此图像中,红色矩形是我的目标。
无论任何矩形大小的我需要绘制底部的红色矩形位上方,并在矩形的中间。我对着这里的最糟糕的噩梦是理解的X,Y宽度和高度坐标。
任何人都可以解释,数学是如何工作的,有时我们上去,Y达到非常小,但相同的宽度坐标较高。而且我从来没有能够证明红内部矩形properly.In一些屏幕效果很好在其他一些失败。红色矩形有时会父矩形出来。
议程是要了解如何协调工作,确保内部红色矩形的完整性
这将是巨大的,我们将根据一个例子进行说明。我在用-
void drawRect(float left, float top, float right, float bottom, Paint paint)
绘制矩形
X水平上运行,从左边到右边。 Ÿ垂直延伸,从顶部到底部。这是完全一样的图形。所以(0/0)是在左上角。
当你走“向上” Y当然会越来越小,因为它从顶部增长到底部。
你必须要注意铺设出像列表视图元素,这些都将给出部分(或新的,你不能告诉)画布你的意见被绘制。这些意见将在自己的顶部/左侧位置具有为0x0。如果你需要绝对的,你必须继续调用View.getLocationOnScreen()
并计算抵消自己。
canvas.drawRect(left,top,right,bottom,paint);
在这
这将是有意义的。
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);
这里是我的方法简单,易于
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);
}
它工作得很好
希望我的笔记如下帮助你理解相对论属于矩形,帆布和视图。
/**
* 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)
*/