I've had to do something similar. My attempt currently does the following in onDraw (simplified for readability - error-handling etc. stripped out):
if ((bmap == null) || (lastZoom != mapv.getLatitudeSpan()))
{
// bitmap is null - so we haven't previously drawn the path, OR
// the map has been zoomed in/out, so we're gonna re-draw it anyway
// (alternatively, I could have tried scaling the bitmap... might
// be worth investigating if that is more efficient)
Projection proj = mapv.getProjection();
// store zoom level for comparing in the next onDraw
lastZoom = mapv.getLatitudeSpan();
// draw a path of all of the points in my route
GeoPoint start = routePoints.get(0);
Point startPt = new Point();
proj.toPixels(start, startPt);
Path path = new Path();
path.moveTo(startPt.x, startPt.y);
Point nxtPt;
for (GeoPoint nextPoint : routePoints)
{
nxtPt = new Point();
proj.toPixels(nextPoint, nxtPt);
path.lineTo(nxtPt.x, nxtPt.y);
}
// create a new bitmap, the size of the map view
bmap = Bitmap.createBitmap(mapv.getWidth(), mapv.getHeight(), Bitmap.Config.ARGB_8888);
// create an off-screen canvas to prepare new bitmap, and draw path on to it
Canvas offscreencanvas = new Canvas(bmap);
offscreencanvas.drawPath(path, mPaint);
// draw the bitmap of the path onto my map view's canvas
canvas.drawBitmap(bmap, 0, 0, null);
// make a note of where we put the bitmap, so we know how much we
// we need to move it by if the user pans the map
mapStartPosition = proj.fromPixels(0, 0);
}
else
{
// as we're in onDraw, we think the user has panned/moved the map
// if we're in here, the zoom level hasn't changed, and
// we've already got a bitmap with a drawing of the route path
Projection proj = mapv.getProjection();
// where has the mapview been panned to?
Point offsetPt = new Point();
proj.toPixels(mapStartPosition, offsetPt);
// draw the bitmap in the new correct location
canvas.drawBitmap(bmap, offsetPt.x, offsetPt.y, null);
}
It's not perfect yet.... for example, the path ends up in the wrong place immediately after zooming - being moved to the correct place once the user starts panning.
But it's a start - and hugely more efficient than redrawing the path on every onDraw call
Hope this helps!