Reputation: 4910
Consider these two code samples for the getView method in a ListView Adapter. Are they identical memory wise or does setImageResource work more efficiently?
I know that decodeResource creates a new bitmap and eats a bit of memory every time. So if I were to call it enough times with large enough drawbles it would cause out of memory error. But I couldn't find with setImageResource works in the source code.
public View getView(int position, View convertView, ViewGroup parent) {
ImageView img = new ImageView(context);
img.setImageBitmap(BitmapFactory.decodeResource(getResources(),resIds[position]));
return img;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView img = new ImageView(context);
img.setImageResource(resIds[position]) ;
return img;
}
Upvotes: 1
Views: 423
Reputation: 486
It says in the docs
This does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup.
Furthermore, it says
If that's a concern, consider using setImageDrawable() or setImageBitmap() and android.graphics.BitmapFactory instead.
This already implies an answer to your question: no, it is not more efficient. To save memory and runtime, store the result of the BitmapFactory (or Drawable) and take care that it is not executed every time the ImageView is set.
Upvotes: 2