【问题标题】:Android imageview flickeringAndroid imageview闪烁
【发布时间】:2015-04-08 04:43:19
【问题描述】:

在我的应用程序中,我有列表视图,列表中的每个项目都包含一个图像视图。使用

从位图加载图像
imageView.setImageBitmap(bitmap);

为了处理并发,我使用了以下官方文档中描述的方法。

Processing Bitmaps Off the UI Thread

在高密度设备上,图像视图会闪烁。这个问题有什么可能的解决方案吗?

当我使用句柄并发时,只有闪烁发生,当我使用异步任务时,才没有闪烁

【问题讨论】:

  • 尽量不要使用 Wea​​kReference 。
  • 我没有对 imageview 使用弱引用,但我将它用于 bitmaoworker 异步任务 private final WeakReference bitmapWorkerTaskReference;

标签: android concurrency bitmap imageview flicker


【解决方案1】:

使用毕加索,毕加索为您省去了下载、设置和缓存图像的所有问题。一个简单示例所需的整个代码是:

就这样使用(来自 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;
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-20
    • 2016-07-03
    • 1970-01-01
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多