以@RadekJ here 的提高图块分辨率的解决方案(通过将 4 个较高缩放级别的图块组合为 1 个)作为参考,我开始研究相反的方法:采用较低的缩放级别,将其切掉分成 4 个部分,并使用它来构建更高的缩放级别。
假设您获得的磁贴的最大缩放级别是 8,但您希望能够通过放大这些磁贴来缩小到 9、10 等。
每次放大时,图块都除以 2。所以以缩放级别 9 为例,我从缩放级别 8 中取出图块,并将其切成 4 块。然后我确定了第 9 层所请求的图块需要哪个“象限”。
接下来,我使用递归来获得更高级别的缩放级别。我对结果很满意。
如果您想查看带有x、y 和zoom level 的图块,请将DRAW_DEBUG_DATA 设置为true。
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import com.google.android.gms.maps.model.Tile;
import com.google.android.gms.maps.model.TileProvider;
import java.io.ByteArrayOutputStream;
public class OverZoomTileProvider implements TileProvider {
public static final int MAX_ZOOM = 8;
private static final boolean DRAW_DEBUG_DATA = false;
private static final int TILE_SIZE = 256;
private static final int HALF_TILE_SIZE = TILE_SIZE / 2;
private Paint tilePainter = new Paint();
// these will only be used when DRAW_DEBUG_DATA is true
private Paint debugRectPaint;
private Paint debugTextPaint;
private TileProvider mTileProvider;
public OverZoomTileProvider(TileProvider tileProvider) {
mTileProvider = tileProvider;
if (DRAW_DEBUG_DATA) {
debugRectPaint = new Paint();
debugRectPaint.setColor(Color.RED);
debugRectPaint.setStrokeWidth(1);
debugRectPaint.setStyle(Paint.Style.STROKE);
debugTextPaint = new Paint();
debugTextPaint.setColor(Color.WHITE);
debugTextPaint.setStyle(Paint.Style.FILL);
debugTextPaint.setColor(Color.BLACK);
debugTextPaint.setTextSize(20);
}
}
@Override
public Tile getTile(int x, int y, int zoom) {
Bitmap image = Bitmap.createBitmap(TILE_SIZE, TILE_SIZE,
Bitmap.Config.ARGB_8888);
image.eraseColor(Color.TRANSPARENT);
Canvas canvas = new Canvas(image);
drawTile(canvas, zoom, x, y);
byte[] data = bitmapToByteArray(image);
image.recycle();
return new Tile(TILE_SIZE, TILE_SIZE, data);
}
private void drawTile(Canvas canvas, int zoom, int x, int y) {
Bitmap bitmap = getTileAsBitmap(x, y, zoom);
if (bitmap != null) {
canvas.drawBitmap(bitmap, 0, 0, tilePainter);
bitmap.recycle();
}
if (DRAW_DEBUG_DATA) {
canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), debugRectPaint);
canvas.drawText("" + x + ", " + x + " (" + zoom + ")", 128, 128, debugTextPaint);
}
}
private Bitmap getTileAsBitmap(int x, int y, int zoom) {
if (zoom <= MAX_ZOOM) {
Tile tile = mTileProvider.getTile(x, y, zoom);
if (tile == NO_TILE) {
return null;
}
return BitmapFactory.decodeByteArray(tile.data, 0, tile.data.length);
}
boolean leftColumn = x % 2 == 0;
boolean topRow = y % 2 == 0;
Bitmap bitmap = getTileAsBitmap(x / 2, y / 2, zoom - 1);
int quadrant;
if (leftColumn && topRow) {
quadrant = 1;
} else if (!leftColumn && topRow) {
quadrant = 2;
} else if (leftColumn) {
quadrant = 3;
} else {
quadrant = 4;
}
switch (quadrant) {
case 1:
bitmap = Bitmap.createBitmap(bitmap, 0, 0, HALF_TILE_SIZE, HALF_TILE_SIZE);
break;
case 2:
bitmap = Bitmap.createBitmap(bitmap, HALF_TILE_SIZE, 0, HALF_TILE_SIZE, HALF_TILE_SIZE);
break;
case 3:
bitmap = Bitmap.createBitmap(bitmap, 0, HALF_TILE_SIZE, HALF_TILE_SIZE, HALF_TILE_SIZE);
break;
case 4:
bitmap = Bitmap.createBitmap(bitmap, HALF_TILE_SIZE, HALF_TILE_SIZE, HALF_TILE_SIZE, HALF_TILE_SIZE);
break;
}
return Bitmap.createScaledBitmap(bitmap, TILE_SIZE, TILE_SIZE, false);
}
private static byte[] bitmapToByteArray(Bitmap bm) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, bos);
byte[] data = bos.toByteArray();
try {
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
}