使用计时器更改毕加索加载图像的网址



当我在计时器中添加毕加索以每 2 分钟更改一次图像的 URL 时应用程序停止工作

我想从网络上获取图像网址数组并将其放在图像视图中非常 2 分钟更改我正在使用毕加索的图像,它在 url 上工作 但是当我输入计时器时,应用程序停止

final String [] url = {"https://png.pngtree.com/thumb_back/fh260/back_pic/00/03/20/63561dc0bf71922.jpg",
"https://placeit-assets.s3-accelerate.amazonaws.com/landing-pages/make-a-twitch-banner2/Twitch-Banner-Blue-1024x324.png",
"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQgYdaf-JhDiFVeQjL6ZRskiF1CRADiJfgDKI3PKBfCMrnnPcHP"};
Timer adtimer = new Timer();
adtimer.schedule(new TimerTask() {
int count = 0  ;
@Override
public void run() {
ImageView Image_view = new ImageView( getActivity());
count++;
if(count >= url.length )
count = 0;
Picasso.get()
.load(String.format(url[count]))
.fit()
.into(Image_view);
}
} , 200 , 5000);

计时器的run不是UI线程,这就是您收到错误的原因。将毕加索放入runOnUiThread,如下所示:

adtimer.schedule(new TimerTask() {
int count = 0  ;
@Override
public void run() {
ImageView Image_view = new ImageView( getActivity());
count++;
if(count >= url.length )
count = 0;
// Any view update should be made in UIThread
runOnUiThread(new Runnable() {
@Override
public void run() {
Picasso.get()
.load(String.format(url[count]))
.fit()
.into(Image_view);
}
});
}
} , 200 , 5000);

最新更新