SlideShare a Scribd company logo
1 of 9
Download to read offline
Lazy Loading in Lists

          Janusz Leidgens
          @Killerdackel
          Plattformmanager Android
          Kupferwerk GmbH
Classic Lazy Loading Pattern
public View getView(int position, View convertView, ViewGroup parent) {
    … setupView


   image = loadImage(url);
   if(image != null) {
       imageView.setImageBitmap(image);
   } else {
       Task loadImage = new Task() {
           protected Void doInBackground(Void... params) {
               image = loadImageFromNetwork(url)
           }

           protected void onPostExecute(Void result) {
               if(image != null && imageView.getTag().equals(url)) {
                   imageView.setImageBitmap(image)
               }
               … setup no image loaded view
           }
       }
       imageView.setTag(url);
       loadImage.execute();
   }
Problems

●   Imageview redraw is triggered asynchronously
●   Scrolling behavior suffers
●   Gallery becomes unusable
Better solution
public View getView(int position, View convertView, ViewGroup parent) {
    … setupView


   image = getImage(url);
   if(image != null) {
       imageView.setImageBitmap(image);
   } else {
       Task loadImage = new Task() {
           protected Void doInBackground(Void... params) {
               image = loadImageFromNetwork(url)
           }

           protected void onPostExecute(Void result) {
               if(image != null) {
                   adapter.notifydatasetchanged();
               }
               … setup no image loaded view
           }
       }
       loadImage.execute();
   }
Advantages

●   Listadapter chooses best moment to redraw
●   Scrolling is smooth
Problems

●   All cells on the screen are redrawn
●   Special care needed to prevent multiple
    downloads
Downloading Images

●   Save image urls to queue
●   Prevent double entries in queue
●   Choose a device specific number of workers
●   Download images as long queue is not empty
How to download one image?

●   Check if image is in In-Memory-Cache → use
●   Check if image is on disk → load to memory
●   Load image from internet → save to disk
Correct in memory Caching

    ●    Classic caches use Softreferences
    ●    Android frees Softreferences immediately
    ●    LRUCache (Support Package)
public class BitmapCache extends LruCache<String, Bitmap> {
    private static final int FIRST_HONEYCOMB_API_INT = 11;

        public BitmapCache(int maxSize) {
            super(maxSize);
        }




        @Override
        protected int sizeOf(String key, Bitmap value) {
            if (Build.VERSION.SDK_INT > FIRST_HONEYCOMB_API_INT) {
                return value.getByteCount();
            }
            return value.getHeight() * value.getWidth() * 4;
        }
}

More Related Content

More from Droidcon Berlin

new_age_graphics_android_x86
new_age_graphics_android_x86new_age_graphics_android_x86
new_age_graphics_android_x86
Droidcon Berlin
 
Testing and Building Android
Testing and Building AndroidTesting and Building Android
Testing and Building Android
Droidcon Berlin
 
Matchinguu droidcon presentation
Matchinguu droidcon presentationMatchinguu droidcon presentation
Matchinguu droidcon presentation
Droidcon Berlin
 
Cgm life sdk_droidcon_2014_v3
Cgm life sdk_droidcon_2014_v3Cgm life sdk_droidcon_2014_v3
Cgm life sdk_droidcon_2014_v3
Droidcon Berlin
 
The artofcalabash peterkrauss
The artofcalabash peterkraussThe artofcalabash peterkrauss
The artofcalabash peterkrauss
Droidcon Berlin
 
Raesch, gries droidcon 2014
Raesch, gries   droidcon 2014Raesch, gries   droidcon 2014
Raesch, gries droidcon 2014
Droidcon Berlin
 
Android open gl2_droidcon_2014
Android open gl2_droidcon_2014Android open gl2_droidcon_2014
Android open gl2_droidcon_2014
Droidcon Berlin
 
20140508 quantified self droidcon
20140508 quantified self droidcon20140508 quantified self droidcon
20140508 quantified self droidcon
Droidcon Berlin
 
Tuning android for low ram devices
Tuning android for low ram devicesTuning android for low ram devices
Tuning android for low ram devices
Droidcon Berlin
 
Froyo to kit kat two years developing & maintaining deliradio
Froyo to kit kat   two years developing & maintaining deliradioFroyo to kit kat   two years developing & maintaining deliradio
Froyo to kit kat two years developing & maintaining deliradio
Droidcon Berlin
 
Droidcon2013 security genes_trendmicro
Droidcon2013 security genes_trendmicroDroidcon2013 security genes_trendmicro
Droidcon2013 security genes_trendmicro
Droidcon Berlin
 
Droidcon2013 commercialsuccess rannenberg
Droidcon2013 commercialsuccess rannenbergDroidcon2013 commercialsuccess rannenberg
Droidcon2013 commercialsuccess rannenberg
Droidcon Berlin
 
Droidcon2013 bootstrap luedeke
Droidcon2013 bootstrap luedekeDroidcon2013 bootstrap luedeke
Droidcon2013 bootstrap luedeke
Droidcon Berlin
 
Droidcon2013 app analytics_huber_1und1
Droidcon2013  app analytics_huber_1und1Droidcon2013  app analytics_huber_1und1
Droidcon2013 app analytics_huber_1und1
Droidcon Berlin
 
Droidcon2013 facebook stewart
Droidcon2013 facebook stewartDroidcon2013 facebook stewart
Droidcon2013 facebook stewart
Droidcon Berlin
 
Droidcon 2013 ui smartphones tam hanna
Droidcon 2013 ui smartphones tam hannaDroidcon 2013 ui smartphones tam hanna
Droidcon 2013 ui smartphones tam hanna
Droidcon Berlin
 
Droidcon 2013 connected services burrel_ford
Droidcon 2013 connected services burrel_fordDroidcon 2013 connected services burrel_ford
Droidcon 2013 connected services burrel_ford
Droidcon Berlin
 
Droidcon2013 topapps benninghaus_telekom
Droidcon2013 topapps benninghaus_telekomDroidcon2013 topapps benninghaus_telekom
Droidcon2013 topapps benninghaus_telekom
Droidcon Berlin
 

More from Droidcon Berlin (20)

droidparts
droidpartsdroidparts
droidparts
 
new_age_graphics_android_x86
new_age_graphics_android_x86new_age_graphics_android_x86
new_age_graphics_android_x86
 
5 tips of monetization
5 tips of monetization5 tips of monetization
5 tips of monetization
 
Testing and Building Android
Testing and Building AndroidTesting and Building Android
Testing and Building Android
 
Matchinguu droidcon presentation
Matchinguu droidcon presentationMatchinguu droidcon presentation
Matchinguu droidcon presentation
 
Cgm life sdk_droidcon_2014_v3
Cgm life sdk_droidcon_2014_v3Cgm life sdk_droidcon_2014_v3
Cgm life sdk_droidcon_2014_v3
 
The artofcalabash peterkrauss
The artofcalabash peterkraussThe artofcalabash peterkrauss
The artofcalabash peterkrauss
 
Raesch, gries droidcon 2014
Raesch, gries   droidcon 2014Raesch, gries   droidcon 2014
Raesch, gries droidcon 2014
 
Android open gl2_droidcon_2014
Android open gl2_droidcon_2014Android open gl2_droidcon_2014
Android open gl2_droidcon_2014
 
20140508 quantified self droidcon
20140508 quantified self droidcon20140508 quantified self droidcon
20140508 quantified self droidcon
 
Tuning android for low ram devices
Tuning android for low ram devicesTuning android for low ram devices
Tuning android for low ram devices
 
Froyo to kit kat two years developing & maintaining deliradio
Froyo to kit kat   two years developing & maintaining deliradioFroyo to kit kat   two years developing & maintaining deliradio
Froyo to kit kat two years developing & maintaining deliradio
 
Droidcon2013 security genes_trendmicro
Droidcon2013 security genes_trendmicroDroidcon2013 security genes_trendmicro
Droidcon2013 security genes_trendmicro
 
Droidcon2013 commercialsuccess rannenberg
Droidcon2013 commercialsuccess rannenbergDroidcon2013 commercialsuccess rannenberg
Droidcon2013 commercialsuccess rannenberg
 
Droidcon2013 bootstrap luedeke
Droidcon2013 bootstrap luedekeDroidcon2013 bootstrap luedeke
Droidcon2013 bootstrap luedeke
 
Droidcon2013 app analytics_huber_1und1
Droidcon2013  app analytics_huber_1und1Droidcon2013  app analytics_huber_1und1
Droidcon2013 app analytics_huber_1und1
 
Droidcon2013 facebook stewart
Droidcon2013 facebook stewartDroidcon2013 facebook stewart
Droidcon2013 facebook stewart
 
Droidcon 2013 ui smartphones tam hanna
Droidcon 2013 ui smartphones tam hannaDroidcon 2013 ui smartphones tam hanna
Droidcon 2013 ui smartphones tam hanna
 
Droidcon 2013 connected services burrel_ford
Droidcon 2013 connected services burrel_fordDroidcon 2013 connected services burrel_ford
Droidcon 2013 connected services burrel_ford
 
Droidcon2013 topapps benninghaus_telekom
Droidcon2013 topapps benninghaus_telekomDroidcon2013 topapps benninghaus_telekom
Droidcon2013 topapps benninghaus_telekom
 

Recently uploaded

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 

Recently uploaded (20)

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 

Droidcon 2012: Lazy loading in lists

  • 1. Lazy Loading in Lists Janusz Leidgens @Killerdackel Plattformmanager Android Kupferwerk GmbH
  • 2. Classic Lazy Loading Pattern public View getView(int position, View convertView, ViewGroup parent) { … setupView image = loadImage(url); if(image != null) { imageView.setImageBitmap(image); } else { Task loadImage = new Task() { protected Void doInBackground(Void... params) { image = loadImageFromNetwork(url) } protected void onPostExecute(Void result) { if(image != null && imageView.getTag().equals(url)) { imageView.setImageBitmap(image) } … setup no image loaded view } } imageView.setTag(url); loadImage.execute(); }
  • 3. Problems ● Imageview redraw is triggered asynchronously ● Scrolling behavior suffers ● Gallery becomes unusable
  • 4. Better solution public View getView(int position, View convertView, ViewGroup parent) { … setupView image = getImage(url); if(image != null) { imageView.setImageBitmap(image); } else { Task loadImage = new Task() { protected Void doInBackground(Void... params) { image = loadImageFromNetwork(url) } protected void onPostExecute(Void result) { if(image != null) { adapter.notifydatasetchanged(); } … setup no image loaded view } } loadImage.execute(); }
  • 5. Advantages ● Listadapter chooses best moment to redraw ● Scrolling is smooth
  • 6. Problems ● All cells on the screen are redrawn ● Special care needed to prevent multiple downloads
  • 7. Downloading Images ● Save image urls to queue ● Prevent double entries in queue ● Choose a device specific number of workers ● Download images as long queue is not empty
  • 8. How to download one image? ● Check if image is in In-Memory-Cache → use ● Check if image is on disk → load to memory ● Load image from internet → save to disk
  • 9. Correct in memory Caching ● Classic caches use Softreferences ● Android frees Softreferences immediately ● LRUCache (Support Package) public class BitmapCache extends LruCache<String, Bitmap> { private static final int FIRST_HONEYCOMB_API_INT = 11; public BitmapCache(int maxSize) { super(maxSize); } @Override protected int sizeOf(String key, Bitmap value) { if (Build.VERSION.SDK_INT > FIRST_HONEYCOMB_API_INT) { return value.getByteCount(); } return value.getHeight() * value.getWidth() * 4; } }