我希望能够获得一些对正在绘制的当前对象的引用
@Override
public void draw(Canvas canvas, MapView mapView,boolean shadow) {
//Log.i("DRAW","MARKER");
super.draw(canvas, mapView, false);
}
上面是我的绘图方法,我想扩展绘图方法以在每个项目下面写下标题。这需要 OverlayItem 中的 .getTitle() 方法。可能在此方法之外跟踪对象,但不确定将其放在哪里....
最佳答案
我做过类似的事情。
我在 MapView
中添加了一些标记,然后用一条线将它们连接起来。
我有一个扩展 Overlay
的类 LineOverlay
。
在构造函数中,它获取要通过线连接的项目列表。
类似于:
public LineOverlay(ItemizedOverlay<? extends OverlayItem> itemizedOverlay, int lineColor) {
mItemizedOverlay = itemizedOverlay;
colorRGB = lineColor;
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(colorRGB);
mPaint.setStrokeWidth(LINE_WIDTH);
}
然后在 onDraw()
上我这样做:
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
if ( mItemizedOverlay.size() == 0 || mItemizedOverlay.size() == 1 )
return;
Projection projection = mapView.getProjection();
int i = 0;
while( i < mItemizedOverlay.size() - 1 ){
OverlayItem begin = mItemizedOverlay.getItem(i);
OverlayItem end = mItemizedOverlay.getItem(i+1);
paintLineBetweenStations(begin,end,projection,canvas);
i++;
}
super.draw(canvas, mapView, shadow);
}
private void paintLineBetweenStations(OverlayItem from, OverlayItem to, Projection projection, Canvas canvas){
GeoPoint bPoint = from.getPoint();
GeoPoint ePoint = to.getPoint();
Point bPixel = projection.toPixels(bPoint, null);
Point ePixel = projection.toPixels(ePoint, null);
canvas.drawLine(bPixel.x, bPixel.y, ePixel.x, ePixel.y, mPaint);
}
在您的情况下,您可以做一些类似的事情,创建一个 SubtitleOverlay
,它扩展 Overlay
,它接收构造函数中的所有项目,然后在 draw
上方法在正确的位置创建字幕。
关于android - 获取当前正在绘制的 OverlayItem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3200897/