在一段时间后定期从 Arraylist 获取并更新 Imageview
Get from Arraylist and Update Imageview Periodically after some Interval
我准备好了一个数组列表(字符串),其中包含一些图像的网络 link。现在我想在一段时间后(比如 4 秒)使用 Glide 使用 imagelinks 定期更新 imageview。
我在使用新图像更新图像视图之前设置暂停时遇到问题。
我的代码:
//i is the count initially set to 0. StringLink is the arraylist
//StatusImage is the imageview
//first I set the first link from StringLink to StatusImage
Glide.with(ShowStatusPostActivity.this).setDefaultRequestOptions(placeholderRequest).load(StringLink.get(i)).into(StatusImage);
//next I try to set the next links from StringLink to StatusImage
//I try to keep one image for 4 seconds and then change it. Here I use Handler to do so
for(; i< StringLink.size();i++ )
{
try{
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
StatusImage.post(new Runnable() {
@Override
public void run() {
Glide.with(ShowStatusPostActivity.this).setDefaultRequestOptions(placeholderRequest).load(StringLink.get(i))
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
return false;
}
})
.into(StatusImage);
}
});
}
}, 4000);
}
catch (Exception e){}
}
但是我在第二个 Glide 中得到了 ArrayIndexOutofBound 的错误。
我不认为 for 循环中的处理程序是一个很好的周期性的主意,我也试过 Thread.sleep 但它也没有用。
这里有什么我可以做的吗?
希望以下功能对您有所帮助。它将每 4 秒触发一次,并显示一条消息。你可以调用你的函数来根据我的计数器加载你的图像。
int i = 0;
int length = 5;
private Handler mHandler;
private Runnable mRunnable;
public void startPeriodicFunciton() {
mHandler = new Handler();
mRunnable = new Runnable() {
@Override
public void run() {
// Do some task on delay
// doTask();
// You can add your fuction to load image here
i++;
if (i == length)
i = 0;
Toast.makeText(MainActivity.this, "value : " + i,
Toast.LENGTH_SHORT).show();
mHandler.postDelayed(mRunnable, 4000);
}
};
mHandler.postDelayed(mRunnable, (4000));
}
我准备好了一个数组列表(字符串),其中包含一些图像的网络 link。现在我想在一段时间后(比如 4 秒)使用 Glide 使用 imagelinks 定期更新 imageview。 我在使用新图像更新图像视图之前设置暂停时遇到问题。
我的代码:
//i is the count initially set to 0. StringLink is the arraylist
//StatusImage is the imageview
//first I set the first link from StringLink to StatusImage
Glide.with(ShowStatusPostActivity.this).setDefaultRequestOptions(placeholderRequest).load(StringLink.get(i)).into(StatusImage);
//next I try to set the next links from StringLink to StatusImage
//I try to keep one image for 4 seconds and then change it. Here I use Handler to do so
for(; i< StringLink.size();i++ )
{
try{
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
StatusImage.post(new Runnable() {
@Override
public void run() {
Glide.with(ShowStatusPostActivity.this).setDefaultRequestOptions(placeholderRequest).load(StringLink.get(i))
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
return false;
}
})
.into(StatusImage);
}
});
}
}, 4000);
}
catch (Exception e){}
}
但是我在第二个 Glide 中得到了 ArrayIndexOutofBound 的错误。
我不认为 for 循环中的处理程序是一个很好的周期性的主意,我也试过 Thread.sleep 但它也没有用。
这里有什么我可以做的吗?
希望以下功能对您有所帮助。它将每 4 秒触发一次,并显示一条消息。你可以调用你的函数来根据我的计数器加载你的图像。
int i = 0;
int length = 5;
private Handler mHandler;
private Runnable mRunnable;
public void startPeriodicFunciton() {
mHandler = new Handler();
mRunnable = new Runnable() {
@Override
public void run() {
// Do some task on delay
// doTask();
// You can add your fuction to load image here
i++;
if (i == length)
i = 0;
Toast.makeText(MainActivity.this, "value : " + i,
Toast.LENGTH_SHORT).show();
mHandler.postDelayed(mRunnable, 4000);
}
};
mHandler.postDelayed(mRunnable, (4000));
}