了解如何实际的drawRect或图形中的Andr​​oid协调工作图形、实际、工作、drawRect

2023-09-06 03:45:08 作者:萌城?

我想绘制一个矩形在一个画布,我面临的困难,了解深入的Andr​​oid的矩形平局。我读过的教程和每一个可能的,但我坚持。

I am trying to draw a rectangle over a canvas and I am facing troubles to understand the in-depth of rectangle draw of Android. I've read tutorials and every possible but I am stuck.

下面的图片中,红色矩形是我的目标。

Here in the image , the red rectangle is my target.

不论任何矩形尺寸的我需要绘制红色矩形位上方的基座,并在矩形的中间。我在这里面临的是了解在X的最糟糕的噩梦,Y宽度和高度协调。

Irrespective of any rectangle size I need to draw the red rectangle bit above the base and in the middle of the rectangle. The worst nightmare I am facing here is understanding the X,Y Width and Height coordinates.

谁能解释这是如何工作的数学,有时我们上去,Y达到非常小的,但宽度相同的坐标都较高。而且我从来没有能够证明红光内部矩形properly.In一些屏幕效果很好在其他一些失败。红色矩形有时会父矩形的。

Can anyone explain how that math works, sometime we go up , Y reaches to very small but same width coordinates are higher. And I am never able to justify red inner rectangle properly.In some screen it works well in some other it fails. The red rectangle sometimes come out of the parent rectangle.

议程是了解如何协调工作,确保内部红色矩形的完整性

这将是巨大的,得到基于一个例子进行说明。我使用 -

It'll be great to get an explanation based on an example. I am using-

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

要绘制矩形

推荐答案

X运行水平,从左至右。 Ý垂直运行时,从顶部到底部。这是完全一样的图形。所以(0/0)位于左上角。

X runs horizontally, from left to right. Y runs vertically, from top to bottom. It's exactly the same as on your graphics. So (0/0) is at top left.

当你走了Y当然会变得越来越小,因为它从顶部长到底部。

When you go "up" Y will of course get smaller, as it grows from top to bottom.

您要注意铺设出像列表视图元素,这些将会给部分(或新的,你看不出来)的画布到您的视图绘制。这些意见将在为0x0的自己顶部/左侧位置。如果你需要绝对的,你必须继续调用 View.getLocationOnScreen(),并计算抵消自己。

You have to pay attention to laying out elements like ListViews, these will give a partial (or new, you cannot tell) canvas to your views that are drawn. These views will have 0x0 at their own top/left position. If you need the absolute you have to subsequently call View.getLocationOnScreen() and calculate offsets yourself.