28

When it comes android development and performance, if you have imageview then which one would be faster or better (is there a difference anyways!): setImageResource and setDrawable Iknow one takes ID and the other one takes drawable. But I will be getting the drwable from ID anyways. Which one do you think is better?

Or should I avoid using imageview alltogether and use load/draw bitmap? too many selections is confusing Thanks for the comments

Snake
  • 14,228
  • 27
  • 117
  • 250

2 Answers2

74

They are different. From Android documents.

setImageResource: 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.

Seunghoon
  • 5,632
  • 5
  • 35
  • 41
  • 3
    In my GridView adapter I used setImageResource and it lagged on scrolling. When I changed calls to setImageBitmap, GridView began to scroll smoothly. Execution time of these 2 methods is obviously different. However, it makes no difference what to use, if developer works with static views (which are not moved). – Eugene Chumak Feb 12 '14 at 14:27
  • Accepted the new answer :) – Snake May 21 '15 at 05:11
  • `setImageDrawable` is also too slow – Konstantin Konopko Mar 18 '16 at 14:28
  • @konopko could you elaborate please? Is setImageDrawable demonstrably slower as compared to setImageResource? – Kartik Chugh Aug 07 '16 at 16:12
2

I think setImageDrawable is better suited for memory intensive operations - like scrolling a RecyclerView, getting bitmap from memcache. But for something simpler, setImageResource is the better API nowadays.

IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147