gpt4 book ai didi

java - 谷歌地图从异步任务 onPostExecute 在 Canvas 上叠加绘图

转载 作者:行者123 更新时间:2023-11-30 04:12:27 24 4
gpt4 key购买 nike

我有一个谷歌地图 mapView,上面有一个自定义叠加层。该叠加层获取用户正在查看的当前坐标,然后转到一个网站,在该网站上获取要叠加到 map 上的图像。这是一种不好的做法,因为 Web 请求可能需要几秒钟的时间并完全锁定 UI 线程,所以我正在尝试解决该问题。

我正在尝试通过一个 AsyncTask 来修复它,它将抓取图像并在准备就绪时将其绘制在 map 上。我正在尝试将 Canvas 传递给 AsyncTask,以便在准备就绪时可以进行绘制,但不会进行绘制,并且我注意到在绘制时 Canvas 大小为 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];
}
}

}

最佳答案

我怀疑发生的事情是您传递给 AsyncTask 的 Canvas 在下载图像时无效。

我认为更好的解决方案是将 result.getImage() 返回的位图保存到一个实例变量中,然后重绘叠加层本身。

所以你会得到类似..

public class MyOverlay extends Overlay {
private Bitmap mBitmap;
private MapView mMapView;

public void draw(Canvas canvas, MapView mapView, boolean shadow) {
// .. your drawing code
if(mBitmap == null) {
// Better download your image!
new SevereWeatherAsync().execute(new AsyncOverlayData(fileUrl, void, curShown));
} else {
//Draw the bitmap!
canvas.drawBitmap(mBitmap, ...);
}
}

private class SevereWeatherAsync extends AsyncTask<> {
@Override
protected AsyncOverlayData doInBackground(AsyncOverlayData... params) {
//You background work..
}

@Override
protected void onPostExecute(AsyncOverlayData result) {
mBitmap = result.getImage();

//Now we can redraw the overlay!
mMapView.invalidate();
}
}

}

关于java - 谷歌地图从异步任务 onPostExecute 在 Canvas 上叠加绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10628877/

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