Images are repeating in ListView

后端 未结 6 1621
遥遥无期
遥遥无期 2020-12-15 12:49

I have implemented android app which should download images from server and display them in ListView, but very interesting thing occures while images are downloading

6条回答
  •  [愿得一人]
    2020-12-15 13:00

    i had this issue and implemented lruCache ...i believe you need api 12 and above or use the compatiblity v4 library. lurCache is fast memory but it also has a budget, so if your worried about that you can use a diskcache ...its all described here http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

    I'll now provide my implementation which is a singleton i call from anywhere like this:

    //where first is a string and other is a imageview to load

    DownloadImageTask.getInstance().loadBitmap(avatarURL, iv_avatar); 
    

    here's the ideal code to cache and then call the above in getView of an adapter when retrieving the web image:

     public class DownloadImageTask {
    
    private LruCache mMemoryCache;
    
    /* create a singleton class to call this from multiple classes */
    
    private static DownloadImageTask instance = null;
    
    public static DownloadImageTask getInstance() {
        if (instance == null) {
            instance = new DownloadImageTask();
        }
        return instance;
    }
    
    //lock the constructor from public instances
    private DownloadImageTask() {
    
        // Get max available VM memory, exceeding this amount will throw an
        // OutOfMemory exception. Stored in kilobytes as LruCache takes an
        // int in its constructor.
        final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
    
        // Use 1/8th of the available memory for this memory cache.
        final int cacheSize = maxMemory / 8;
    
        mMemoryCache = new LruCache(cacheSize) {
            @Override
            protected int sizeOf(String key, Bitmap bitmap) {
                // The cache size will be measured in kilobytes rather than
                // number of items.
                return bitmap.getByteCount() / 1024;
            }
        };
    
    }
    
    public void loadBitmap(String avatarURL, ImageView imageView) {
        final String imageKey = String.valueOf(avatarURL);
    
        final Bitmap bitmap = getBitmapFromMemCache(imageKey);
        if (bitmap != null) {
            imageView.setImageBitmap(bitmap);
        } else {
            imageView.setImageResource(R.drawable.ic_launcher);
    
            new DownloadImageTaskViaWeb(imageView).execute(avatarURL);
        }
    }
    
    private void addBitmapToMemoryCache(String key, Bitmap bitmap) {
        if (getBitmapFromMemCache(key) == null) {
            mMemoryCache.put(key, bitmap);
        }
    }
    
    private Bitmap getBitmapFromMemCache(String key) {
        return mMemoryCache.get(key);
    }
    
    /* a background process that opens a http stream and decodes a web image. */
    
    class DownloadImageTaskViaWeb extends AsyncTask {
        ImageView bmImage;
    
        public DownloadImageTaskViaWeb(ImageView bmImage) {
            this.bmImage = bmImage;
        }
    
        protected Bitmap doInBackground(String... urls) {
    
            String urldisplay = urls[0];
            Bitmap mIcon = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon = BitmapFactory.decodeStream(in);
    
            } catch (Exception e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }
    
            addBitmapToMemoryCache(String.valueOf(urldisplay), mIcon);
    
            return mIcon;
        }
    
        /* after decoding we update the view on the mainUI */
        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
    
        }
    
    }
    

    }

提交回复
热议问题