Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine multiple drawables

I have multiple drawables and want to combine it to one drawable (for example, 4 squares to create one big square, like Windows logo :)). How can I do this?

like image 685
artem Avatar asked Jul 04 '12 23:07

artem


1 Answers

You can do that using a TableLayout or some LinearLayouts. However, if what you want is to create a single image to usin within a ImageView you will have to create a Bitmap manually; it is not hard:

Bitmap square1 = BitmapFactory.decodeResource(getResources(), R.drawable.square1);
Bitmap square2 = BitmapFactory.decodeResource(getResources(), R.drawable.square2);
Bitmap square3 = BitmapFactory.decodeResource(getResources(), R.drawable.square3);
Bitmap square4 = BitmapFactory.decodeResource(getResources(), R.drawable.square4);

Bitmap big = Bitmap.createBitmap(square1.getWidth() * 2, square1.getHeight() * 2, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(big);
canvas.drawBitmap(square1, 0, 0, null);
canvas.drawBitmap(square2, square1.getWidth(), 0, null);
canvas.drawBitmap(square3, 0, square1.getHeight(), null);
canvas.drawBitmap(square4, square1.getWidth(), square1.getHeight(), null);

I have not even compile the code above; I'm just showing you how it can be done. I'm also assuming you have square drawables all with the same dimensions. Notice that the bitmap called big can be used wherever you want (e.g. ImageView.setImageBitmap()).

like image 60
Cristian Avatar answered Oct 12 '22 07:10

Cristian