Android For循环不停止

发布于 2024-10-21 10:31:01 字数 512 浏览 6 评论 0原文

我正在 Android 上玩 2D 图形。我使用 for 循环生成 x 和 y 的随机生成器。奇怪的是,循环永远不会停止:

 for (int i = 0; i < 5; i++){
            System.out.println(i);
            invalidate();
            int randomX = randomGenerator.nextInt(1000);
            int randomY = randomGenerator.nextInt(1000);
            canvas.drawPoint(randomX, randomY, paint);
            float radius = 20;
            canvas.drawCircle(randomX, randomY, radius, paint);
        }

我查看 logcat,它显示 i = 0,1,2,3,4。我是不是要瞎了???我=

I'm playing around with 2 d graphics on android. I'm using random generator for x and y using a for loop. weird thing is that the loop never stops:

 for (int i = 0; i < 5; i++){
            System.out.println(i);
            invalidate();
            int randomX = randomGenerator.nextInt(1000);
            int randomY = randomGenerator.nextInt(1000);
            canvas.drawPoint(randomX, randomY, paint);
            float radius = 20;
            canvas.drawCircle(randomX, randomY, radius, paint);
        }

i look at the logcat it shows i = 0,1,2,3,4. am i going blind??? i =

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

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

发布评论

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

评论(1

葮薆情 2024-10-28 10:31:01

如果您的 for 循环位于视图的 onDraw() 方法内,则调用 invalidate() 将强制视图重新绘制自身,调用 < code>onDraw() 再次,从而无限循环。

If your for loop is inside the onDraw() method of a view, calling invalidate() will force the view to redraw itself, calling onDraw() again, thus the infinite loop.

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