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.
Related
I'm having trouble with imageView.setColorFilter(). In my app, I have dark mode (black background, white text) and light mode (white background, black text).
Targeting API 21+, I am using vector asset icons generated by Android Studio which is white, where by default android:tint="#FFFFFF" and android:fillColor="#FF000000" in the xml files.
As per material design, the icons have certain alpha applied for different status. I am using status of Active + Unfocused, where for black icon = #8A000000 and white icon = #B3FFFFFF.
In dark mode, the icon will appear greyish over the black background, which is exactly how it should be with alpha applied. However, in light mode, the icon is 100% black regardless of the alpha above. I am applying the color using imageView.setColorFilter() with default PorterDuff.Mode.SRC_ATOP.
What is happening here and how can I solve it? Thank you.
While writing this question, I have experimented with a few things and came to solved the problem myself.
The issue is that the icon itself is BLACK (#FF000000) as per vector xml . Android Studio tinted it with white only.
Using PorterDuff.Mode.SRC_ATOP, the blending of source and destination pixels (which are both black) hence does not result in visible change. In order to fully replace the original icon color, I used PorterDuff.Mode.SRC_IN instead. In this case, I can use the same vector xml in both modes.
As per official docs
PorterDuff.Mode.SRC_ATOP - Keeps the destination pixels that are not covered by source pixels. Discards destination pixels that are covered by source pixels. Discards all source pixels.
PorterDuff.Mode.SRC_IN - Keeps the source pixels that cover the destination pixels, discards the remaining source and destination pixels.
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.
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
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"
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 .