//创建 hh.img.setTag(list.get(i).getPicUrl() ); hh.img.setImageResource(R.mipmap.ic_launcher); //创建借口回调 ImagCallBack(hh.img,list.get(i).getPicUrl(), new Imagcall() { @Override public void Callback(ImageView imageView, Bitmap bitmap) { //判断 if(hh.img.getTag().equals(list.get(i).getPicUrl())){ //赋值图片 hh.img.setImageBitmap(bitmap); } } }); return view; } //创建加载图片方法 private void ImagCallBack(final ImageView img, final String picUrl, final Imagcall imagcall) { AsyncTask<Void, Void, Bitmap> asyncTask = new AsyncTask<Void, Void, Bitmap>() { @Override protected Bitmap doInBackground(Void... voids) { //获取路径 String path=picUrl; try { URL url=new URL(path); HttpURLConnection han= (HttpURLConnection) url.openConnection(); han.setRequestMethod("GET"); han.setReadTimeout(5000); han.setConnectTimeout(5000); int code = han.getResponseCode(); if(code==200){ InputStream stream = han.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(stream); return bitmap; } } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Bitmap bitmap) { //调取接口类方法 imagcall.Callback(img,bitmap); } }; asyncTask.execute(); }
/*
*接口类
*/
//创建接口类 public interface Imagcall { public void Callback(ImageView imageView, Bitmap bitmap); }