Example of this is for setImageResource vs. setImageDrawable
same goes for a Background?
What is the preferred choice then?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
According to the documentation
setImageResourcedoes Bitmap reading and decoding on the UI thread, which can cause a latency hiccup. So your question would be better if you’d ask in which casessetImageResourceis a better choice abovesetImageDrawable.My guess would be that the
setImageResourceis used for lazy loading images, andsetImageDrawableis used when you already loaded the image through a Drawable during view initiation. Which one is suiting you best probably depends on whether you have more serious memory constraints or not; in my experience,setImageResourceis scarcely ever needed.And to answer why your code doesn’t work; this is because you can’t do
getDrawable()from a resourceId 0, this throws an exception as it can’t find the resource. SincesetImageResourceis lazy loading, it checks if the resource is 0 before creating the Drawable, and hence throws no exception.You could probably refactor it to an
if(checked) setImageDrawable(..) else setImageDrawable(null);