0

我正在使用一些 .png 文件将叠加层添加到地图中。它工作正常,但有时会抛出位图大小超过 VM 预算错误。当将图像添加到列表视图时发生同样的问题时,我通过应用 SoftReference 解决了它。但我不知道如何应用它在地图视图中添加叠加层。请有任何想法。谢谢

像这样添加叠加层

public ItemizedOverlayMarker(Drawable defaultMarker, Context context) {
    super(boundCenterBottom(defaultMarker));
    mContext = context;
}

@Override
protected OverlayItem createItem(int i) {
    return mOverlays.get(i);
}

@Override
public int size() {
    return mOverlays.size();
}
public void addOverlay(OverlayItem overlay) {
    mOverlays.add(overlay);
    populate();
}

像这样调用 ItamizerOverlay :

final List<Overlay> mapOverlaysResult = mapView.getOverlays();
Drawable drawableResult = getResources().getDrawable(R.drawable.pin_pink);
final ItemizedOverlayMarker itemizedoverlayResult = new ItemizedOverlayMarker(drawableResult, this);

for (int i = 0; i < Constants.listOfPlaces.size(); i++) {

    GeoPoint geoPoint = new GeoPoint((int)(Double.parseDouble(latitude) * 1E6), (int)(Double.parseDouble(longitude) * 1E6));
    OverlayItem overlayitem = new OverlayItem(geoPoint, name, vicinity);
    itemizedoverlayResult.addOverlay(overlayitem);
                }
4

1 回答 1

-1

首先将图像转换为文件对象

并传递下面的函数它将返回位图..

private Bitmap decodeFile(File f)
    {
        try
        {
            //decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);

            //Find the correct scale value. It should be the power of 2.
            final int REQUIRED_SIZE=70;
            int width_tmp=o.outWidth, height_tmp=o.outHeight;
            int scale=1;
            while(true)
            {
                if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                    break;
                width_tmp/=2;
                height_tmp/=2;
                scale*=2;
            }

            //decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } 
        catch (FileNotFoundException e) {}
        return null;
    }
于 2012-12-10T10:10:10.820 回答