0
     ImageView image = (ImageView) findViewById(R.id.imageview);

     image.setImageResource(drawable.image1);
     SystemClock.sleep(1000);
     image.setImageResource(drawable.image2);

我正在尝试更改图像一秒钟,我上面的代码不起作用但不知道为什么?我应该使用线程吗?或者有没有人有更好的想法?

编辑为了澄清这个问题:

显示为“drawable.image2”的图像我希望“drawable.image1”显示一秒钟,然后更改为“drawable.image2”。

编辑2:

此代码在 onClick 中使用。当用户单击图像时,它需要更改一秒钟

4

5 回答 5

1

I'd recommend using a TimerTask with a Timer. You can set it up like this:

protected void showDelayedImages() {

    mImageView.setImageResource(resId1);

    Timer timer = new Timer();
    timer.schedule( new MyTimerTask(), 1000 );
}

private class MyTimerTask extends TimerTask {
    @Override
    public void run() {

        runOnUiThread( new Runnable() {

            @Override
            public void run() {

                mImageView.setImageResource(resId2);
            }
        } ); 
    }
}
于 2012-04-04T15:53:02.160 回答
0

It looks like you're performing the "switch" in an onCreate() method, the sleep will probably just make your Activity load slower since at this stage there isn't actually anything written to the page.

To have your image change you need to perform the switch on the UI thread and you need to perform it after the image has been inflated and added to the page.

Try adding this code in an "onClick" event.

于 2012-04-04T15:51:16.923 回答
0
Thread.sleep(1000);

应该这样做。虽然也有更好的方法。

于 2012-04-04T15:48:47.950 回答
0

使用调试模式并在每次调用时设置断点setImageResource。逐步检查是否每个都被调用以查看您的图像是否正确更改。

在实际情况下,您可能希望根据某些用户操作更改图像,或者例如在线程处理时更改图标,然后在完成时更改它。对于此示例,请查看AsyncTask.

于 2012-04-04T15:49:22.757 回答
0

使用R.drawable.image1而不是drawable.image1

于 2012-04-04T15:49:48.360 回答