2

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 );
        }
    }
}
4

1 回答 1

1

你用 a 绘制的所有东西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);
    }
}
于 2013-03-04T18:40:59.240 回答