Android 如何暂停几秒钟
我想像幻灯片一样显示图像,但我不能让它在显示下一张图像之前暂停5秒 我试图使线程睡眠,但它暂停了15秒,并显示了最后的第三个图像Android 如何暂停几秒钟,android,Android,我想像幻灯片一样显示图像,但我不能让它在显示下一张图像之前暂停5秒 我试图使线程睡眠,但它暂停了15秒,并显示了最后的第三个图像 for (int i = 0; i < 3; i++) { File imgFile = new File(paths[i]); if(imgFile.exists()){ Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
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**
}
你需要一个训练员
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 );
}
试试这个:
public class YourClass {
private String[] paths;
private int mNextImageIndex = 0;
public void switch() {
File imgFile = new File(paths[mNextImageIndex]);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
mimage.setImageBitmap(myBitmap);
}
mNextImageIndex = (mNextImageIndex + 1) % paths.length;
new Handler().postDelayed(new Runnable() {
public void run() {
switch();
}
},5000);
}
}
它仍然只显示更新后的最新图像pause@Harish正确地用5000*编辑了我代码的最后一行*我-你在测试中包括了这个吗?@Smittey啊!不,我没有。在您的回答中,我注意到的第一件事是您的处理程序中的延迟时间,我觉得其中有问题: