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

change url of picasso loaded image using timer

当我在计时器中添加 picasso 以每 2 分钟更改 url 图像时应用程序停止工作

我想从网络上获取图像数组 url 并将其放在图像视图中 2 分钟后更改我正在使用 picasso 的图像并且它适用于 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 线程,这就是您收到错误的原因。将 Picasso 放入 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);