multiple images in a one single image - android

I want to create a bitmap / image which has many images like "Collage" which has more then one images in a single picture.
I have stored all my images in a grid view but now i want to create a single image from all those images. And even i want to make few images click able
so what can be the road map to do this ? any sort of help / example will be helpful.
reference image

Bitmap pic1 = BitmapFactory.decodeResource(getResources(), R.drawable.pic1);
Bitmap pic2 = BitmapFactory.decodeResource(getResources(), R.drawable.pic2);
Bitmap bg= BitmapFactory.decodeResource(getResources(), R.drawable.background);
Bitmap out1 = Bitmap.createBitmap(bg) ;
Canvas comboImage = new Canvas(out1);
comboImage.drawBitmap(pic1, 10f, 20f, null);
comboImage.drawBitmap(pic2, 30f, 40f, null);
out1 will have pic1 & pic2, with a background image bg.

To create a single image from multiple images look at using Canvas. You can put bitmaps (and drawables) on a canvas. You can commit the changes and then push then to a single bitmap that you can then use. As far as making certain sections clickable after making it one single image, I will leave this up to someone else to explain, I am not worked directly with the ontouch() functions.

Related

Maps: merging multiple different-size drawables

I have a problem. I need to merge two different sized pictures (drawables). The idea is to have a picture of someone (loaded dynamically) that is 100x100px and have a transparent background that is bigger (e.g. 100x120). In those last 20 pixels I have an arrow that is supposed to point to a person's location on a map. Then I think I could do something like this:
Drawable[] layers = new Drawable[2];
layers[0] = res.getDrawable(R.drawable.background_img);
layers[1] = res.getDrawable(R.drawable.icon);
LayerDrawable layerDrawable = new LayerDrawable(layers);
But this simply overlays one image onto another ignoring their bounds.
Thanks in advance,
Vaidas
-- UPDATE: Finally solved the problem. Works like a charm :)
private Drawable createPersonDrawable(Bitmap personImage)
{
Bitmap resultingBitmap = Bitmap.createBitmap(drawableWidth,
drawableHeight, Bitmap.Config.ARGB_8888);
Canvas comboCanvas = new Canvas(resultingBitmap);
comboCanvas.drawBitmap(personImage, 0, 0, null);
// Get the bottom part of the image from resources
Bitmap bottomPart = BitmapFactory.decodeResource(getResources(),
R.drawable.person_map_icon_bottom);
comboCanvas.drawBitmap(bottomPart, 0, drawablePersonImageHeight, null);
comboCanvas.save();
return new BitmapDrawable(resultingBitmap);
}
I found the description here: http://www.jondev.net/articles/Combining_2_Images_in_Android_using_Canvas
I don't have the exact commands here but you should do:
Create a Bitmap with the total size you want.
Create a Canvas passing the created Bitmap
Draw the two images on the Canvas.
Add the Bitmap to the view you are using.
if you want to change Drawable size and position at LayerDrawable, you can use setLayerSize to change drawable size ,and you can use setLayerInset to control position.

Best way to animate programatically created bitmaps in android

I have a board game app that creates a lot (hundreds) of bitmaps, and constantly changes them as the game progresses. These bitmaps are created, not loaded from a resource... so they have no R.id to refer to.
I would like to animate some of them, e.g. a bitmap moving from one loction to another when a player taps to move it. What is the best way to do this?
Note, this is 2.1 and the bitmaps are drawn on a canvas via a matrix translate.
Make this function to get image from drawable folder
private Bitmap getImageBitmap(){
int resID = getResources().getIdentifier(uri, "drawable",
"a.b.c");
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), resID);
return mBitmap;
}
For set the image , where do you you want>
ImageView imageview01.setImageBitmap(getImageBitmap("name of bitmap");
if you want more about bitmap. then you may try this links.
http://developer.android.com/reference/android/widget/ImageView.html
drawable getResources().getIdentifier problem
setImageURI / setimagebitmap to fetch image from net and display in image view

Android: building a composite image

Im porting an app from Flex to Android and wondering about how to build a composite image and display it.
Specifically I have a map (PNG or JPG) of a house and Im placing different markers in various locations. I've implemented this in HTML using DIV and in Flex using a canvas. Each marker has an X,Y pair based on the original size of the image. Ideally I'd like to display the image, place the markers and then support resize, drag (of the image, not the markers), etc.
There is info about 'multi-touch' available here though its a bit dated.
Suggestions on where to start?
You have to watch out for non-mutable bitmaps. When you load your bitmap, you have to create a copy which will be mutable. Then just apply your overlay using a Canvas.
Bitmap tempBitmap = BitmapFactory.decodeResource(getResources(), R.id.background, options);
Bitmap overlay = BitmapFactory.decodeResource(getResources(), R.id.overlay, options);
Bitmap finalBitmap = Bitmap.createBitmap(tempBitmap.getWidth(), tempBitmap.getHeight(), tempBitmap.getConfig());
Canvas canvas = new Canvas(finalBitmap);
canvas.drawBitmap(tempBitmap, new Matrix(), null);
canvas.drawBitmap(badge, new Matrix(), null);
// finalBitmap will contain your background and its overlay
-I_Artist

Android: how to simply draw a bitmap on another bitmap

I'm new to Android dev and I'm having a hard time trying to do something which seems obvious to me: drawing little images on top of a bigger image.
Let's say that I have a 500x500 image and I want to draw icons at different locations. Icons are png files that I load with:
Bitmap img =
BitmapFactory.decodeResource(getResources(),
R.drawable.idIcon1)
My "background image" is a LayerDrawable.
Then, I am totally lost... Do I have to create a canvas ? How to draw on my "background image" my icons at different positions?
int positionLeft=0;
int positionTop=0;
Bitmap newBitmap =Bitmap.createBitmap(backgroundBitmap.getWidth(),bitmap.getHeight(),Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newBitmap);
canvas.drawBitmap(backgroundBitmap, positionLeft, positionTop,null);
positionLeft=100;
positionTop=100;
canvas.drawBitmap(iconBitmap,positionLeft,positionTop,null);
imageView.setImageBitmap(newBitmap);
You're making simple things difficult. Just use a layout with android:background attribute, and then add ImageViews dynamically with the necessary bitmaps inside.

refresh a bitmap or canvas in android

can anybody tell me how to refresh or
reset a bitmap or canvas to draw
another image on it?
actually i have a map binary file which contains bitmap tiles in bytes now problem is i have a buffer image with buffer image i get screen image to display. so when i move my screen image i need to refresh my buffer image and draw new tiles to display on screen.
here is code to display image.
bitmap = Bitmap.createBitmap(screenWidth * (int)mapState.getiBufferMult(), screenHeight * (int)mapState.getiBufferMult(), Config.RGB_565);
canvas = new Canvas(bitmap);
image = new ImageView(this);
this is not full source but i hope you guys can have idea.
thanks a lot
If you generate the bitmap that you want, you can say image.setImageBitmap(yourBitmap);
The Bitmap class also offers methods to modify an existing bitmap or replace just part of it.
If you are using the same bitmap and modifying it, call invalidate () on the ImageView when you have the bitmap ready.

Categories

Resources