开发者

MapOverlay draw() method optimization

Here is my draw method. I have 755 roads and about 10 coordinates on each road. So I need two for loop to draw paths. It is running too slow. Any help on optimizing this code. Maybe I don't have to create some of objects.

    Projection projection = mv.getProjection();
    roadList = getRoadList();
    int length = roadList.length;

    for (int i = 0; i < length; i++) {
        Coordinate[] coordinateList = roadList[i].getCoordinateList();
        int numberOfCoordinates = coordinateList.length;
        Path path = new Path();

        for (int j = 0; j开发者_Go百科 < numberOfCoordinates - 1; j++) {
            Coordinate coordinateFrom = coordinateList[j];
            Coordinate coordinateTo = coordinateList[j + 1];
            GeoPoint geoPointFrom = coordinateFrom.getGeoPoint();
            GeoPoint geoPointTo = coordinateTo.getGeoPoint();
            Point pointFrom = new Point();
            Point pointTo = new Point();
            projection.toPixels(geoPointFrom, pointFrom);
            projection.toPixels(geoPointTo, pointTo);
            path.moveTo(pointFrom.x, pointFrom.y);
            path.lineTo(pointTo.x, pointTo.y);

            if (!canvas.quickReject(path, EdgeType.BW)) {
                if (j == numberOfCoordinates - 2) {
                    canvas.drawPath(path, paint);
                }
            }               
        }           
    }

In get methods there is not any calculations. They are just getting some predefined variables.


I would suggest creating the Paths outside the onDraw() method rather than each time it's called (which could be hundreds of times). The data doesn't seem to change between calls. Create them as you get the data and keep them in a Collection of some sort. Then draw them in onDraw().

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜