简体   繁体   English

在列表中加载静态图像会减慢滚动速度

[英]Loading static images in list slows down scrolling

My static list consists of images and data.I am loading images in background asynchronously.Still loading of images slows down scrolling performance.If i comment out the image loading code list scrolls very fast.How can i imporove scrolling performance while loading images in list?Below is the code i am using for loading images- 我的静态列表由图像和数据组成。我正在异步加载背景中的图像。图像的加载会降低滚动性能。如果我注释掉图像加载代码列表滚动速度非常快。如何在列表中加载图像时改进滚动性能?以下是我用于加载图片的代码 -

     public View getView(final int i, View v, ViewGroup viewGroup) {
  thumbnailImageView = (ImageView) view.findViewById(R.id.video_thumbnail);
        final String id = video.getId();
        String thumbnailPath = FileUtils.getCachedFileName(0,video.getId());

        BitmapWorkerTask task = new BitmapWorkerTask(thumbnailImageView);

            task2.execute(thumbnailPath);
    .
    .

    }



     class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap>
        {

          private final ImageView imageViewReference;

            public BitmapWorkerTask(ImageView imageView)
            {
              imageViewReference = imageView;
            }





            @Override
            protected Bitmap doInBackground(String... urls) {

                return BitmapFactory.decodeFile(urls[0]);


            }


            @Override
            protected void onPostExecute(Bitmap bitmap)
            {
                imageViewReference.setImageBitmap(bitmap);

            }
        }

You could use Universal Image Library for loading the images in the background. 您可以使用通用图像库在后台加载图像。 It has a feature to stop loading the image onScroll or Fling. 它具有停止在Scroll或Fling上加载图像的功能。

Try Picasso as it has support for local files as well: 尝试Picasso,因为它也支持本地文件:

Picasso.with(context).load(R.drawable.landing_screen).into(imageView1);
Picasso.with(context).load("file:///android_asset/DvpvklR.png").into(imageView2);
Picasso.with(context).load(new File(...)).into(imageView3);

Check the below code..It may help to you...You are not providing full code..So I assumed that you are extending your adapter from BaseAdapter and your list item is having one ImageView 检查下面的代码..它可能对你有所帮助...你没有提供完整的代码..所以我假设你从BaseAdapter扩展你的适配器,你的列表项有一个ImageView

final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);

final int cacheSize = maxMemory / 8;

LruCache<Integer, Bitmap> mMemoryCache = new LruCache<Integer, Bitmap>(
        cacheSize) {
    @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
    @Override
    protected int sizeOf(Integer key, Bitmap bitmap) {
        return bitmap.getByteCount() / 1024;
    }
};

/**
 * This will return a bitmap from the cache
 * 
 * @param key
 * @return
 */
public Bitmap getBitmapFromMemCache(Integer key) {
    return mMemoryCache.get(key);
}

/**
 * This method will add the bitmap to the memory cache
 * 
 * @param key
 * @param bitmap
 */
public void addBitmapToMemoryCache(Integer key, Bitmap bitmap) {
    if (getBitmapFromMemCache(key) == null) {
        mMemoryCache.put(key, bitmap);
    }
}

private View video;

@Override
public View getView(int position, View view, ViewGroup parent) {

    ViewHolder holder = null;

    if (view == null) {
        view = LayoutInflater.from(context).inflate(R.layout.list_item,
                null);
        holder = new ViewHolder();
        holder.thumbnailImageView = (ImageView) view
                .findViewById(R.id.video_thumbnail);

        view.setTag(holder);
    }

    else {
        holder = (ViewHolder) view.getTag();
    }


    if (holder.bitmapWorkerTask!= null && holder.bitmapWorkerTask.getStatus() == Status.RUNNING) {
        holder.bitmapWorkerTask.cancel(true);
    }

    Bitmap bitmapFromMemCache = getBitmapFromMemCache(position);
    if (bitmapFromMemCache == null) {
        holder.thumbnailImageView.setImageResource(R.drawable.default_image);
        holder.bitmapWorkerTask = new BitmapWorkerTask(holder.thumbnailImageView,position);
        String thumbnailPath = FileUtils.getCachedFileName(0, video.getId());
        holder.bitmapWorkerTask.execute(thumbnailPath);
    } else {
        holder.thumbnailImageView.setImageBitmap(bitmapFromMemCache);
    }


    ........
}

private class ViewHolder {
    private ImageView thumbnailImageView;
    private BitmapWorkerTask bitmapWorkerTask;
}

class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap> {
    private final ImageView imageViewReference;
    private int position;

    public BitmapWorkerTask(ImageView imageView,int position) {
        imageViewReference = imageView;
        this.position=position;
    }

    @Override
    protected Bitmap doInBackground(String... urls) {
        Bitmap bitmap= BitmapFactory.decodeFile(urls[0]);
        addBitmapToMemoryCache(position, bitmap);
        return bitmap
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        imageViewReference.setImageBitmap(bitmap);
    }
}

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

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