gpt4 book ai didi

Android GroundoverLay 占用大量内存

转载 作者:行者123 更新时间:2023-11-30 00:03:33 25 4
gpt4 key购买 nike

我正在开发 Android Weather 应用程序,我的要求是显示动画预报。我们有一个服务器,我从那里使用 Glide 将预测图像加载为位图,然后使用位图创建 GoogleMap 的 GroundOveryayOption。我正在使用 Runnable 和 Handler 来制作动画。

除了内存消耗外一切正常,最终我得到了“内存不足异常”

为了顺利运行我的预测动画,我必须使用 Glide 加载所有位图并从位图创建 GroundOverlayOptions obj 并将 GroundOverlayOptions 存储在 HashMap 中,如下所示。

    @Override
public void onResourceReady(@NonNull Bitmap bitmap, @Nullable Transition<? super Bitmap> transition) {
mBitmapLoaded = true;

//the actual image is png and its size is in KBs but Bitmap size is in MBs
Log.i("Weather", ""+ bitmap.getByteCount());

GroundOverlayOptions overlayOptions = new GroundOverlayOptions()
.image(BitmapDescriptorFactory.fromBitmap(bitmap))
.positionFromBounds(getLatLngBounds(mBounds))
.visible(frameVisible);

//groundOverlaysItemMap is a Hashmap to store GroundOverlayOptions where key is Time
groundOverlaysItemMap.put(mTime, mMap.addGroundOverlay(overlayOptions));
}

感谢任何帮助。

最佳答案

将“大”图像拆分为“小”(例如 256x256 像素)图 block (例如 MapTiler,如 this 视频),将它们保存为 raw文件夹(或设备存储)并使用TileProvider就像在that Alex Vasilkov的答案:

public class CustomMapTileProvider implements TileProvider {
private static final int TILE_WIDTH = 256;
private static final int TILE_HEIGHT = 256;
private static final int BUFFER_SIZE = 16 * 1024;

private AssetManager mAssets;

public CustomMapTileProvider(AssetManager assets) {
mAssets = assets;
}

@Override
public Tile getTile(int x, int y, int zoom) {
byte[] image = readTileImage(x, y, zoom);
return image == null ? null : new Tile(TILE_WIDTH, TILE_HEIGHT, image);
}

private byte[] readTileImage(int x, int y, int zoom) {
InputStream in = null;
ByteArrayOutputStream buffer = null;

try {
in = mAssets.open(getTileFilename(x, y, zoom));
buffer = new ByteArrayOutputStream();

int nRead;
byte[] data = new byte[BUFFER_SIZE];

while ((nRead = in.read(data, 0, BUFFER_SIZE)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();

return buffer.toByteArray();
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (OutOfMemoryError e) {
e.printStackTrace();
return null;
} finally {
if (in != null) try { in.close(); } catch (Exception ignored) {}
if (buffer != null) try { buffer.close(); } catch (Exception ignored) {}
}
}

private String getTileFilename(int x, int y, int zoom) {
return "map/" + zoom + '/' + x + '/' + y + ".png";
}
}

关于Android GroundoverLay 占用大量内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49446472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com