我有一个谷歌地图 mapView,上面有一个自定义叠加层。此叠加获取用户正在查看的当前坐标,然后访问一个网站,在该网站上获取图像以叠加到地图上。这是一种不好的做法,因为 Web 请求可能需要几秒钟并完全锁定 UI 线程,因此我正在尝试解决该问题。
我正在尝试通过使用 AsyncTask 来修复它,该任务将抓取图像并在它准备好时将其绘制在地图上。我正在尝试将 Canvas 传递给 AsyncTask 以便在准备好时可以进行绘制,但不会进行绘制,并且我注意到绘制时画布大小为 0x0。
在我尝试将其放入 AsyncTask 之前,所有绘图代码都可以正常工作,只是它很慢。
这一切都在我的自定义叠加层中:
public class MapOverlaySevereWeather extends com.google.android.maps.Overlay
{
    private GeoPoint lastTopLeft = new GeoPoint(0, 0);
    private GeoPoint lastBotRight = new GeoPoint(0, 0);
    private Bitmap mapImage;
    private Canvas thisCanvas;
    private MapView mMapView;
    @Override
    public void draw(Canvas canvas, MapView mapView, boolean shadow)
    {
        super.draw(canvas, mapView, shadow);
        if( shadow || MapOverlayHandler.isInMotion() )
        { return; }
        mMapView = mapView;
        thisCanvas = canvas;
        Rect curShown = canvas.getClipBounds();
        GeoPoint topLeft = mapView.getProjection().fromPixels(0,0);
        GeoPoint bottomRight = mapView.getProjection().fromPixels(curShown.right, curShown.bottom);
        if( !topLeft.equals(lastTopLeft) || !bottomRight.equals(lastBotRight) )
        {
            int sizeX = mapView.getWidth();//curShown.right - curShown.left;
            int sizeY = mapView.getHeight();////curShown.bottom - curShown.top;
            float minLat = (float)bottomRight.getLatitudeE6() / 1E6f;
            float minLon = (float)topLeft.getLongitudeE6() / 1E6f;
            float maxLat = (float)topLeft.getLatitudeE6() / 1E6f;
            float maxLon = (float)bottomRight.getLongitudeE6() / 1E6f;
            String fileUrl = "url that gets image based off lat long size";
            new SevereWeatherAsync().execute(new AsyncOverlayData(fileUrl, canvas, curShown));
        }
        lastTopLeft = topLeft;
        lastBotRight = bottomRight;
        return;
    }
    private class SevereWeatherAsync extends AsyncTask<AsyncOverlayData, Void, AsyncOverlayData>
    {
        @Override
        protected void onPostExecute(AsyncOverlayData result)
        {
            super.onPostExecute(result);
            Log.w("Severe","Drawing on " + thisCanvas.getHeight() + " x " + thisCanvas.getWidth());
            Paint paint = new Paint();
            paint.setAlpha(100);        
            thisCanvas.drawBitmap(mapImage, null, result.getCurRect(), paint);
            mMapView.invalidate();
        }
        @Override
        protected AsyncOverlayData doInBackground(AsyncOverlayData... params)
        {
            Log.w("Severe","getting image");
            URL imageFileURL = null;       
            try
            {
                imageFileURL = new URL(params[0].getURL());
                HttpURLConnection conn = (HttpURLConnection) imageFileURL.openConnection();
                conn.setDoInput(true);
                conn.connect();
                InputStream is = conn.getInputStream();
                mapImage = BitmapFactory.decodeStream(is);
            }
            catch(Exception e)
            { return null; }        
            return params[0];
        }
    }
}