AsyncTask
我在in 中的 Canvas 上绘制 a doInBackground
。绘图代码运行通过,一步步调试检查。该onPostExecute()
方法也被调用,也被检查过。然而,什么都没有被绘制?我可以确认绘图代码是正确的,因为之前在 Overlay 子类的 onDraw() 方法中运行了相同的逻辑。这是来源:
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 );
}
}
}