如何在Android上绘制下一个东西之前暂停5秒?

发布于 2024-11-19 18:54:57 字数 330 浏览 2 评论 0原文

假设我想画一条线,然后等待五秒钟,然后再画另一条线。我有一个这样的方法:

    public void onDraw(Canvas canvas) {
        int w = canvas.getWidth();
        int h = canvas.getHeight();
        canvas.drawLine(w/2, 0, w/2, h-1, paint);
        // PAUSE FIVE SECONDS
        canvas.drawLine(0, h/2, w-1, h/2, paint);
    }

如何暂停?

Say I want to draw a line, then wait five seconds, then draw another line. I have a method like this:

    public void onDraw(Canvas canvas) {
        int w = canvas.getWidth();
        int h = canvas.getHeight();
        canvas.drawLine(w/2, 0, w/2, h-1, paint);
        // PAUSE FIVE SECONDS
        canvas.drawLine(0, h/2, w-1, h/2, paint);
    }

How do I pause?

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

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

发布评论

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

评论(4

与他有关 2024-11-26 18:54:57

您可以使用这样的 CountDownTimer :

public void onDraw(Canvas canvas) {
        int w = canvas.getWidth();
        int h = canvas.getHeight();
        canvas.drawLine(w/2, 0, w/2, h-1, paint);
        // PAUSE FIVE SECONDS
        new CountDownTimer(5000,1000){

            @Override
            public void onTick(long miliseconds){}

            @Override
            public void onFinish(){
               //after 5 seconds draw the second line
               canvas.drawLine(0, h/2, w-1, h/2, paint);
            }
        }.start();

    }

问候,

you can use a CountDownTimer like this :

public void onDraw(Canvas canvas) {
        int w = canvas.getWidth();
        int h = canvas.getHeight();
        canvas.drawLine(w/2, 0, w/2, h-1, paint);
        // PAUSE FIVE SECONDS
        new CountDownTimer(5000,1000){

            @Override
            public void onTick(long miliseconds){}

            @Override
            public void onFinish(){
               //after 5 seconds draw the second line
               canvas.drawLine(0, h/2, w-1, h/2, paint);
            }
        }.start();

    }

Regards,

小伙你站住 2024-11-26 18:54:57

不要在 UI 线程中调用 onDraw 方法中等待,否则您将阻止它。使用标志来处理将绘制哪条线

boolean shouldDrawSecondLine = false;

public void setDrawSecondLine(boolean flag) {
    shouldDrawSecondLine = flag;
}

public void onDraw(Canvas canvas) {
    int w = canvas.getWidth();
    int h = canvas.getHeight();
    canvas.drawLine(w/2, 0, w/2, h-1, paint);
    if (shouldDrawSecondLine) {
        canvas.drawLine(0, h/2, w-1, h/2, paint);
    }
}

而不是像这样在代码中使用它

final View view;
// initialize the instance to your view
// when it's drawn the second line will not be drawn

// start async task to wait for 5 second that update the view
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
    @Override
    protected Void doInBackground(Void... params) {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        view.setDrawSecondLine(true);
        view.invalidate();
        // invalidate cause your view to be redrawn it should be called in the UI thread        
    }
};
task.execute((Void[])null);

Don't wait in onDraw method it's called in the UI thread and you'll block it. Use flags to handle which line will be drawn

boolean shouldDrawSecondLine = false;

public void setDrawSecondLine(boolean flag) {
    shouldDrawSecondLine = flag;
}

public void onDraw(Canvas canvas) {
    int w = canvas.getWidth();
    int h = canvas.getHeight();
    canvas.drawLine(w/2, 0, w/2, h-1, paint);
    if (shouldDrawSecondLine) {
        canvas.drawLine(0, h/2, w-1, h/2, paint);
    }
}

Than use it in your code like this

final View view;
// initialize the instance to your view
// when it's drawn the second line will not be drawn

// start async task to wait for 5 second that update the view
AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {
    @Override
    protected Void doInBackground(Void... params) {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        view.setDrawSecondLine(true);
        view.invalidate();
        // invalidate cause your view to be redrawn it should be called in the UI thread        
    }
};
task.execute((Void[])null);
耳钉梦 2024-11-26 18:54:57

嗯...您可以在(这将在其他方法中)设置一个标志,并在您的 onDraw() 中根据该标志的值绘制该线。

即也许类似(虽然我不确定为什么你需要暂停)

invalidate();
pause/sleep();
//set flag
invalidate();

Well... you could set a flag in (which would be in some other method) and within your onDraw() based on the value of this flag draw that line.

i.e. maybe something like (though I'm not sure why you would need a pause)

invalidate();
pause/sleep();
//set flag
invalidate();
我要还你自由 2024-11-26 18:54:57

使用 Thread.sleep(5000) 就可以了。

Use Thread.sleep(5000), that should do it.

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