Android - drawing path as overlay on MapView

*爱你&永不变心* 提交于 2019-11-27 03:36:31

问题


I have a class that extends Overlay and implemments Overlay.Snappable. I have overriden its draw method:

@Override
public void draw(Canvas canvas, MapView mv, boolean shadow)
{
    Projection projection = mv.getProjection();
    ArrayList<GeoPoint> geoPoints = new ArrayList<GeoPoint>();
    //Creating geopoints - ommited for readability
    Path p = new Path();
    for (int i = 0; i < geoPoints.size(); i++) {
    if (i == geoPoints.size() - 1) {
        break;
    }
    Point from = new Point();
    Point to = new Point();
    projection.toPixels(geoPoints.get(i), from);
    projection.toPixels(geoPoints.get(i + 1), to);
    p.moveTo(from.x, from.y);
    p.lineTo(to.x, to.y);
    }
    Paint mPaint = new Paint();
    mPaint.setStyle(Style.FILL);
    mPaint.setColor(0xFFFF0000);
    mPaint.setAntiAlias(true);
    canvas.drawPath(p, mPaint);
    super.draw(canvas, mv, shadow);
}

As you can see, I make a list of points on a map and I want them to form a polygonal shape.

Now, the problem is that when I set paint style to be FILL or FILL_AND_STROKE nothing shows up on the screen, but when I set it to be just stroke, and set stroke width, it acctually draws what it is supposed to draw.

Now, I looked for solution, but nothing comes up. Can you tell me if I something missed to set in the code itself, or are there some sorts of constraints when drawing on Overlay canvases?

Thanks


回答1:


A few things. You should use p.moveTo(from.x, from.y); only once i.e., first time when you want to do it for the first time.

Try this to set attributes for the paint object used for painting the polygon.

polygonPaint = new Paint();
polygonPaint.setStrokeWidth(2); 
polygonPaint.setStyle(Paint.Style.STROKE);
polygonPaint.setAntiAlias(true); 

Hope this helps.



来源:https://stackoverflow.com/questions/3036139/android-drawing-path-as-overlay-on-mapview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!