【问题标题】:Loading static images in list slows down scrolling在列表中加载静态图像会减慢滚动速度
【发布时间】:2015-03-10 11:35:56
【问题描述】:

我的静态列表由图像和数据组成。我在后台异步加载图像。仍然加载图像会降低滚动性能。如果我注释掉图像加载代码列表滚动非常快。如何在加载时提高滚动性能列表中的图像?下面是我用于加载图像的代码-

     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);

            }
        }

【问题讨论】:

  • 显示你的适配器代码!!!
  • @Chandrakanth 上面写的代码只在适配器中
  • 您必须为位图维护 LruCache...
  • @Chandrakanth 你能帮我写代码吗?它将如何解决我的问题?
  • 从哪里获得 getView() 中的“userProfileImageView”引用?提供完整的适配器类代码..让我帮你!!!

标签: java android performance android-listview image-loading


【解决方案1】:

您可以使用Universal Image Library 在后台加载图像。它具有停止加载图像 onScroll 或 Fling 的功能。

【讨论】:

  • 对于 UIL 我需要使用 NetworkImageView..但是由于我的图像是静态的,它们作为缓存存储在设备的外部存储中,所以我使用的是普通的 imageview
  • UIL可以用于静态图片吗?
【解决方案2】:

试试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);

【讨论】:

    【解决方案3】:

    检查下面的代码..它可能对您有所帮助...您没有提供完整的代码..所以我假设您正在从 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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-21
      • 1970-01-01
      • 2013-08-25
      • 1970-01-01
      • 2011-08-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多