I have an ImageView that uses a png of a coin. the png is square and everything outside of the round coin is showing as white in stead of clear. How would I accomplish this?
the corner are not transparent in you png file.
You need to make them transparent beforehand using your favorite image editor, or you can use an online service Here
Related
I've made this 9-patch png and added it to the res/drawable folder:
It works nice in the example eclipse shows:
But on the emulator it looks messed up:
Why is this happening and how can I fix this?
EDIT: The border on which I've placed the black dots is white, not transparent. Not sure if that's got anything to do with it...
This probably happens because you have a small mistake with the black pixels you have set.
Try to re-cut the edges of the picture and to add the black pixels all over again.
I had this problem too once and this helped me.
if the border is white then this is your problem it has to be pure black or nothing at all.
UPDATE: Try this image:
It seems you are not saved your image with .9.png
In order to make it work do like this
save your image in the following format in your drawable folder
yourimage.9.png (not png format of your image)
Check this reference draw 9 patch
The Android SDK contains a 9-patch editing tool (draw9patch.bat) that can help you to produce and/or validate 9-patch images. Without it, it is very easy to make a mistake in defining your border.
The one-pixel border should be transparent except for the black pixels that define the various parts of the image to be treated differently during scaling.
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"
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.
Is there a way to change color of a ninepatch image(png) programly on android ? I know how do this with bitmap but not with ninepatch...
Thanks to helping me.
Regards
Jim
NinePatch images are standard PNG files, with a 1-pixel border around the outside to mark the repeated areas. You can edit them in any image editor, and as long as you leave the 1-pixel outer border alone, they'll continue to work just fine.
See here for how they are constructed.
after I found out (sadly) that there is no easy way to create a simple border around a View, I was forced to use the 9-patch-image approach to get a border around my View.
So I read the short thing there is on the 9-patch and its easy enough to understand. I started the "Draw 9-patch" application that came with the Android SDK, opened up a very simple image I created in Paint and then... I got stuck.
According to the page (http://developer.android.com/guide/developing/tools/draw9patch.html) I should be able to define those areas where the image can be expanded and so on, just by clicking on the edges ("Click within the 1-pixel perimeter to draw the lines that define the stretchable patches...").
So I try to do just that and absolutely nothing happens. I try to hold down SHIFT when clicking and also do the rigth-mouse-click, and nothing happens. When I move the mouse away from the 1-pixel perimiter I get a grey-and-red "marquee" (stripes).
Bottom line is: nothing happens no matter what I do.
The tool definitely works, but make sure the image you create first does NOT have the .9.png extension. If it does, the draw9patch tool will assume the 1 pixel perimeter of your image is already setup as 9-patch. If you open an image with just a .png extension, the tool will add the 1 pixel perimeter and you will be able to manipulate it as described, and save it as 9-patch when you're done.
The 9-patch tool is a bit awkward to use, but it does work. I believe you need to left click and drag around the edge to paint the outside border.
Alternatively, you can use any image editor and create a one pixel empty border around it, and draw in the following:
Top/Left = black pixels for the area of the image that can be grown or shrunk as needed. For a typical border, it is everything except for the curved corners
Bottom/Right = black pixels for the small snippet that should be repeated if the image needs to be grown in that direction.
You must use a non-8bit png file to create a .9.png file, or it can not be edit.
In my case, I can convert a 8-bit png to 9-patch file with Android Studio successfully, but I can't edit it anyway.
When I export the origin psd to normal png file, and convert it to 9-patch file, the 9-patch borders ban be edit now!