adapter中加载图片
/**
* 加载图片的时候,,,异步加载出现了现象,闪烁,错位,跳动* 原因:这里图片的加载是异步加载,,,,适配器做了优化....复用的优化
*
* 当前的url路径跟当前的imageView控件不匹配....
*/
//1.
holder.imageView.setTag(newslistBean.getPicUrl());
//3.设置一个默认的图片显示
holder.imageView.setImageResource(R.mipmap.ic_launcher);
//2.写一个接口回调bitmap
new ImageHelper().dispalyImage(holder.imageView,newslistBean.getPicUrl(), new ImageCallBack() {
@Override
public void setSuccessImage(Bitmap bitmap, ImageView imageView) {
//设置图片的显示
if (newslistBean.getPicUrl().equals(imageView.getTag())){
imageView.setImageBitmap(bitmap);
}
}
});
接口类
public interface ImageCallBack {
public void setSuccessImage(Bitmap bitmap, ImageView imageView);
}
图片帮助类
public class ImageHelper {
public void dispalyImage(final ImageView imageView, final String picUrl, final ImageCallBack imageCallBack) {
//根据url路径请求图片进行展示
AsyncTask<Void, Void, Bitmap> asyncTask = new AsyncTask<Void, Void, Bitmap>() {
@Override
protected Bitmap doInBackground(Void... voids) {
try {
URL url = new URL(picUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
//获取
int responseCode = connection.getResponseCode();
if (responseCode == 200){
InputStream inputStream = connection.getInputStream();
//转成图片,返回
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
//调用接口的方法
imageCallBack.setSuccessImage(bitmap,imageView);
/*//展示之前先判断一下imageView的tag,跟当前的url路径是否匹配
if (picUrl.equals(imageView.getTag())){
//设置图片的显示
imageView.setImageBitmap(bitmap);
}*/
}
};
asyncTask.execute();
}
}