如何暂停几秒钟 [英] How to pause a few seconds

查看:221
本文介绍了如何暂停几秒钟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示一个类似的幻灯片图像,但我不能让它暂停5秒显示下一个图像之前...

我试图使线程睡眠,但暂停15秒,显示最后一个(第三)的图像。

 的for(int i = 0;我3;;我++){
    文件imgFile =新的文件(路径[我]);    如果(imgFile.exists()){
        位图MYBITMAP = BitmapFactory.de codeFILE(imgFile.getAbsolutePath());
        mimage.setImageBitmap(MYBITMAP);
    }    // **我要5秒钟线程暂停这里**
}


解决方案

您需要一个处理程序

 处理程序handler1 =新的处理程序();
    的for(int i = 0;我3;;我++)
    {
        handler1.postDelayed(新的Runnable(){            @覆盖
            公共无效的run()
            {
                文件imgFile =新的文件(路径[我]);                如果(imgFile.exists())
                {                    位图MYBITMAP = BitmapFactory.de codeFILE(imgFile.getAbsolutePath());
                    mimage.setImageBitmap(MYBITMAP);
                }
            }
        },5000 * I);
    }

I want to display images like a slideshow but I can't make it pause for 5 seconds before displaying the next image...

I have tried to make the thread sleep but it paused for 15 seconds and displayed the last (3rd) image.

for (int i = 0; i < 3; i++) {
    File imgFile = new File(paths[i]);

    if(imgFile.exists()){
        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
        mimage.setImageBitmap(myBitmap);
    }

    //**I want the thread pause for 5 seconds here**
}

解决方案

You need a handler

    Handler handler1 = new Handler();
    for(int i=0;i<3;i++) 
    {
        handler1.postDelayed(new Runnable() {

            @Override
            public void run() 
            {
                File imgFile = new  File(paths[i]);

                if(imgFile.exists())
                {

                    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                    mimage.setImageBitmap(myBitmap);
                }
            }
        }, 5000 * i );
    } 

这篇关于如何暂停几秒钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆