Android: draw a number of bitmaps or drawables into a single drawable

In some cases, you might need to render a numer of bitmaps or drawables into a single drawable. For instance, swapping LayerDrawables of different sizes displayed on top of MapView, might render not as it is supposed to. Code snippet below shows how to render LayerDrawable into a single BitmapDrawable. It is a simplified piece of code and contains “magic numbers”, but anyway, this should show the idea:


public Drawable getPersonMapItemDrawable(Drawable drawable1, Drawable drawable2){

  Drawable[] layers = new Drawable[2];
  layers[0] = drawable1;
  layers[1] = drawable2;

  int resourceBitmapHeight = 136, resourceBitmapWidth = 153;

  float widthInInches = 0.9f;

  int widthInPixels = (int)(widthInInches * context.getResources().getDisplayMetrics().densityDpi);
  int heightInPixels = (int)(widthInPixels * resourceBitmapHeight / resourceBitmapWidth);

  int insetLeft = 10, insetTop = 10, insetRight = 10, insetBottom = 10;

  LayerDrawable layerDrawable = new LayerDrawable(layers);
  layerDrawable.setLayerInset(1, insetLeft, insetTop, insetRight, insetBottom);		
  
  Bitmap bitmap = Bitmap.createBitmap(widthInPixels, heightInPixels, Bitmap.Config.ARGB_8888);

  Canvas canvas = new Canvas(bitmap);
  layerDrawable.setBounds(0, 0, widthInPixels, heightInPixels);
  layerDrawable.draw(canvas);
		
  BitmapDrawable bitmapDrawable = new BitmapDrawable(context.getResources(), bitmap);
  bitmapDrawable.setBounds(0, 0, widthInPixels, heightInPixels);
		
  return bitmapDrawable;
}

,

3 Comments

Augmented Reality

A program created as an assignment project for human-computer interaction at the University.

A webcam captures the view, finds the marker, then the object is drawn at the proper position.
Written in C#, VC++ and C++, using WPF, OpenGl, DIBs and some math.

Not downloadable as the proper marker is needed to play. A movie embeded below created as a part of the task.

, , , , , , , ,

Leave a comment

Network checkers

A game created as an assignment for Computer Networks. Multithreaded server written in C and running under Linux, client programmed in Java using for instance Java3d, Swing…

Download client
Download server (for linux)

, , , , , , , , , ,

Leave a comment

Checkers

A game of checkers created for some programming subject. Written in pure C and OpenGL.

Download

, , , , , , , ,

Leave a comment

Spacegunner – FPP shooter

A game created with Konrad Szałkowski smijran[at]o2.pl as an assignment for Computer Graphics at the Poznan University of Technology.

FPP shooter, programmed in C++ using OpenGL, winapi and Ms XAct for 3d sound effects (unfortunatelly these may not work on machines without DirectX SDK installed… for reason unknown to me). Shadow mapping technique used for generating a shadow of the tank.

Download

, , , , , , , , , , ,

Leave a comment

PUTtanks – multiplayer RTS game

A simple network multiplayer RTS game, written in Java, mainly making use of Java3d, Swing and Sockets.

Two instances of program can run with server hosted at localhost, if you want to try it and cannot find anyone to play with 😉

Downloads:
The game
Java 3D – choose suitable for your platform, if you haven’t installed it yet.

, , , , , , , ,

Leave a comment