I have two questions actually:
- Is it better to draw an image on a bitmap or create a bitmap as resource and then draw it over a bitmap? Performance wise... which one is better?
- If I want to draw something transparent over a bitmap, how would I go about doing it?
- If I want to overlay one transparent bitmap over another, how would I do it?
Sorry for the long list, but in the interest of learning, I would like to explore both the approaches...
You can do something like this:
The idea is very simple: Once you associate a bitmap with a canvas, you can call any of the canvas' methods to draw over the bitmap.
This will work for bitmaps that have transparency. A bitmap will have transparency, if it has an alpha channel. Look at Bitmap.Config. You'd probably want to use ARGB_8888.
Important: Look at this Android sample for the different ways you can perform drawing. It will help you a lot.
Performance wise (memory-wise, to be exact), Bitmaps are the best objects to use, since they simply wrap a native bitmap. An ImageView is a subclass of View, and a BitmapDrawable holds a Bitmap inside, but it holds many other things as well. But this is an over-simplification. You can suggest a performance-specific scenario for a precise answer.
I think this example will definitely help you overlay a transparent image on top of another image. This is made possible by drawing both the images on canvas and returning a bitmap image.
Read more or download demo here
and call the above function on button click and pass the two images to our function as shown below
For more than two images, you can follow this link, how to merge multiple images programmatically on android
I cant believe no-one has answered this yet! A rare occurrence on SO!
1
Question doesn't quite make sense to me. But ill give it a stab. If you're asking about direct drawing to a canvas (polygons, shading, text etc...) VS loading a bitmap and blitting it onto the canvas that would depend on the complexity of your drawing. As the drawing gets more complex the CPU time required will increase accordingly. However, blitting a bitmap onto a canvas will always be a constant time which is proportional to the size of the bitmap.
2
Without knowing what "something" is how can i show you how to do it? You should be able to figure out #2 from the answer for #3.
3
Assumptions:
you want them both overlaid from the top left corner.
If the purpose is to obtain a bitmap, this is very simple:
In the end, image will contain the overlap of image and image2.