了解drawRect或图形坐标在Android中的实际工作方式
我正试图在画布上画一个矩形,我在理解Android矩形绘制的深度方面遇到了困难。我已经阅读了教程和所有可能的内容,但我被卡住了 在图中,红色矩形是我的目标。了解drawRect或图形坐标在Android中的实际工作方式,android,android-layout,android-canvas,Android,Android Layout,Android Canvas,我正试图在画布上画一个矩形,我在理解Android矩形绘制的深度方面遇到了困难。我已经阅读了教程和所有可能的内容,但我被卡住了 在图中,红色矩形是我的目标。 不管矩形的大小,我都需要画出红色的矩形位在基座和矩形的中间。我在这里面临的最糟糕的噩梦是理解X,Y宽度和高度坐标 有人能解释一下数学原理吗,有时候我们往上走,Y很小,但相同宽度的坐标更高。我永远也无法正确地证明红色的内部矩形。在某些屏幕上,它工作得很好,在另一些屏幕上,它失败了。红色矩形有时会出现在父矩形之外 议程是了解坐标如何工作,并确
不管矩形的大小,我都需要画出红色的矩形位在基座和矩形的中间。我在这里面临的最糟糕的噩梦是理解X,Y宽度和高度坐标
有人能解释一下数学原理吗,有时候我们往上走,Y很小,但相同宽度的坐标更高。我永远也无法正确地证明红色的内部矩形。在某些屏幕上,它工作得很好,在另一些屏幕上,它失败了。红色矩形有时会出现在父矩形之外 议程是了解坐标如何工作,并确保内部红色矩形的完整性 如果能从一个例子中得到解释,那就太好了。我正在使用-void drawRect(float left, float top, float right, float bottom, Paint paint)
要绘制矩形,请从左到右水平运行X。Y从上到下垂直运行。它与图形上的完全相同。所以(0/0)在左上角 当你往上走的时候,Y当然会变小,因为它会从上到下增长
您必须注意布局元素,如ListView,这些元素将为您绘制的视图提供部分(或新的,您不能说)画布。这些视图在其自身的上/左位置将有0x0。如果需要绝对值,则必须随后调用
View.getLocationOnScreen()
并自己计算偏移量。canvas.drawRect(左、上、右、下、绘制)代码>
在这个
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)
因此
希望我下面的笔记能帮助你理解直线、画布和视图的相对性
/**
* 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)
*/
以下是我的方法简单易行
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(topleftx,toplefty,bottomrightx,bottomrighty)我知道这些参数,但我无法理解X,Y值,以及如何设置它。。任何例子的解释都是正确的help@JRowan在我发布的矩形中,这些参数值在画布的左上角设置了x=0和y=0,画布的右下角是x=canvas.getWidth()和y=canvas.getHeight()这正是矩形在角点坐标处的外观,这是书中的问题还是什么我认为2和4是不正确的。它们必须是:2。顶部:矩形顶部与画布顶部之间的距离4。底部:矩形底部与画布顶部之间的距离。萨文:这些值是以像素为单位的吗?简单而完美的解释。这正是我真正理解这一点所需要的。这就是我需要的梅雷德里卡:这些值以像素为单位?我认为矩形变量名是我在StackOverflow上见过的最有趣的事情。除息的
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);
}