ArtoAle
ArtoAle

Reputation: 2977

How to add multiple layer on an image in android

I whould like to have an image (of a map) in the application and to programmatically add some layers on top of it (placeholders, paths, etc...) I think that a photoshop-like layer approach could be helpful, but I have no idea of where to start. Any simple example/link to tutorial or to documentation is useful :)

Thanks

Upvotes: 1

Views: 2635

Answers (1)

Luis
Luis

Reputation: 12048

I'm giving you a simple approach that you can build on:

  • Create a empty Bitmap finalBitmap. This will be the final destination for all layers composition.
  • Create a Canvas to draw to the finalBitmap. This canvas will be used to draw all layers into final bitmap.
  • Create a Bitmap with your map image. Draw it to finalBitmap using canvas. This would be layer 1.
  • Place markers, routes, etc. using the same approach. Those would be layers 2, 3, etc.

Exemple code:

//The empty Bitmap
finalBitmap = Bitmap.createBitmap(width, height , Bitmap.Config.ARGB_8888);
canvas = new Canvas(finalBitmap );
imageView.setImageBitmap(finalBitmap );


//Create the map image bitmap
Config config = Config.RGB_565;
Options options = new Options();
options.inPreferredConfig = config;
InputStream in = null;
Bitmap bitmap = null;
try {
        in = new FileInputStream(fMapImage);
        bitmap = BitmapFactory.decodeStream(in);
        if (bitmap == null)
            throw new RuntimeException("Couldn't load bitmap from asset :" + fMapImage.getAbsolutePath());
    } catch (IOException e) {
        throw new RuntimeException("Couldn't load bitmap from asset :" + fMapImage.getAbsolutePath());
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            }
        }
}


//Draw the map image bitmap
Rect dst = new Rect(pt00.x, pt00.y, ptMM.x, ptMM.y);
canvas.drawBitmap(bitmap, null, dst, null);

//Here draw whatever else you want (markers, routes, etc.)

Regards

Upvotes: 2

Related Questions