Does Transparency in Android Layout impact Performance? - android

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.

Related

Is there difference of performance in Views with gradient background and without?

I propose the question in terms of speed performance.
I assume it would be slower for android to build a shape which has a gradient than one that doest. Is that correct?
If so, is the difference significant or not? Even when using lists and many gradiented objects?
The difference in speed should be barely noticeable if there is an advantage between the two,I've tested this multiple times before. I've used solid colors and gradients and both seemed to load at the same time. I don't think it is a significant load of work it has to handle,but processing the gradient may take longer than say a solid color.

Nine-patch versus Vector graphics

I am learning about Android UI and am unclear why people use nine-patch, when you could use vector graphics, because those are scalable without any pixel degradation. I am a beginner in Android, so I hope I am not missing anything here, but it seems like it would be easier to build vector graphics and use those. You would not need the special editor to build them.
Can anyone explain the advantages to using nine-patch over vector? (Don't just explain advantages of nine-patch, as that is already done on StackO., but rather the advantages that vector does not have). Because it seems like Android recommends nine-patch. Thanks.
In vector graphics all side are scale or stretch when we set it to any background whereas in 9-patch we can define which sides can scale or strech so at runtime only those side scale which we set it to scale in 9-patch tool.
from this
-> The advantage of using 9-patch images is that when using it as a background, for instance, the image won't stretch and loose proportions in different screen sizes. the center 'patch' will remain as is and the 'borders' patches will be stretched to fit the screen/view size.
let say you have this image.
and a button with fill parent width. if you set this image to button background it will scale completely and your image gets blur (mean t will expand to button width)and it will not look good. so what 9 - path tool do that you define that online scale some part of image let say if width is fill parent. dont scale whole image . let say we set that after t (in image). scale whole area, so t will not get blur. so this will make good your button.hope you got my point..:)
after making your image 9 patch and setting to button background. your button look like this.
instead of t(in image). whole area expand and fill the buttons width.
Imagine a button with rounded corners. How do you scale it? If you scale it only horizontally, you will have elliptic corners, which would be ugly.
This is what 9-patch is for.
i hate 9patches. i am quite attentive when doing my artwork. i don't even use photoshop. i go with illustrator.
i do everything right when exporting my artwork, i used to do my 9 patches with insane zoom on, maybe check afther that in photoshop for misplaced pixels...
** sad trumpet ** when put on a view, if i used some subtle round corners like 6px or 10px and a stroke everything looks awful at mdpi.
So I solved my problems by marrying the two. I wrote my own 9patch which uses vectors. :)
Everything looks like it's been touched by baby Jesus. Perfect corners, strokes and, best of all, you can use one asset for all the screen sizes, densities while, of course, no more transparent borders, wicked errors because 9patch won't stretch inward, so on and so forth.
I use vectors for icons too. While there sometimes are issues with various effects, these are minimal and easily avoided if you do some reading on how to avoid them.
Best of luck to all you guys!
This is a very basic example.
You can do whatever strikes your fancy. Because of the performance impact of svg's on an app, when first run [or when the user changes appearance options, i like to save the newly generated bitmaps as pngs, if possible.
You don't get any more "best of both worlds" than this.
You can define content are on a 9-patch image which means (for example) text will always be placed in that specific area, I think you cannot do this in Vector image. Android has built-in support for 9-path but for vectors you have to use a library.
9-patch rendering is easy to implement and efficient. If you have an image that can be scaled by stretching horizontal or vertical lines (e.g. buttons or rectangular icons), then use a 9-patch. If you have some icons that don't scale well, then create multiple versions at different resolutions and use Android's resource management to handle it. Both of these approaches are much easier and more efficient than vector graphics.
If you have large images or scenes to render, and you don't want to take up a ton of space with bitmaps, thats when you start thinking about vector graphics.

background image or background color?

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.

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