如何设置有延迟的图像

发布于 2024-11-05 19:10:49 字数 1174 浏览 1 评论 0原文

我有一个包含三张图像的“活动”。当点击一张图片时,所有图片都会切换到另一张图片。有没有办法进行切换,使图像实际更改之前有 2 秒的延迟(即下面的每个“for 循环”中都有 2 秒的延迟)?我试图用计时器来做到这一点,但实际上并没有延迟我运行程序时的延迟:

protected void onCreate(Bundle savedInstanceState) {
image1.setOnClickListener(this);

@Override
public void onClick(View arg0) 
{
do_switches();
}

private void do_switches() 
{
//loop through all images and change them
for(int j=1 ;j<=3; j++)
{           


 final int curr2=current;
 final Handler handler = new Handler(); 
 Timer t = new Timer(); 
 t.schedule(new TimerTask() { 
       public void run() { 
        handler.post(new Runnable() { 
               public void run() { 

        switch(curr2){

        case 1:

            image1.setImageResource(ImageArray[1]);
                    break;

        case 2:

            image2.setImageResource(ImageArray[2]);
                             break;

        case 3:
            image3.setImageResource(ImageArray[3]);
                        break;

        }
            } 
        }); 
    } 
 }, 2000); 
}

}

我也尝试过仅使用 SystemClock.sleep(2000) 而不是计时器,但我也没有工作。我也尝试过用 try/catch 设置一个线程,但运气不佳,或者可能是我没有正确实现它。有没有办法让 for 循环的每次迭代都延迟? 谢谢

I have an "Activity" with three images. When one image is clicked, all the images are to switch to another picture. Is there a way to make the switches such that there is a 2 second delay before the images actually change (i.e. a 2 second delay in every 'for loop' below)? I am trying to do this with a timer but it does not actually to the delay when I run my program:

protected void onCreate(Bundle savedInstanceState) {
image1.setOnClickListener(this);

@Override
public void onClick(View arg0) 
{
do_switches();
}

private void do_switches() 
{
//loop through all images and change them
for(int j=1 ;j<=3; j++)
{           


 final int curr2=current;
 final Handler handler = new Handler(); 
 Timer t = new Timer(); 
 t.schedule(new TimerTask() { 
       public void run() { 
        handler.post(new Runnable() { 
               public void run() { 

        switch(curr2){

        case 1:

            image1.setImageResource(ImageArray[1]);
                    break;

        case 2:

            image2.setImageResource(ImageArray[2]);
                             break;

        case 3:
            image3.setImageResource(ImageArray[3]);
                        break;

        }
            } 
        }); 
    } 
 }, 2000); 
}

}

I have also tried using just SystemClock.sleep(2000) instead of the timer but I that didnt work either.I also tried setting up a Thread with a try/catch with no luck or maybe I didn't implement it properly. Is there a way to put this delay on every iteration of my for loop?
Thanks

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

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

发布评论

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

评论(2

层林尽染 2024-11-12 19:10:49

不是最好的选择之一,但您仍然可以尝试 CountDownTimer。

http://developer.android.com/reference/android/os/CountDownTimer.html

Not one of best option, but still you can try CountDownTimer.

http://developer.android.com/reference/android/os/CountDownTimer.html

几度春秋 2024-11-12 19:10:49

您可以使用 handler.postDelayed(Runnable r, long timeInMillis)。创建更改图片的可运行程序,然后调用 postDelayed() 并传入可运行程序和 2000 作为时间延迟。

编辑:啊,我明白你想做什么。据我所知,你不可能每次都让 for 循环暂停 2 秒。如果您链接 postDelayed() 调用,您可以获得相同的效果。只需设置下一个可运行对象并在第一个可运行对象中调用 postDelayed() ,第二个可运行对象中的第三个可运行对象也是如此。您最终将获得与每次迭代暂停 2 秒的 for 循环相同的功能。

You can use handler.postDelayed(Runnable r, long timeInMillis). Make your runnable that changes the pictures and then call postDelayed() and pass in the runnable and 2000 for the time delay.

Edit: Ahh I see what you are trying to do. As far as I know you aren't going to be able to make a for loop pause for 2 seconds each time through. You can get the same effect though if you chain the postDelayed() calls. Just set up the next runnable and call postDelayed() on it inside the first one, and same for the third one from inside the second one. You will end up with the same functionality as a for loop that pauses for 2 seconds each iteration.

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