Android Mapview slows after drawing too much polygon?

那年仲夏 提交于 2020-01-14 03:19:06

问题


I've this custom class that extends the Overlay that is being added into my mapview. i have just one of this class that adds all my polygon and text into this overlay class. However this results in a very slow mapview. I added a draw integer and tested out that this class will draw 84 times each time the ondraw function is being called. Is there any solution that will help to reduce the loading speed of the mapview? Right now the mapview is very slow, each time i move left right or even zoom will be very slow. looking at the android catlog, it seems to me that overlay class ondraw is being called everysecond? should i be looking at another type of layer instead of using overlay?

@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) 
{
    shadow=false;
    int numberofdraw= 0;




    //outline 
    Paint paint = new Paint();
    paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setStrokeWidth(2);
    //paint.setColor(0x10000000);    
    paint.setColor(Color.BLACK); 
    paint.setStyle(Paint.Style.STROKE);
    paint.setAntiAlias(true);
    Point point1_draw = new Point();     

    for (int i =0;i<data.getCustomPolygonList().size();i++)
    {

        CustomPolygon customPolygon= data.getCustomPolygonList().get(i);
        Path path = new Path();
        path.setFillType(Path.FillType.EVEN_ODD);
        for(int n=0;n<customPolygon.getCorrdinateList().size();n++)
        {

            GeoPoint sector1 = new GeoPoint((int)(customPolygon.getCorrdinateList().get(n).getLatitude()*1e6), (int)((customPolygon.getCorrdinateList().get(n).getLongitude())*1e6));
            if(n==0){
                mapView.getProjection().toPixels(sector1, point1_draw);
                path.moveTo(point1_draw.x,point1_draw.y);
            }else
            {
                mapView.getProjection().toPixels(sector1, point1_draw);
                path.lineTo(point1_draw.x,point1_draw.y);
            }
        }

        path.close();
        canvas.drawPath(path, paint);
        numberofdraw++;
        //  canvas.clipPath(path, Op.DIFFERENCE);

    }


    //inside sector color
    for (int i =0;i<data.getCustomPolygonList().size();i++)
    {
        CustomPolygon customPolygon= data.getCustomPolygonList().get(i);
        paint = new Paint();
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStrokeWidth(2);
        paint.setColor(0x186666ff);    
        //paint.setColor(customPolygon.getColor()); 
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setAntiAlias(true);
        point1_draw = new Point();     
        Path path = new Path();
        path.setFillType(Path.FillType.EVEN_ODD);
        for(int n=0;n<customPolygon.getCorrdinateList().size();n++)
        {

            GeoPoint sector1 = new GeoPoint((int)(customPolygon.getCorrdinateList().get(n).getLatitude()*1e6), (int)((customPolygon.getCorrdinateList().get(n).getLongitude())*1e6));
            if(n==0){
                mapView.getProjection().toPixels(sector1, point1_draw);
                path.moveTo(point1_draw.x,point1_draw.y);
            }else
            {
                mapView.getProjection().toPixels(sector1, point1_draw);
                path.lineTo(point1_draw.x,point1_draw.y);
            }
        }

        path.close();
        numberofdraw++;
        canvas.drawPath(path, paint);

    }



    //inside sector text
    for (int i =0;i<data.getCustomPolygonList().size();i++)
    {
        CustomPolygon customPolygon= data.getCustomPolygonList().get(i);
        TextPaint paintText = new TextPaint();
        Point   point1 = new Point();     
        String text=customPolygon.getName();

        for(int n=0;n<customPolygon.getCorrdinateList().size();n++)
        {
            if(customPolygon.getTextLocation()!=null)
            {
                paintText.setTextSize(24);
                Rect rect = new Rect();
                paintText.getTextBounds(text, 0, text.length(), rect);
                paintText.setTextAlign(Paint.Align.CENTER);

                paintText.setTypeface(Typeface.DEFAULT_BOLD); 

                paintText.setColor(Color.BLACK);

                GeoPoint sector1 = new GeoPoint((int)(customPolygon.getTextLocation().getLatitude()*1e6), (int)((customPolygon.getTextLocation().getLongitude())*1e6));


                mapView.getProjection().toPixels(sector1, point1);

            }
        }

        numberofdraw++;
        canvas.drawText(text, point1.x, point1.y, paintText);
    }



    Log.e(Config.log_id,"draw no. "+    numberofdraw+"");
}

回答1:


there are many ways you can improve it, I will suggest you one.

Buffering: For each polygon create a new canvas and a new bitmap

Canvas myBufferCanvas;
Bitmap myBufferBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
myBufferCanvas = new Canvas(myBufferBitmap);

Then only call draw when the polygon is change, call the draw with myBufferCanvas and then call drawBitmap on the true canvas.

The advantage of this method is performance, it will be extremely fast! The disadvantage is memory, if you have to many polygons, you can kill the device. Just try reusing them some home and you will be fine. Just remember that you can apply any transformation to your buffered image without redrawing it.




回答2:


I think you should be using a custom ItemizedOverlay to manage drawing items on your MapView. That aside, there are some changes you can make to your existing code which I'm sure would speed it up. In your draw() method you are creating new Paint objects each time, setting their properties and then disposing of them. Worse still, inside the draw() method you loop through the polygon list twice, creating more Paint objects or TextPaint objects. Instead, you can create one of each Paint object once when the Overlay is initialised and then re-use them in the loop. You may also be able to do everything in one loop.




回答3:


Process the data, then build the paths and finally paint the paths on the map, see the response from TWiStErRob in this post.



来源:https://stackoverflow.com/questions/11912427/android-mapview-slows-after-drawing-too-much-polygon

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