onDraw()/onCreate() 之后延迟

发布于 2024-12-08 06:59:35 字数 1910 浏览 0 评论 0原文

在这段代码中,我应该在哪里放置线程延迟,这将在 onCreate() 完成之后发生,这意味着也在 onDraw() 完成/显示之后发生?之后,我将调用 grid.clearPattern() ,它会在调用 grid.displayPattern() 时清除画布上绘制的图案。所以之后我仍然需要能够修改画布。

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

}

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱娆 2024-12-15 06:59:35

也许将对 grid.clearPattern() 的调用粘贴到 android.os.Handler 中?有一个类似的应用程序暂停问题,这对我来说很有效。因此,在 onCreate() 的末尾添加类似的内容 - 3500 是以毫秒为单位的暂停,选择您想要的值。

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

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);
榆西 2024-12-15 06:59:35

能这样吗?

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

但这会很痛苦...

Could it be like this?

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

But it will be a pain...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文