Reputation: 131
I am trying to find the right approach in setting the placeholder image/resource (VectorDrawable
resource in my case) for an ImageView
that gets set, with the target image bitmap when it is available.
There are two approaches I saw in guides. I can either set a background using the background
property on XML or programmatically using setBackground()
. Then, I would set the target image using setImageDrawable()
.
The second approach is to use setImageDrawable()
for the placeholder and later use setImageDrawable()
again for the target image.
Both approaches work but I have noticed some UI lag when using the first approach. I am not sure if it is caused by the approach or not.
So, I want to know. What is the correct approach in using placeholders and why?
Thank you.
Upvotes: 3
Views: 538
Reputation: 583
when load image in ImageView changed src value, it's mean not changing background. change background is expensive for android, It gets worse when we want to load a lot of images (like ListView, RecyclerView, ...). for having better performance should be changed image src, not changing image background.
for changing image src you can use
image.setImageResource()
or
image.setImageDrawable()
change background suitable for a single image
In addition, all image loader support placeholder if using that's the same doesn't need set manually.
Upvotes: 1
Reputation: 1828
The second approach is always the best for this task. But still, if you want to go with the first approach then the correct way to use it is like this
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
setBackgroundDrawable();
} else {
setBackground();
}
Upvotes: 1