background image or background color? - android

If you have an activity and you need to set the background color of the activity, simply you would set the background color as
android:background="#0000FF"
or prefer to use an image as
android:background="#drawable/bgimg"
, taking performance and space requirement into consideration?
I think as using image consume space, when ever possible this approach should be avoided. Is it so, or this really does not make a significant difference? Just need to be sure from the experts.
thanks.

First of all this depends upon your need. If you only want a plain Red background then applying background color should be the preferred one.
As working with images may have few odds,
Scaling of the image for various screen sizes.
They definitely consume memory.
May not give the desired effect (blurry at times) if re-sized by the OS automatically.
On the other hand if you need something like a combination of colors then Gradient will help us. (not for the complex ones though :))
At last if you have an image type of background then drawable images will certainly be the choice.

color would be better, since you might have to use different image sizes to suit different screens, which in turn will increase the app size and you might not be very sure that the images are well displayed. And if you opt for higher resolution images, chances are that the app might crash with a fatal signal error just because it runs out of memory.
And #Atul O Holic's answer is complete too.

As little as the difference could be, the color approach is always going to be cheaper when we think in the space required, because you don't need to have a file with the image resource. Also, you can always use a native color from android using android:color/[color].
Using an image as background is only better when you can't have the same effect by just using a single color as background.

If it's one solid colour just set the background colour (you can also set the colour in your resources so you wouldn't need to keep setting it!).

Totally agree with joa2fast4u, when targeting multiple devices, especially ones with low memory, any optimizations you can make to save memory/reads/writes is preferred. Also, you can specify many preset styles in your styles/themes resource files to propagate and control the entire theme of your app from the default background to the text color on your widgets.

1) Solid Color - Apply a background color
2) Multiple Colors - Use a Gradient
3) Anything more complex - Use images
Please note that the usage of images involves optimization of multiple screen resolutions & sizes. File size & device form factor is also an important factor to be considered here.

Related

Which one is faster Color Filtering or use different drawables in android?

i'm using icons on recyclerview row items.And i'm changing icon's colors with Color Filter option in android but i'm wondering which one is faster and better?
different colored drawables
(run-time) color filter on white
drawable icon
I already use color filtering on white drawable but it sometimes does not appear in list row till refresh recyclerview so i just think about to use different drawables.
thank you for your interest...
It is hard to say, but I would say that changing the drawable is faster, and this is why I think this:
in order to filter a image the system has to read each pixel from RAM, transform each RGB component in order to add the tint (it's not as simple as adding +3 to the intensity of each pixel), and then write each pixel
changing the drawable implies reading from the flash memory and load it into ram
The thing is that flash storage on mobile devices is pretty fast too these days so I consider it's faster to load than tint images.
BUT it depends on how you create the layout. If you load a image the Android system will recalculate the whole layout if other elements change position when you change the drawable. I don't think they wrote code to keep the layout the same if the new drawable has the same size as the last one, and this will involve more calculations than a simple tinting.
color filtering is better because it use GPU( so it faster) and it can decries size of your final apk but sometime using hardware accelerator can make your application laggy and slow because of leak of memory ,size of your drawable, bad design and ...

Does Transparency in Android Layout impact Performance?

I come from an iOS background, where one of the rules for fast views is to avoid transparent backgrounds and pngs if possible. I haven't found any information about this on Android. So my question is, should I use non-transparent views and drawables where possible, or does Android not care about this as much as iOS does?
just like #blackbelt said, transparency always impacts on performance. When possible, avoid using transparent views and/or images.
Also, in Android you can have translucent views/activities. Those will impact performance a few orders of magnitude more than simple transparency. If possible, avoid those too.
You might want to check out the Android documentation regarding overdraw.
Unlike standard overdraw, in which the system completely hides
existing drawn pixels by drawing opaque pixels on top of them,
transparent objects require existing pixels to be drawn first, so that
the right blending equation can occur.

Android... positioning components accurately over background image

I want my app to run on a wide variety of android devices in portrait only. I have a whole-screen background image that includes borders for other components. I want to place items like listviews/sponsor ads accurately within those borders... overlay them on top of the background in exactly the right place. Is using Framelayout the best/easiest way to do this? How do I make sure I get the listviews or sponsor ads in exactly the right place, and exactly the right size given that the background will be stretched to different dimensions for different devices? I've considered cutting up the background image instead but that seems like more work. What's the best approach? Thanks for any thoughts!
Unfortunately, this scheme is never going to work consistently across all screen sizes. If you want borders, you need to create 9-patch graphics out of those and assigne them as teh background to the UI item.

Android Creating Custom Buttons

I have quite a few buttons in my application, they vary in sizes based on the text inside. I want to customize the buttons but I am not sure if I should use
A) Ninepatch pngs
B) Android xml created
C) Photoshop pngs
I want the buttons to scale nicely and therefore not look distorted based on screen size. What are the positives and negatives of the above methods? Is there an ideal way to go about this?
It depends on what they should look like. Rounded corners? Complicated images? Drop shadows? Plain colors?
If you want simple buttons that are monotone in color, xml drawables are the best solution. You can do rounded corners through xml, so if plain is what you are looking for this is the best option.
If you are working with an image, then a 9 patch is the way to go. Play around with the draw9patch tool that comes with the sdk to get the hang of it. You also don't necessarily only need to leave the corners in tack, sometimes you may want to break up the resizable area depending on the image.
PNGs are the last resort as they are the least flexible of the 3 options.
By the way, you should also consider using a state list for all of your buttons, regardless of how you implement the drawable that the button uses. See the link below:
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

Should I draw or load an image?

I am wondering what would be the best practice for android. To draw or to load an image?
For example I want to have a circle that is green filled with the text 25 in it.
-Should I have it in .PNG image file and just load it to the imageview placeholder when I need it?
OR should I load it in Java (by determining the position in java and load it from my res folder)?
OR Should I draw it (draw a circle, fill color, add text field inside)?
On another note, to have a checkmark, what do advise me to do?I don't even know how to draw that
I really need your help in this
Thank you so much
I'd actually argue that there is a single right answer. You should use a shape with a set color and text view inside it. It is easy to do what you've described in xml, see: Oval Gradient in Android for an example (there are a number if you google for android shapes). This will be less CPU/memory intensive than loading a bitmap, and more importantly, it will work for and look good on every screen size. As an added bonus of the user has changed their default font sizes, a textView/shape can respect that and a static image can not. Just set the background of your TextView to be the shape drawable (either in xml or programatically).
To get a bitmap to look good on multiple devices (a must for android development) , you will need a number of different versions of the same bitmap at different resolutions (XHDPI, HDPI, MDPI... etc).
Check marks are harder because they don't conform to a standard shape. Those you probably will have to make pngs for.
It depends on what you want to do.
Rendering a bitmap will be faster, but you cannot manipulate it after it is drawn.
So, for your case, you have a green circle with 25 written it. Do you also need a green ball with 23 and another with 24 in it? What about other colors?
It really depends on the amount of variation you need. At a certain point the overhead of using dozens of images will not be worth the evert of drawing to a canvas. Or, depending on how complex the images are, it will be.
The question is not "Which is better?", but instead, "Which is better for what I need to do?"
I'm not an expert on Android development but after seeing your question I have done a bit of reading and came across this article on displaying images with android that is relevant and includes some code to possibly help you out. http://www.javacodegeeks.com/2011/07/android-game-development-displaying.html
As for your image of a check mark, why dont you just go into photoshop or paint and use the line tool to draw a checkmark and then fill it in with color and save it as a .png perhaps?

Categories

Resources