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?
The transparent button can be easily created by using HTML and CSS. In this article, we will use background-color: transparent; property to design the transparent background button. HTML Code: In this section, we will create a basic structure of button using the button Tag.
You can specify a transparent background for the image button. The result will be a clickable image that acts like a button but can look like whatever you want it to look like. Just set android:background="@null" for the image button.
Use a android:scaleType="fitCenter" to have Android scale the images, and android:adjustViewBounds="true" to have them adjust their bounds due to scaling. All of these attributes can be set in code on each ImageButton at runtime. However, it is much easier to set and preview in xml in my opinion.
To remove the standard button background image, define your own background image or set the background color to be transparent. Save the XML file in your project res/drawable/ folder and then reference it as a drawable for the source of your ImageButton (in the android:src attribute).
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:.
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With