Skip to content Skip to sidebar Skip to footer

How To Add 3 Images In A Canvas In Android

I have 3 images that I want to add one after other on a canvas. This is my code:- public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); set

Solution 1:

I had to solve something similar not too long ago, and you're nearly there with your solution. However, you should be using Rect objects to offset where you draw your bitmap each time. Assuming you've copied all your images into an array of Bitmaps images[], and you've created your bitmap and canvas as you did above, use the following:

Rect srcRect;
Rect dstRect;

for (int i = 0; i < images.length; i++){
    srcRect = new Rect(0, 0, images[i].getWidth(), images[i].getHeight());
    dstRect = new Rect(srcRect);
    if (i != 0){
        dstRect.offset(images[i-1].getWidht(), 0)
    }
    canvas.drawBitmap(images[i], srcRect, dstRect, null);
}

This will copy them all into one line. It's not too difficult to adapt this to copy 4 images into a square, or something similar, using two for loops.

Post a Comment for "How To Add 3 Images In A Canvas In Android"