要实现不出现抖动的更新自定义图块,可以使用以下代码示例:
private static final String TAG = "MyTileOverlay";
private TileOverlay mTileOverlay;
private int mMinZoomLevel = 1;
private int mMaxZoomLevel = 18;
private List
// implement the custom TileProvider TileProvider tileProvider = new UrlTileProvider(256, 256) { @Override public synchronized URL getTileUrl(int x, int y, int zoom) { if (!mZoomLevels.contains(zoom) || zoom < mMinZoomLevel || zoom > mMaxZoomLevel) { return null; } String key = String.format(Locale.US, "http://mydomain.com/tile/%d/%d/%d.png", zoom, x, y); URL url = null; try { url = new URL(key); } catch (MalformedURLException e) { e.printStackTrace(); } return url; } };
// implement the custom TileOverlay mTileOverlay = mMap.addTileOverlay(new TileOverlayOptions().tileProvider(tileProvider).transparency(0));
// update the TileOverlay with new Bitmaps private synchronized void updateMapCache(String key, byte[] bytes) { mMapCache.put(key, bytes); // perform an update of individual tiles at zoom levels where there is content available for (int zoom : mZoomLevels) { Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); Tile tile = mTileOverlay.getTileProvider().getTile(zoom, x, y); // x and y values will change depending on which tile is being updated if (tile != null) { Bitmap oldBitmap = tile.getBitmap(); if (oldBitmap != null && !oldBitmap.isRecycled()) { mBitmapCache.evict(key); mBitmapCache.put(key, bitmap); } tile.setBitmap(bitmap); mTileOverlay