Search code examples
androidheatmaptile

Dynamically creating a tile for a TileProvider


I want to create a heat map on Android and am trying to generate a tile to return with getTile, but can't find anything about dynamically generating a large image from a smaller one copied into it a bunch of times. Are there any tutorials or code snippets for this?

Also, if this isn't the way to go about this let me know as well. Since I'm dynamically generating the tile I can't use the urlprovider, I just can't find a single example of someone generating tiles dynamically.


Solution

  • If you want to create a bitmap from another bitmap by cropping, resizing, etc, you're gonna want to use a Canvas:

    Canvas canvas = new Canvas(resultBitmap); //Result Bitmap will be what you end up drawing.
    canvas.drawBitmap(otherBitmap, areaFromOtherBitmapToCopyRect, areaInResultBitmapToDrawRect, paint);
    

    the 2nd and 3rd parameters there are rects inside the the source bitmap (from which you're copying a part or the whole image), and the result bitmap (to which you're drawing the image).

    However, if you're drawing a heat-map, you might find it easier to just draw small rectangles of colors instead of copying other bitmaps (which is computationally harder). You create the Canvas in the same way, but instead of calling drawBitmap, call drawRect:

    Paint redPaint = new Paint();
    redPaint.setColor(0xFFFF0000); //This will be red. The 1st FF is for alpha.
    canvas.drawRect(someAreaInTheResultRect, redPaint);
    

    All that's left is to play with the colors according to the value of the area in the heat map, play with the positions (by generating rects properly, and you're done :)

    Hope this helps!