在一个快速的等待命令之后,我的应用程序将停止,比如说10毫秒,这样它看起来就像是在制作动画(在我的例子中是一个球)。

    public class PiranhaDrop extends Activity
    {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    FrameLayout main = (FrameLayout) findViewById(R.id.main_view);
    main.addView(new Drawing(this,0,0,0));
    int MyLoop=0;
    while(MyLoop<100)
    {
        main.addView(new Drawing(this,MyLoop,10,10));
        synchronized(this){wait(10);}
        //try{Thread.sleep(WaitTime);} catch (InterruptedException e){}
        MyLoop=MyLoop+1;
        main.setOnTouchListener(new View.OnTouchListener()
        {
            public boolean onTouch(View v, MotionEvent e)
            {
                float x = e.getX();
                float y = e.getY();
                FrameLayout flView = (FrameLayout) v;
                flView.addView(new Drawing(getParent(),x,y,25));
                return false;
            }
        });
    }
}

}
正如你所看到的,在循环开始后,我尝试了一些事情(等待时间指的是我摆脱的一段时间,它不起作用)-都没有起作用。
谢谢。

最佳答案

正确的等待方式是:

try
{
    Thread.sleep(10);
}
catch (Exception e){}

但是,请注意,这将在ui线程上运行。
10分钟没有问题,这是最好的等待方式…但在较长时间内,这将导致用户出现一个难看的对话框,说你的应用程序没有响应。

09-10 03:29
查看更多