如果要求只是调暗或更改地图的整体颜色,那么您可以使用图块叠加。这将在地图上创建部分透明的图层(在本例中为绿色)。它位于图块的正上方,因此标记和其他对象不受影响。
@Override
public void onMapReady(GoogleMap googleMap) {
TileProvider coordTileProvider = new CoordTileProvider(getActivity());
map.addTileOverlay(new TileOverlayOptions().tileProvider(coordTileProvider));
}
课程CoordTileProvider
是:
public static class CoordTileProvider implements TileProvider {
private static final int TILE_SIZE_DP = 256;
private final float mScaleFactor;
private final Bitmap mBorderTile;
public CoordTileProvider(Context context) {
/* Scale factor based on density, with a 0.2 multiplier to increase tile generation
* speed */
mScaleFactor = context.getResources().getDisplayMetrics().density * 0.2f;
Paint paint = new Paint();
paint.setColor(Color.argb(150,200,255,200));
mBorderTile = Bitmap.createBitmap((int) (TILE_SIZE_DP * mScaleFactor),
(int) (TILE_SIZE_DP * mScaleFactor), android.graphics.Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(mBorderTile);
canvas.drawRect(0, 0, TILE_SIZE_DP * mScaleFactor, TILE_SIZE_DP * mScaleFactor,
paint);
}
@Override
public Tile getTile(int x, int y, int zoom) {
Bitmap coordTile = drawTileCoords(x, y, zoom);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
coordTile.compress(Bitmap.CompressFormat.PNG, 0, stream);
byte[] bitmapData = stream.toByteArray();
return new Tile((int) (TILE_SIZE_DP * mScaleFactor),
(int) (TILE_SIZE_DP * mScaleFactor), bitmapData);
}
private Bitmap drawTileCoords(int x, int y, int zoom) {
Bitmap copy = null;
synchronized (mBorderTile) {
copy = mBorderTile.copy(android.graphics.Bitmap.Config.ARGB_8888, true);
}
return copy;
}
}