在我的应用程序中,我有列表视图,列表中的每个项目都包含一个Image视图。使用从位图加载图像
imageView.setImageBitmap(bitmap);
为了处理并发性,我使用了以下官方文档中描述的方法。
在UI线程外处理位图
在高密度设备上,图像视图会闪烁。这个问题有什么可能的解决方案吗?
当我使用handle并发时,只有闪烁发生,当我只使用asynctask时,没有闪烁的
使用Picasso,Picasso为您省去了下载、设置和缓存图像的所有问题。一个简单示例所需的全部代码是:
就像这样使用(从UI线程):
在listview中,在getView()中使用它,它处理缓存和其他操作。。
Picasso.with(context)
.load(url)
.into(imageView);
http://square.github.io/picasso/
或
ImageView tre = (ImageView) findViewById(R.id.imageview);
String URL = "http://www...sdsdsd ...";
mChart.setTag(URL);
new DownloadImage.execute(tre);
public class DownloadImage extends AsyncTask<ImageView, Void,Bitmap> {
ImageView imageView = null;
@Override
protected Bitmap doInBackground(ImageView... imageViews) {
this.imageView = imageViews[0];
return download_Image((String)imageView.getTag());
}
@Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
private Bitmap download_Image(String url) {
Bitmap bmp =null;
try{
URL ulrn = new URL(url);
HttpURLConnection con = (HttpURLConnection)ulrn.openConnection();
InputStream is = con.getInputStream();
bmp = BitmapFactory.decodeStream(is);
if (null != bmp)
return bmp;
}catch(Exception e){}
return bmp;
}