Reputation: 5642
What are the pros and cons between the two scenarios below:
Using a LayoutView (such as RelativeLayout/LinearLayout) and invoking setBackground(drawable) with a Drawable object
Using an ImageView (width: fill_parent, height: fill_parent) inside of a LayoutView and invoking the following:
Is approach #2 better performance? And if so, which would be the recommended way of setting the background for an ImageView?
Upvotes: 2
Views: 637
Reputation: 6892
I would stick with approach #1, if I just wanted to have a simple background for my layout. Note that you have more than just that method you are referring for applying a background over a LayoutView
, like:
myLayout.setBackgroundColor(int color);
myLayout.setBackgroundDrawable(Drawable d);
myLayout.setBackgroundResource(int resid);
Now, the reasons for choosing approach #1:
LayoutView
size. In approach #2, that task would have to be executed by yourself, using ImageView
XML
attributes. ImageView
and other possible child Views
of your LayoutView
(relative positions, sizes, order), because they will be related in one way or another. In approach #1, your LayoutView
background will never mess around with the child Views
.RelativeLayout
with a child ImageView
whose only function is defining a background does not make sense, unless it is a very special case. You have the android:background
attribute for that. In this case, less code makes better code.Seems like there are more than enough reasons for using approach #1.
For the second part of your question, the best way of setting a background with a Drawable depends on the origin of that Drawable (from resources, generated Drawable object, etc) and what you want to do with that Drawable (passing it to Bitmap and make some changes, etc).
You can check the documentation for each of those methods to better understand when you should use each of them.
Upvotes: 1