Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Well, I wouldn't personally block the UI thread by calling Thread.sleep(1000) within my draw method. Instead, I would use a Handler object, then I'd call recursive postDelayed calls in order to schedule repeated invalidate() calls. Finally, I'd code 2 special functions to start the "animation" and to interrupt it if needed (following code is NOT tested... Just a snippet I wrote in a few sec to illustrate my point. Caveat emptor.). Note that what which was described by Aderito Brinca is accurate as well, although it doesn't solve the UI thread blocking issue as mentioned above.</p> <pre><code>private Handler mHandler = new Handler(); private boolean isAnimStarted=false; final private long refreshIntervalMsec = 2000; //2 seconds refresh rate final private long maxRefreshTimeMsec = 120000; //120 seconds deadline private long systemTimeMillisOnStart = -1L; private Display display = null; //within a local variable declared in GameView //constructor this variable was useless. Now that it is a private //class member, it can be used by overridden onDraw or Draw methods. public GameView(Context context){ super(context); display = ((WindowManager)context.getSystemService( Context.WINDOW_SERVICE)).getDefaultDisplay(); //INSERT THE REST OF YOUR CONSTRUCTOR CODE HERE } public boolean isGameViewAnimStarted(){ //CALL this in order to check for running anim return isAnimStarted; } public void startGameViewAnim(){ //CALL this to start your anim thing isAnimStarted=true; systemTimeMillisOnStart = SystemClock.uptimeMillis(); refreshAnim(); } private void refreshAnim(){ invalidate(); if( (SystemClock.uptimeMillis() - systemTimeMillisOnStart) &lt; maxRefreshTimeMsec){ mHandler.postDelayed(new Runnable(){ public void run(){ refreshAnim(); } }, refreshIntervalMsec); } else{ stopGameViewAnim(); } } public void stopGameViewAnim(){ //CALL this to interrupt your anim thing mHandler.removeCallbacksAndMessages(null); isAnimStarted=false; } @Override public void draw(Canvas canvas) { // TODO Auto-generated method stub super.draw(canvas); //INSERT YOUR CODE HERE } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload