setImageDrawable vs setBackground for placeholder

Tiano Manti :

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.

GOVIND DIXIT :

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();
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=123431&siteId=1