以下代码应该加速我的MapView,因为我正在处理大量的路由。不幸的是,“其他”部分没有画任何东西。有人可以帮助我理解这一点。 THX
public boolean draw(Canvas canvas, MapView mapv, boolean shadow, long when) {
if ((bmap == null) || (lastLatSpan != 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)
// create a new bitmap, the size of the map view
Canvas offscreencanvas = new Canvas();
bmap = Bitmap.createBitmap(mapv.getWidth(), mapv.getHeight(),
Bitmap.Config.ARGB_8888);
offscreencanvas.setBitmap(bmap);
Path path = null;
Projection projection = mapv.getProjection();
// lastLatSpan = mapv.getLatitudeSpan();
for (int j = 0; j < mTourenArray.length; j++) {
GeoPoint[] route = GeoPointFactory
.createGeoPointArray(mTourenArray[j].getGeoPointArray());
defaultColor = mTourenArray[j].getColorByCat();
path = new Path();
Paint polyPaint = new Paint();
polyPaint.setColor(defaultColor);
for (int i = 0; i < route.length - 1; i++) {
GeoPoint gp1 = route[i];
GeoPoint gp2 = route[i + 1];
if (shadow == false) {
polyPaint.setAntiAlias(true);
Point point = new Point();
projection.toPixels(gp1, point);
// mode=1:start
if (i == 0) {
RectF oval = new RectF(point.x - mRadius, point.y
- mRadius, point.x + mRadius, point.y
+ mRadius);
// start point
canvas.drawOval(oval, polyPaint);
Point point2 = new Point();
projection.toPixels(gp2, point2);
path.moveTo(point.x, point.y);
path.lineTo(point2.x, point2.y);
}
/* mode=3:end */
else if (i == route.length - 2) {
/* the last path */
Point point2 = new Point();
projection.toPixels(gp2, point2);
path.lineTo(point2.x, point2.y);
RectF oval = new RectF(point2.x - mRadius, point2.y
- mRadius, point2.x + mRadius, point2.y
+ mRadius);
/* end point */
polyPaint.setAlpha(255);
offscreencanvas.drawOval(oval, polyPaint);
}
// mode=2:path
else if (i < route.length - 2 & i != 0) {
Point point2 = new Point();
projection.toPixels(gp2, point2);
path.lineTo(point2.x, point2.y);
}
}
}
// create an off-screen canvas to prepare new bitmap, and
// draw path on to it
polyPaint.setStrokeWidth(5);
polyPaint.setStyle(Paint.Style.STROKE);
polyPaint.setAntiAlias(true);
polyPaint
.setAlpha(defaultColor == Color.parseColor("#6C8715") ? 220
: 120);
offscreencanvas.drawPath(path, polyPaint);
} // Outer For End
// 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 = projection.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);
Paint tmpPaint = new Paint();
// create a new bitmap, the size of the map view
// draw the bitmap in the new correct location
canvas.drawBitmap(bmap, offsetPt.x, offsetPt.y, tmpPaint);
}
return super.draw(canvas, mapv, shadow, when);
}
答案 0 :(得分:0)
这看似显而易见,但我不确定您的方法的上下文是什么(或者您的某些成员字段持有什么)但是您可能会检查一些内容。 mapStartPosition是否等于当前位置? 我不熟悉super.draw(Canvas,MapView,boolean,long)调用的方法签名,但根据我的经验,通常不需要/有助于直接调用super.draw方法。 Doc说MapView继承了视图中的绘制,它只将Canvas作为一个arg,所以我不确定你在那里做什么。通常我只是在画布或其他可绘制对象上调用draw。
我知道这不是一个“答案”,但也许会有所帮助。祝你好运!
答案 1 :(得分:0)
thx etteyafed for your answer
问题是
if (shadow == false)
这里的工作代码:
if (shadow == false) {
if ((bmap == null) || (lastLatSpan != mapv.getLatitudeSpan()) || true) {
// 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)
// create a new bitmap, the size of the map view
Canvas offscreencanvas = new Canvas();
bmap = Bitmap.createBitmap(mapv.getWidth(), mapv.getHeight(),
Bitmap.Config.ARGB_8888);
bmap.copy(Config.ARGB_4444, true);
offscreencanvas.setBitmap(bmap);
mCanvas = offscreencanvas;
Path path = null;
Projection projection = mapv.getProjection();
lastLatSpan = mapv.getLatitudeSpan();
for (int j = 0; j < mTourenArray.length; j++) {
GeoPoint[] route = GeoPointFactory
.createGeoPointArray(mTourenArray[j]
.getGeoPointArray());
defaultColor = mTourenArray[j].getColorByCat();
path = new Path();
Paint polyPaint = new Paint();
polyPaint.setColor(defaultColor);
for (int i = 0; i < route.length - 1; i++) {
GeoPoint gp1 = route[i];
GeoPoint gp2 = route[i + 1];
polyPaint.setAntiAlias(true);
Point point = new Point();
projection.toPixels(gp1, point);
// mode=1:start
if (i == 0) {
Point point2 = new Point();
projection.toPixels(gp2, point2);
RectF oval = new RectF(point.x - mRadius, point.y
- mRadius, point.x + mRadius, point.y
+ mRadius);
// start point
offscreencanvas.drawOval(oval, polyPaint);
path.moveTo(point.x, point.y);
path.lineTo(point2.x, point2.y);
}
/* mode=3:end */
else if (i == route.length - 2) {
/* the last path */
Point point2 = new Point();
projection.toPixels(gp2, point2);
path.lineTo(point2.x, point2.y);
RectF oval = new RectF(point2.x - mRadius, point2.y
- mRadius, point2.x + mRadius, point2.y
+ mRadius);
/* end point */
polyPaint.setAlpha(255);
offscreencanvas.drawOval(oval, polyPaint);
}
// mode=2:path
else if (i < route.length - 2 & i != 0) {
Point point2 = new Point();
projection.toPixels(gp2, point2);
path.lineTo(point2.x, point2.y);
}
}
// create an off-screen canvas to prepare new bitmap, and
// draw path on to it
polyPaint.setStrokeWidth(5);
polyPaint.setStyle(Paint.Style.STROKE);
polyPaint.setAntiAlias(true);
polyPaint.setAlpha(defaultColor == Color
.parseColor("#6C8715") ? 220 : 120);
offscreencanvas.drawPath(path, polyPaint);
} // Outer For End
// draw the bitmap of the path onto my map view's canvas
// And set these pixels to destination bitmap:
canvas.drawBitmap(bmap, 0, 0, null);
panAfterZoom(mapv, 600);
// 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 = projection.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);
// start point
// create a new bitmap, the size of the map view
// draw the bitmap in the new correct location
canvas.drawBitmap(bmap, offsetPt.x, offsetPt.y, null);
}
}
return super.draw(canvas, mapv, shadow, when);
}