Question

I am implementing a map viewer based on openstreetmap tiles. Each tile has the same size of 256x256 pixels.

When a tile is not available to draw, I look at its "parent" size (one from lower zoom) and cut the region corresponding to the tile I need.

Here's the preudocode:

class TileDrawer:

  Canvas canvas;

  class TileSource:
      Canvas canvas;
      Bitmap placeHolder;

      canvas.setBitmap(placeHolder);

      Bitmap getTile(x, y, z):
         if x,y,z in cache:
             return cache[x,y,z];
         else if parent in cache:
             canvas.drawBitmap(parent, tileRect, parentRect);
             return placeHolder;
         else
             return null;

   drawTiles(tileSource):
         for tileXYZ in tilesXYZ:
             tile = tileSource.getTile(tileXYZ)
             canvas.drawBitmap(tile)

The problem is after drawing the screen is tiled with the very same drawing. So I suspect that the code canvas.drawTile doesn' draw immediately, and hence the tile displayed is the one that called getTile last. As an experiment, I copied the bitmap before returning in the getTile, and everything worked as needed.

Can anyone confirm this behavior? How can I avoid this without having to keep a bunch of tile placeholders and without copying bitmaps all the time?

Clarification

I guess the behaviour is equivalent to modifying a member bitmap in the loop and then calling canvas.drawBitmap after each modification. The problem is that only the final modification is drawn, and not the ones in between.

Was it helpful?

Solution

Refer to answers here

Android, canvas: How do I clear (delete contents of) a canvas (= bitmaps), living in a surfaceView?

I called this after drawing each bitmap:

Canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR)

The problem as I said was that the canvas in the class TileSource used the latest tile to be drawn instead of drawing the requested tiles each time. The line above forces all the changes of the canvas be applied immediately (seems so)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top