do you know how to make a certain part of a picture (jpg) semitransparent, like in the attached image?
Just use a PNG image instead. Problem solved :)
JPEG doesn't support transparency. Can you use a 32-bit PNG instead?
If you have an existing image that you want to apply some transparency pattern to, you will need to create a Bitmap object in code, draw your image, then set some flags to allow you to draw the transparency levels over it, a bit like a Photoshop layer mask. I did a search for Android alpha masking, and found this blog post.
Err - I may be utterly confused here, but why not just use setAlpha(int alpha) on the inflated ImageView?
That seems to work fine on a .jpg for me.
Or is it only part of the image that should be transparent? Hard to see the 'sample image'.
Related
I'm new to Android Graphics and would appreciate if you can advise an algorithm to color particular part of image with outlines. I would like to create similar to this application.
I should be able to fill in parts of the image with colors, or use freehand tool to paint, but it should not go outside the outlines.
Should I use any image recognition tools to track black pixels for example?
I can also use predefined images only. I know I can convert each part of it into vector format. Will it be helpful?
Any tutorial references or example code would be highly appreciated.
You could look for connected-component-labeling. It basically assigns a label to each pixel where the label depends on the color of the neighbour. A description and implementation (though not in Java) can be found here: http://www.codeproject.com/Articles/336915/Connected-Component-Labeling-Algorithm. An implementation in java might be found here (https://github.com/klonikar/connected-components-labeling). I didn't check it though.
When you selected the pixels that you need, you can color them as described here: how to change the color of certain pixels in bitmap android
I have a custom view where normally I was using .png files to draw onto in a canvas and that worked fine but now my requirements are to use .gif files and for some reason the same code will not work. Is there something specific about .gif files that doesnt allow android canvas objects to draw on bitmaps made from them?
Apparently you cannot draw on a canvas created from a .gif file. Once I was able to convert the .gif to .png I was able to draw on the canvas again.
Actually, I can draw GIF files on canvas, but all transparent colours turn into white! I assume it is just a bug, related to the new parameter inPremultiplied in Bitfactory.Options which probably was not tested properly.
I am really frustrated with that, since now I have to replace GIFs by PNG in all my previous applications. (Fixing the bug in the following version doesn't make any difference, since there will still be models, not supporting GIF transparency.)
Indeed GIF file has it own advantage. It doesn't support alpha or true colour, but it has transparent index which in many cases is what actually needed. If you want PNG files to be not crucially bigger in size than GIF (I assume this is the main reason for using GIF instead of PNG), convert PNG files from true color to index (colormap) format (most of graphic editor, including GIMP and Photoshop support that), which works OK with KitKat. You will still lose size, but no mare than 1%.
I have an image that is circular and I want to make the other junk in the image to be transparent. I've spent the last hour looking around and I can't seem to figure out how this is done for android. Any help is greatly appreciated!
I haven't worked with Android but on iOS pngs weren't loading for me when I made them in photoshop but when I made them in flash all of a sudden they would work! Try that first :)
Anyways, you want to google something along the line of "pixel arrays android code" you can create an array of each pixel, find an individual color, then make it transparent...
But I really think this is just a GIMP issue and you don't need to go through all of that hassle.
what i can understand from your uncleared words is that you want to display transparent image of a normal image you have . so i would suggest don't do it through programming . create a transparent image through any image editor tool and use it .
if image is dynamic that you cant go in this way so use Matrix and setAlpha . search over net for both the terms
(Sorry for my bad english)
I have two pictures. One picture is colorful and big. On the other picture shows a black figure arbitrary shape (like a cross or a star or a piece of the classic puzzle) on a transparent background.
I want to put the second picture on the first picture and replace the black with part of the first image. Then i need to save the second image in a new file.
Thank you.
Max.
You can get this by using a library and a single line code.
Please try this, hope you will get a better solution.
dependencies {
compile 'com.mafstech.libs:mafs-image-shape:1.0.4'
}
And this line is your main code
Shaper.shape(context, R.drawable.your_original_image_which_will_be_displayed, R.drawable.shaped_image__your_original_image_will_get_this_images_shape, imageView, height, weight);
You need to use xfer modes, unless you are preaperd to create Paths and then clip canvas.
Check my answer here: how to draw pic to Closed curve area
Follow the links for examples...
Android has a nice way of defining stretchable images called a nine-patch. See these docs for a description of the concept. The idea is to surround a png image with a 1-pixel border where you can define the stretchable areas and the padding dimensions of the image. This is absolutely brilliant and I'd like to use the idea in my iPhone app. Before writing my own nine-patch to UIImage loader I thought I'd see if one already exists. Google doesn't return any results so I don't have much hope, but it doesn't hurt to ask, right? :-)
EDIT: Folks, I appreciate the answers but I know about stretchableImageWithLeftCapWidth.... I'm looking for code that takes a path #"foo.9.png" and returns a stretchable UIImage. This code will undoubtedly use stretchableImageWithLeftCapWidth... internally. I'm sure I could write the code myself using that method. But I'm asking if somebody else has already done it.
I received an e-mail from Tortuga22 software who informed me that they have created such a library and released it under the Apache license:
Announcement: http://blog.tortuga22.com/2010/05/31/announcing-tortuga-22-ninepatch/
Source code: http://github.com/tortuga22/Tortuga22-NinePatch
Example usage:
// loads-and-caches ninepatch and rendered image of requested size
UIImage buttonImg = [TUNinePatchCache imageOfSize:buttonSize
forNinePatchNamed:#"buttonNormalBackground"];
[self.buttonNeedingBackground setImage:buttonImg
forControlState:UIControlStateNormal];
Also look at UIView's contentStretch property. It is more robust and well-behaved than stretchableImageWithLeftCapWidth. Basically, it works by just defining the stretchable rectangle within your image and automatically creating a scaled nine-patch. This internal rectangle can be anything - it doesn't even have to be in the center of the image. Plus unlike stretchableImage this method will properly shrink graphics and behave as expected for graphics with lighting or gloss. I can't think of any real-world application where you would want more than this.
Yes UIImage does support something like it. See
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight and the documentation for leftCapWidth and topCapHeight
basically the image is not stretched in the area leftCapWidth pixels from the left and right edge and topCapHeight pixels from the top and the bottom. When the image is scaled the area inside of these limits is subject to stretching.
All UIImage images support this natively. By default the entire images is stretchable, but you can set caps with the leftCapWidth and topCapHeight properties or you can generate one from an existing UIImage with the - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight method.
Do note that in apple's implementation, when you set one or both of these values, the stretchable area is forced to be a single pixel high/wide.