开发者

Looking for just a quick 'n easy wait command

开发者 https://www.devze.com 2023-03-31 00:10 出处:网络
Just after a quick wait command that will make my application stop, for say 10 milliseconds, so that it\'ll look like it\'s animating stuff (in my case a ball).

Just after a quick wait command that will make my application stop, for say 10 milliseconds, so that it'll look like it's animating stuff (in my case a ball).

    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;
            }
        });
    }
}

}

As you can see, just after the loop begins I've tried a few things (the WaitTime refers to a long that I got rid of, it didn't work) - neither of which have worked.

Thanks.


No need to do this by hand. Use frame animation (with series of bitmaps) or better a tween animation (moving/resizing an existing view).


The proper way to wait is this:

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

However, note that this will run on the UI thread. For 10ms there is no real problem and it's the preferred way to wait... But for longer periods it will result in an ugly dialog to your users saying your application is not responding.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号