Android GroundoverLay占用大量内存 [英] Android GroundoverLay consumes lot of memory

查看:57
本文介绍了Android GroundoverLay占用大量内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发Android Weather应用程序,我的要求是显示动画天气预报.我们有一台服务器,在该服务器上,我使用Glide将预测图像加载为位图,然后使用该位图创建GoogleMap的GroundOveryayOption.我正在使用Runnable和Handler进行动画制作.

I am working on Android Weather application where my requirement is to show animated forecast. We have a server from where I load forecast images as Bitmap using Glide and then create GoogleMap's GroundOveryayOption using the bitmap. I am using Runnable and Handler for animation.

除了内存消耗之外,其他所有东西都工作正常,最终我收到了内存不足异常"

Everything is working fine except the memory consumption and eventually I got "Out of memory exception"

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

In-order to smoothly run my forecast animation I have to load all the Bitmaps using Glide and create GroundOverlayOptions obj from Bitmap and store GroundOverlayOptions in HashMap as below.

    @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 像素)图块(例如,使用视频),将其保存到 raw 文件夹(或设备存储)中,然后使用 Alex Vasilkov 的答案:

Split your "large" image into "small" (e.g. 256x256 pixels) tiles (for example with MapTiler like in this video), save them into raw folder (or device storage) and use TileProvider like in that answer of 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占用大量内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆