Francois
Francois

Reputation: 10968

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?

Upvotes: 4

Views: 10894

Answers (2)

Abhra
Abhra

Reputation: 92

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);

Upvotes: 5

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43088

You're making simple things difficult. Just use a layout with android:background attribute, and then add ImageViews dynamically with the necessary bitmaps inside.

Upvotes: 0

Related Questions