如何创建 Rect 的数组或数组列表

发布于 2024-11-09 04:45:54 字数 772 浏览 0 评论 0原文

我要疯狂地想弄清楚这一点。我正在尝试创建一个 Rect(矩形)数组或数组列表,我可以更新坐标并在屏幕上绘制(以使它们移动)。

现在我有一个名为 Fire 的单独类,在其中每次迭代时我都会在其自己的 onDraw() 方法中使用新坐标创建一个新矩形。在 View 的 onDraw() 方法的第一次迭代中,我将一个新的 Fire 添加到数组列表中。

在 Fire 类中,我有:

public void onDraw(Canvas canvas){
    moveF();
    Rect r = new Rect(_l,_t,_r,_b);
    canvas.drawRect(r, paint);
}

在 View 类中,我有:

protected void onDraw(Canvas canvas) {
    int i = 0;
    canvas.drawColor(Color.WHITE);
    if(i==0){
        fires.add(new Fire(20,100,40,120));
        i++;
    }
    for(Fire fire : fires){
        fire.onDraw(canvas);
    }
}

我删除了无意义的代码部分,但这是重要的东西。矩形打印,但是它也打印之前的所有位置,我不明白为什么。我一直在努力解决这个问题,如果你们能提供任何帮助,我将不胜感激。我能够轻松地在java中实现这个,但是android给我带来了问题。

提前致谢!!!

I am going crazy trying to figure this out. I am trying to make an array or arraylist of Rect (rectangles) that I can update the coordinates and draw on the screen(to make them move).

Right now I have a separate class called Fire in which I make a new rectangle each iteration with the new coordinates in its own onDraw() method. In the View's onDraw() method's first iteration I add a new Fire to an arraylist.

In the Fire class I have:

public void onDraw(Canvas canvas){
    moveF();
    Rect r = new Rect(_l,_t,_r,_b);
    canvas.drawRect(r, paint);
}

In the View class I have:

protected void onDraw(Canvas canvas) {
    int i = 0;
    canvas.drawColor(Color.WHITE);
    if(i==0){
        fires.add(new Fire(20,100,40,120));
        i++;
    }
    for(Fire fire : fires){
        fire.onDraw(canvas);
    }
}

I got rid of pointless parts of code, but this is the important stuff. The Rectangle prints, however it prints all the previous locations as well and I don't understand why. I have been trying to fix this forever and any help you guys could give would be greatly appreciated. I was able to implement this in java easy, but android is giving me problems.

Thanks in advance!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感受沵的脚步 2024-11-16 04:45:54

在代码中,您将新的矩形添加到列表中,然后绘制每个矩形。但从描述来看,您似乎只想绘制一个带有更新坐标的矩形。

不必每次都创建一个新的 Rect,而是重用一个矩形并使用 set(...) 更新其坐标。

第二个问题是,您设置了 i=0,然后立即检查 i==0,这始终为真。尝试这样的事情:

static final Rect rect = new Rect();

// your code here

protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.WHITE);

    moveF();
    rect.set(_l,_t,_r,_b);
    canvas.drawRect(rect, paint);

}

From the code, you are adding new rectangles to the list, and then drawing each rectangle. But from the description, it seems that you just want to draw a single rectangle, with updated coordinates.

Instead of creating a new Rect each time, reuse a rectangle and update its coordinates with set(...).

A second problem is that you set i=0 and then immediately check for i==0, which would be always true. Try something like this instead:

static final Rect rect = new Rect();

// your code here

protected void onDraw(Canvas canvas) {
    canvas.drawColor(Color.WHITE);

    moveF();
    rect.set(_l,_t,_r,_b);
    canvas.drawRect(rect, paint);

}
囚你心 2024-11-16 04:45:54

每次调用 View.onDraw 时,您都会创建一个新的火焰并将其添加到您的列表中。
然后你迭代所有的火并绘制它们。
所以你会得到越来越多的火灾。
所有火灾的移动方式都是相同的还是有一些随机成分?
您可能看不到一场火灾之前的位置,但有许多火灾在同一条路径上移动。

您想要 1 个火四处移动还是越来越多的火独立移动?

With each call to View.onDraw you create a new fire and add it to your list.
Then you iterate over all fires and draw them.
So you get more and more fires.
Do all fires move the same way or is there some random component?
It might be that you don't see the previous locations of one fire, but that there are many fires moving on the same path.

Do you want to have 1 fire moving around or more and more fires moving around independently?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文