I’m making my first app and I’m quite happy with it, yet I need help with one thing: when setting the image for an ImageView, should I do this?
if (blah > 0 && blah < 10) {
Drawable question_night = res.getDrawable(R.drawable.wt_you_should_wear_2);
questionImg.setImageDrawable(question_night);
}
Or this?
if (blah > 0 && blah < 10){
questionImg.setImageDrawable(res.getDrawable(R.drawable.wt_you_should_wear_2));
}
I need to know which is better, regarding how much VM heap it uses, whether it affects the speed of the app, if it has problems (leaks e.t.c.).
Thanks in advance.
It will make no difference in performance or memory use. You may well find the first option easier to read and debug though.
(On the other hand, I’d name the variable somewhat differently. It’s not at all clear why
question_nightwould be a reference to aDrawable, and the conventional name would bequestionNightanyway.)EDIT: This answer only compares the two options given, from a Java language perspective. siliconeagle’s answer of passing the resource directly rather than going via a
Drawablecertainly sounds like a cleaner approach to me.