Add an image button with transparent background - android

I want to add image buttons on my main app screen that have a transparent background. I have created the images in GIMP and saved them with transparent backgrounds in png format but when I add them to my android app in Eclipse they appear with a white background. How do I remove this in my code?

Try using null for the background for your image button in the xml layout.
android:background="#null"

A transparent background generally works when used in photoshop, if it is not, you have to set the alpha bits of the pixels around the border of your image
use alpha-masking( subset of alpha blending, google them to know more).
A little theory: depending on the alpha bits for each pixel in your Bitmap (the translucency bits), the extent of blending of that pixel with over-written pixel is determined. Considering extremes, if alpha is 255, overwriting pixel is used instead of the over-written one (fully opaque, in regular terms); if alpha is 0, overwriting pixel is just ignored (transparent). For other alphas in between: there is blending.
In your case you would have to make the alpha of the border zero, to achieve complete blend. Outlining all the steps in geral:.
The drawable to be used can't be a JPEG( JPEG doesn't store alpha values per pixel). Go for PNGs
2.You will need to create and keep your bitmap drawable in that way beforehand(use google for alpha blending PNGs) such that the borders have zero alpha value ( use softwares like Paint.NET for ex).
If images are being created dynamically, you would need to use a blending equation ( for details read materials by Porter and Duff).
For ex, this is PNG image with transparent pixels having alpha 0 other than the alphabet itself
.
Above is the alpha blended PNG on colored background of an Activity. I hope this is what you really want to achieve.

You can just set
android:background="#null"
in the XML

I had the same issue on my .aspx page - adding an that did an add to and remove from favorite functionality. I know the image was a .gif image format having a transparent background - but as mentioned here it showed in the browser in IE9 and Chrome, as a white squared background.
I added this and it solved the issue - but you must ensure the .gif or image indeed has a transparent background too:
<asp:ImageButton runat="server" ID="lnkFavorite" BackColor="Transparent" AlternateText="Add Favorite" CommandName="Favorite"
ImageUrl="Images/MakeFavorite_30.png" ToolTip="Click to add to the My Favorite Threads grid."
CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") + ";" + Eval("FavoriteUserID")%>' />
The BackColor="Transparent" attribute did it for me! I hope that helps too!
Russ

Answering here 5 years later for compatibility with Android L+.
In order to use a PNG (transparent) image as background on an ImageButton, you should use the property background instead of src
<ImageButton
android:id="#+id/imageProg"
android:layout_width="80dp"
android:layout_height="80dp"
android:onClick="doStuff"
android:background="#drawable/ic_stuff"

Related

Android Bitmap - Replace an object's color keeping Anti-Aliasing

I have an image in my assets folder on which I am drawing stuff using an external program and then using them in my app. The problem is that the bitmaps are blank (transparent) with black and white objects in them. Note that the objects are created with Anti-Aliasing on to look better. I know this was asked before but I couldn't find what I want. I need to replace all the black and white pixels in the image (even the transparent anti-aliased ones!) to the colors given by the user. Below are some images to show what I want to do:
Please note that this is just an example and I have even some very complicated shapes and the final colors aren't known (as inputed by the user in RGB style).
Any help is appreciated. Thanks! :)
Usually you can tint the images you load apliying them a color at runtime.
The problem is that color applies to the whole image and it only matchs exactly the same color in white pixels, with or without alpha.
So you could separate all the areas of the image with the same color, save them as white and then tint them at runtime while overlapping one over another.
It depends on the framework you are using.

9Patch image not strectching

I am using http://romannurik.github.io/AndroidAssetStudio/nine-patches.html to create a 9Patch image which is attached. . I use this is to set a drawableBottom to the TextView.But this does not stretch to the width the text view. What am i doing wrong ? This is the original image
Here are a few reasons for it to "not stretch"
Guides must only be one pixel wide, so if you want a 48×48 button, your png will actually be 50×50. Anything thicker than one pixel will remain part of your image.
guides must be solid black (#000000). Even a slight difference in color (#000001) or alpha will cause it to fail
MOST IMPORTANT: you should keep in mind that remaining area of the one-pixel outline must be completely transparent. This includes the four corners of the image – those should always be clear. This can be a bigger problem than you realize. For example, if you scale an image in Photoshop it will add anti-aliased pixels which may include almost-invisible pixels which will also cause it to fail*. If you must scale in Photoshop, use the Nearest Neighbor setting in the Resample Image pulldown menu (at the bottom of the Image Size pop-up menu) to keep sharp edges on your guides.
http://radleymarx.com/blog/simple-guide-to-9-patch/
You need to use android sdk draw9patch tool to make a 9-patch image.
you will need to define stretchable patches to image border like shown in below image border.

Is it possibile to create PNGs with some transparent areas?

I'm creating a nine-patch image for a widget background and I'd like to have only an area of the image partially transparent.. Is it possible to create such a PNG or to use a sort of transparency map in android? The setImageAlpha sets the transparency for the whole PNG..
Yes, without any problem. PNG can feature so called alpha channel which is other word is transparency. It's separate layer with the dimensions of the image it belongs to and each pixel of alpha channel defines transparency of corresponding pixel of image.
In PNG alpha is one byte value (from 0 (fully transparent) to 255 (opaque)), of course, is set for each pixel separately. To use transparency, your image should be RGB (true color, or in fact ARGB, where 'A' stands for alpha channel) PNG file and any sane graphics application should support it.
PNG also supports transparency with indexed (color mapped) images, but it works differently - in that case you choose single color in image color palette and if pixel is in that color, then it is fully transparent, otherwise it got its color from palete. Therefore you got less control on transparency as it is 0-1 type - either color is transparent or it is not. There's no gradation.
I recommend using any graphics program like Gimp or Photoshop and play with this.
Well, you can make the PNG's background transparent with any image editor.

White borders around image

How do i remove white borders around an image when used on different backgrounds?
http://groups.google.com/group/android-developers/browse_thread/thread/f4ef24e03431bbe4/ce7f19488fd0388a?lnk=gst&q=Crop+Image+View+that+fits+to+the+image#ce7f19488fd0388a
if your trying to use it as the main screen background then set its layout weight and height to "fill_parent" then the background can be any image you want it to be ref just like the SRC call you made. That would let the image fill the screen only and give you access to write on the screen with the SCR command. If you give us more of what your doing we can help more.
OK, I have seen the image and you want the ImageView to be of the same shape as your image because you don't want to see the white border around your image.
Well,in short, I don't think that is possible AFAIK. ImageView is a rectangular view which asks for height,width.
However, rather than try to do the impossible, I would suggest you to use alpha-masking( subset of alpha blending, google them to know more).
A little theory: depending on the alpha bits for each pixel in your Bitmap (the translucency bits), the extent of blending of that pixel with over-written pixel is determined. Considering extremes, if alpha is 255, overwriting pixel is used instead of the over-written one (fully opaque, in regular terms); if alpha is 0, overwriting pixel is just ignored (transparent). For other alphas in between: there is blending.
In your case you would have to make the alpha of the border zero, to achieve complete blend. Outlining the steps:.
The drawable to be used can't be a JPEG( JPEG doesn't store alpha values per pixel). Go for PNGs
2.You will need to create and keep your bitmap drawable in that way beforehand(use google for alpha blending PNGs) such that the borders have zero alpha value ( use softwares like Paint.NET for ex).
If images are being created dynamically, you would need to use a blending equation ( for details read materials by Porter and Duff).
For ex, this is PNG image with transparent pixels having alpha 0 other than the alphabet itself
.
Above is the alpha blended PNG on colored background of an Activity. I hope this is what you really want to achieve, and my answer is actually useful .

Android how to set button to have transparent background but outline still there

When I set android:background="#android:color/transparent, it sets my buttons background to transparent, but the outline of the button disappears. How do I set it so that the background is transparent but there is still an outline?
You can use a custom selector that uses different nine patch image for different states of the button.
This link might be helpful
You might use this image...
I hope it helps..
One approach would be to make yourself a 9-patch that has a border but transparency in the middle.
Here is an example:
it is an 11x11 pixel square with a 2 pixel black border. If you download this png and drop it into draw9patch and add the a pixel in the middle of the left and top then include the 9patch png file in your drawables and set
android:background="#drawable/bground"
either save your 9-patch as "bground" or change that in the above line to whatever you save it as.

Categories

Resources