我正在尝试从sdcard文件夹图像中逐个加载图片并在几秒后使用处理程序自动更改但不会更改其他图像只显示一个图像
在这里,我将处理程序代码用于get和set image on create activity
HomeActivity.java
final Handler imagehandler = new Handler();
Runnable runnable;
runnable = new Runnable()
{
int i=0;
@SuppressLint("SdCardPath")
public void run()
{ // slider image run
File dir = new File("/mnt/sdcard/images/");
File file[]=dir.listFiles();
for (int i=0;i<file.length;i++)
{
Drawable d = (Drawable) Drawable.createFromPath(file[i].toString());
imageslider.setImageDrawable(d);
imagehandler.postDelayed(this, 4000);
// for interval
}
};
imagehandler.postDelayed(runnable,10);
}
答案 0 :(得分:0)
从Runnable中删除for循环之前,它将始终显示imageView中的最后一个图像。而不是for循环使用计数器来显示图像:
File dir = new File("/mnt/sdcard/images/");
File file[]=dir.listFiles();
public static int count=0;
new Handler().postDelayed(new Runnable() {
public void run() {
if(count<file.length){
Drawable d = (Drawable)
Drawable.createFromPath(file[count].toString());
imageslider.setImageDrawable(d);
count++; //<<< increment counter here
}
else{
// reset counter here
count=0;
}
}
}, 4000);