我在畫布上畫一個AsyncTask
的doInBackground
。繪圖代碼貫穿始終,通過逐步調試進行檢查。也調用onPostExecute()
方法,也檢查了該方法。但是,什麼都沒有被吸引?我可以確認繪圖代碼是否正確,因爲先前在Overlay的子類的onDraw()方法內運行了相同的邏輯。這裏的源:到Bitmap
你對Canvas
設置你用Canvas
畫繪製畫布到疊加
import java.lang.ref.WeakReference;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.os.AsyncTask;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;
public class AsyncOverlayTask extends AsyncTask<DataVO, Void, Canvas> {
private final Paint mPathPaint;
private final Paint mPointPaint;
private Path path;
private final MapView mMapView;
private final Canvas mCanvas;
public AsyncOverlayTask(final MapView mapView, final Paint pathPaint, final Paint pointPaint) {
mMapView = new WeakReference<MapView>(mapView).get();
mPathPaint = pathPaint;
mPointPaint = pointPaint;
mCanvas = new Canvas();
}
@Override
protected Canvas doInBackground(final DataVO... params) {
Thread.currentThread().setName("AsyncOverlayTask");
final Canvas canvas = new Canvas();
final Bitmap bitmap = Bitmap.createBitmap(mMapView.getWidth(), mMapView.getHeight(), Bitmap.Config.ARGB_8888);
canvas.setBitmap(bitmap);
canvas.drawColor(Color.TRANSPARENT);
GeoPoint previousGeoPoint = null;
GeoPoint geoPoint = null;
final Path path = new Path();
Projection projection;
// at least 2 elements in aData
if ((params != null) && (params.length > 0)) {
for (final DataVO dataVo : params) {
if (dataVo.getLatitude().replace(".", "").length() > 8) {
geoPoint = new GeoPoint(Integer.valueOf(dataVo.getLatitude().replace(".", "").substring(0, 8)), Integer.valueOf(dataVo
.getLongitude().replace(".", "").substring(0, 7)));
} else {
geoPoint = new GeoPoint(Integer.valueOf(dataVo.getLatitude().replace(".", "")), Integer.valueOf(dataVo.getLongitude()
.replace(".", "")));
}
// project point
projection = mMapView.getProjection();
Point projectedPoint = new Point();
projectedPoint = projection.toPixels(geoPoint, projectedPoint);
// draw point
canvas.drawCircle(projectedPoint.x, projectedPoint.y, 7, mPointPaint);
if (previousGeoPoint != null) {
final Point prevPoint = new Point();
projection.toPixels(previousGeoPoint, prevPoint);
// if((projectedPoint.x != prevPoint.x) &&
// (projectedPoint.y != projectedPoint.y)) {
path.moveTo(projectedPoint.x, projectedPoint.y);
path.lineTo(prevPoint.x, prevPoint.y);
canvas.drawPath(path, mPathPaint);
// }
} else {
path.moveTo(projectedPoint.x, projectedPoint.y);
}
previousGeoPoint = geoPoint;
}
}
return canvas;
}
@Override
protected void onPostExecute(final Canvas canvas) {
if ((mMapView != null) && (canvas != null)) {
final DataOverlay dataOverlay = new DataOverlay();
dataOverlay.draw(canvas, mMapView, false);
if (mMapView != null) {
mMapView.getOverlays().add((dataOverlay));
mMapView.invalidate();
}
}
}
private class DataOverlay extends Overlay {
@Override
public void draw(final Canvas canvas, final MapView mapView, final boolean shadow) {
super.draw(canvas, mapView, shadow);
}
}
}
我有一個線索。 mMapview.getWidth()和getHeight()都返回-1 ... – 2013-03-04 15:52:11