我在AsyncTask
doInBackground
中的Canvas上绘制了一个。绘图代码贯穿,通过逐步调试检查。也调用了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 );
}
}
}
答案 0 :(得分:1)
您使用Canvas
绘制的所有内容实际上都是在Bitmap
上设置的Canvas
上绘制的。您使用的Bitmap
仅在doInBackground()
中引用,而在其他任何地方均未引用。您的叠加层需要做的是绘制位图:
@Override
protected Bitmap doInBackground(final DataVO... params) {
return bitmap;
}
@Override
protected void onPostExecute(final Canvas canvas) {
// ...
final DataOverlay dataOverlay = new DataOverlay(bitmap);
// ...
}
private class DataOverlay extends Overlay {
private final Bitmap mBitmap;
DataOverlay(Bitmap b) { mBitmap = b; }
@Override
public void draw(final Canvas canvas, final MapView mapView, final boolean shadow) {
canvas.drawBitmap(b, 0.0f, 0.0f, null);
}
}