简体   繁体   English

循环使用毕加索来加载网址图片

[英]Looping Picasso to load url image

I want to display images from URL with regular interval using Picasso. 我想使用毕加索定期显示来自URL的图像。 It display single image very fine but when I loop it either it create error or does not show. 它显示单个图像非常好,但是当我循环播放时,它会创建错误或不显示。 I'm pasting my code here that is not displaying any image to image view 我在这里粘贴我的代码,该代码不显示任何图像到图像视图

public class Main extends AppCompatActivity {

    Integer i=1;
    String adurl = "http://mywebsite.com/img";
    String nADURL;
    private Context mContext;
    private int index = 0;
    private final int interval = 3000;
    private final int DURATION = 1500;
    ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 

        //Initializing the ImageView
       iv = (ImageView) findViewById(R.id.ivAdd);

        showAd9 sh = new showAd9();    
        sh.execute("3000");
    }

  public  class showAd9 extends AsyncTask<String, String, String> {

        @Override
        protected String doInBackground(String... params) {

                nADURL = adurl.toString() + String.valueOf(i) + ".png";
                try {
                    int time = Integer.parseInt(params[0]);
                    while(i<=4) {
                        Picasso.with(main.this).load(nADURL).skipMemoryCache().error(R.drawable.wrong).into(iv);
                        i++;
                        Thread.sleep(time);
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {  
        }
    }

Your solution is not straight-forward - I think it's better to do this stuff with help of Handler . 您的解决方案并非直截了当-我认为最好在Handler帮助下完成这些工作。 And the point is that you do the loading with Picasso from background thread. 关键是您要从后台线程使用Picasso进行加载。 Picasso handles this for you, just schedule your interval with Handler.postDelayed() - this should solve your problem. Picasso为您处理此问题,只需使用Handler.postDelayed()安排时间间隔-这应该可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM