开发者

Delaying after onDraw()/onCreate()

开发者 https://www.devze.com 2023-04-10 19:48 出处:网络
Where in this code would I put a thread delay, that will happen after the completion of onCreate(), which means also after the completion/showing of onDraw()? Afterwards I will be calling grid.clearPa

Where in this code would I put a thread delay, that will happen after the completion of onCreate(), which means also after the completion/showing of onDraw()? Afterwards I will be calling grid.clearPattern() which clears the pattern drawn on the canvas when grid.displayPattern() was called. So afterwards I will still need to be able to modify the canvas.

package com.patterns;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class PlayGame extends Activity implements View.OnTouchListener {
    int size;
    Grid grid;
    PatternView patternview;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    size = getIntent().getExtras().getInt("size");
    patternview = new PatternView(this);
    setContentView(patternview);    

    Handler pauser = new Handler();
    pauser.postDelayed(new Runnable() {
        public void run() {
            patternview.clearDraw();
        }
    }, 2000);
    patternview.setOnTouchListener(this);
}

public class PatternView extends View { 
    Paint paint = new Paint();
    public PatternView(Context context){
        super(context);


    }

    protected void clearDraw() {
    Log.d("debug", "clearDraw called");
    grid.clearPattern();
}

    @Override 
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        grid = new Grid(size, size, getWidth(), getWidth(), canvas, paint);                     
        grid.createPattern();
        grid.displayPattern开发者_开发百科();
        Log.d("debug", "lines drawn");
        grid.setBoard();    
        Log.d("debug", "board set");            
    }
}

@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
    //Log.d("debug", "screen touched");
    grid.screenTouch(arg1);
    grid.fillActiveRectangles();
    return false;
}

}


Maybe stick the call to grid.clearPattern() into an android.os.Handler? Had a similar app-pausing problem and this did the trick for me. So stick something like this at the end of onCreate() -- the 3500 is a pause in milliseconds, choose the value that you want.

Handler pauser = new Handler();
pauser.postDelayed (new Runnable() {
public void run() {
        grid.clearPattern();
    }
}, 3500);


Could it be like this?

grid.createPattern();    
grid.displayPattern(canvas, paint); 
Thread.sleep(2000);

But it will be a pain...

0

精彩评论

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

关注公众号