Android fast Bitmap loading

為{幸葍}努か 提交于 2019-12-03 03:20:30
Praful Bhatnagar

One option would be to use create image cache using the WeakReference so that the image would be removed from the memory when system encounter low memory situation. This way you can keep images in memory and only need load from sdcard when they are not in memory. So your current activity would always keep the hard reference to the bitmap's required and the image cache would keep the weak reference to the bitmap's.

Following is some more information about weak reference:

JavaDoc weakReference

StackOverflow post discussing using weak reference for cache

Pointer Null
  1. Don't load on main thread. With 2.1 sec delay you're close to being killed with ANR (app not responding) error if you block main thread.

  2. Load in separate thread. Don't create 10 threads, but one AsyncTask, and load all your images one after another in doInBackground.

    Loading in AsyncTask should take (nearly) same time as loading in main thread. Don't put too much fancy animations, so that main thread doesn't consume too much CPU time.

One way to do this will be to implement a kind of listener/observer.

From your UI thread, launch other thread that will load the image. Once the image is loaded, thread will invoke a call back method implemented in the activity class to update the corresponding image view.

Instead of android asynch task, I will go for my threads (pooled/Executors). I will get all the possible image paths before loading them, and dispatching them to thread loading the image.And in the call back I know where to updated the image.

Alternatively you can also check how the Loader perform in your case.

rds

Clearly, you should not leave a 2s-long operation in the UI thread.

I suppose the ThreadPoolExecutor is a godd solution, but it is not optimal to create many threads in your pool. On background thread is sufficient. I bet you'll have better performance simply by changing this.

I don't recommend an AsyncTask directly from the Activity, because it is fragile to config change.

I don't recommend working with sprites. Increasing the size of one image is very dangerous on a mobile where memory is limited. In particular with sprites, you'll have to get parts of the Bitmap, hence having the full bitmap in memory for a moment.

Now that I unanwsered your specific questions, I think you go throuh Lazy loading of images in a listview because the problem is very similar.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!