klinker41 / android-chips

Chips in your AutoCompleteTextView on Android

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Inefficient API is used

struggggle opened this issue · comments

Dear developers,

I found an inefficient API, setImageURI(), is used in chips.

For setImageURI(), Google says this:
https://developer.android.com/reference/android/widget/ImageView.html#setImageURI(android.net.Uri)
" This does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup. If that's a concern, consider using setImageDrawable(android.graphics.drawable.Drawable) or setImageBitmap(android.graphics.Bitmap) and BitmapFactory instead. "

I noticed that in the following code, setImageURI() is used:
com.android.ex.chips.DropdownChipLayouter.java bindIconToView() (line number: 181)
https://github.com/klinker41/android-chips/blob/master/library/src/main/java/com/android/ex/chips/DropdownChipLayouter.java#L181

and bindIconToView() is invoked indirectly by:
com/android/ex/chips/RecipientAlternatesAdapter.java getView()
https://github.com/klinker41/android-chips/blob/master/library/src/main/java/com/android/ex/chips/RecipientAlternatesAdapter.java#L546

getView() callback is frequently invoked in the UI thread and this means that setImageURI() will be called many times in the UI thread.

Since setImageURI() is a slow operation, for performance considerations, I think we should perform these operations in worker threads (e.g., via AsyncTask).

In addition, when loading an image, Google suggests us to resize the image before decoding them, so as to save memory resource and avoid OutOfMemory exception.
https://developer.android.com/topic/performance/graphics/load-bitmap.html
" To avoid java.lang.OutOfMemory exceptions, check the dimensions of a bitmap before decoding it, unless you absolutely trust the source to provide you with predictably sized image data that comfortably fits within the available memory "

If the photos processed by setImageURI() are larger than required. Perhaps we should resize the images before reading and decoding them, which can reduce time and memory overhead and help make app more smooth in user interaction (using setImageDrawable(Drawable) or setImageBitmap(android.graphics.Bitmap) and BitmapFactory instead.).

Do you think the use of setImageURI() will affect chips's performance? If yes, for performance considerations, maybe we should optimize it.

Thanks.

Resizing the images shouldn't be an issue. The thumbnails provided by the Contacts ContentProvider are very small as is. I have never seen an OutOfMemory error with the contact chips library, and we have used it in production for 3 years.

Since the images are so small, I don't think that setImageUri will have much of a performance impact, but if you would like to change it, please feel free to open up a PR and I will take a look!