如何在 for 循环中使用 invalidate()

我想展示我正在制作的游戏的镜头。但画布在我的 for 循环结束时就失效了。这是同样的问题,就像这里画布不会更新(无效)直到整个 while 循环结束 但答案对我不起作用。在这种情况下如何使我的画布无效?:

else if (event.getX() > 1150 && event.getX() < 1300 

          && event.getY() > 550 && event.getY() < 700)

  {

    System.out.println("Schuss");

    final ArrayList<Shot> positions = game.shot(100, 45, canvas);

    new Thread(new Runnable()

    {

        public void run()

        {

            for (int i = 0; i < positions.size(); i++)

            {

                game.setCurrentShot(positions.get(i));

                System.out.println(i);

                canvas.postInvalidate();

            }       

        }

    }).start();

}

在射击功能中,我计算所有位置。镜头将通过,然后我想在画布上显示这些位置,但这不起作用。


慕村9548890
浏览 131回答 1
1回答

喵喵时光机

尝试这个:else if (event.getX() > 1150 && event.getX() < 1300 && event.getY() > 550 && event.getY() < 700){&nbsp; &nbsp; System.out.println("Schuss");&nbsp; &nbsp; final ArrayList<Shot> positions = game.shot(100, 45, canvas);&nbsp; &nbsp; YourActivity.this.runOnUiThread(new Runnable() {&nbsp; &nbsp; &nbsp; &nbsp; public void run() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < positions.size(); i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; game.setCurrentShot(positions.get(i));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.d("YourApp", "i = "+i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; canvas.invalidate();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java