Android的图形页面消耗太多的多边形后减慢? [英] Android Mapview slows after drawing too much polygon?

查看:164
本文介绍了Android的图形页面消耗太多的多边形后减慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经扩展将被加入到我的MapView叠加这个自定义类。我刚才这个类,增加了我所有的多边形和文字到这个覆盖类之一。然而,这导致非常慢的MapView。我加了一个平局整数和测试,这一类将吸取上述OnDraw函数被调用时的84倍。是否有任何解决方案,这将有助于减少的MapView的加载速度?眼下的MapView很慢,每次我动左右,甚至变焦时会很慢。看着android的目录下载,这在我看来,覆盖类的OnDraw被调用everysecond?我应该寻找另一个型层而是采用叠加的?

  @覆盖
公共无效画(油画画布,MapView类MapView类,布尔阴影)
{
    阴影= FALSE;
    INT numberofdraw = 0;
    //轮廓
    涂料粉刷=新的油漆();
    油漆=新的油漆(Paint.ANTI_ALIAS_FLAG);
    paint.setStrokeWidth(2);
    //paint.setColor(0x10000000);
    paint.setColor(Color.BLACK);
    paint.setStyle(Paint.Style.STROKE);
    paint.setAntiAlias​​(真);
    点point1_draw =新点();    的for(int i = 0; I< data.getCustomPolygonList()大小();我++)
    {        CustomPolygon customPolygon = data.getCustomPolygonList()得到(I)。
        路径path =新路径();
        path.setFillType(Path.FillType.EVEN_ODD);
        对于(INT N = 0; N< customPolygon.getCorrdinateList()大小(); N ++)
        {            GeoPoint的sector1 =新的GeoPoint((INT)(customPolygon.getCorrdinateList()获得(N).getLatitude()* 1e6个电子),(INT)((customPolygon.getCorrdinateList()获得(N).getLongitude())* 1e6个电子));
            如果(N == 0){
                MapView.getProjection()在toPixels(sector1,point1_draw)。
                path.moveTo(point1_draw.x,point1_draw.y);
            }其他
            {
                MapView.getProjection()在toPixels(sector1,point1_draw)。
                path.lineTo(point1_draw.x,point1_draw.y);
            }
        }        path.close();
        canvas.drawPath(路径,油漆);
        numberofdraw ++;
        // canvas.clipPath(路径,Op.DIFFERENCE);    }
    //部门内部的颜色
    的for(int i = 0; I< data.getCustomPolygonList()大小();我++)
    {
        CustomPolygon customPolygon = data.getCustomPolygonList()得到(I)。
        油漆=新的油漆();
        油漆=新的油漆(Paint.ANTI_ALIAS_FLAG);
        paint.setStrokeWidth(2);
        paint.setColor(0x186666ff);
        //paint.setColor(customPolygon.getColor());
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        paint.setAntiAlias​​(真);
        point1_draw =新点();
        路径path =新路径();
        path.setFillType(Path.FillType.EVEN_ODD);
        对于(INT N = 0; N< customPolygon.getCorrdinateList()大小(); N ++)
        {            GeoPoint的sector1 =新的GeoPoint((INT)(customPolygon.getCorrdinateList()获得(N).getLatitude()* 1e6个电子),(INT)((customPolygon.getCorrdinateList()获得(N).getLongitude())* 1e6个电子));
            如果(N == 0){
                MapView.getProjection()在toPixels(sector1,point1_draw)。
                path.moveTo(point1_draw.x,point1_draw.y);
            }其他
            {
                MapView.getProjection()在toPixels(sector1,point1_draw)。
                path.lineTo(point1_draw.x,point1_draw.y);
            }
        }        path.close();
        numberofdraw ++;
        canvas.drawPath(路径,油漆);    }    //行业里面的文字
    的for(int i = 0; I< data.getCustomPolygonList()大小();我++)
    {
        CustomPolygon customPolygon = data.getCustomPolygonList()得到(I)。
        TextPaint paintText =新TextPaint();
        点1点=新点();
        字符串文本= customPolygon.getName();        对于(INT N = 0; N< customPolygon.getCorrdinateList()大小(); N ++)
        {
            如果(customPolygon.getTextLocation()!= NULL)
            {
                paintText.setTextSize(24);
                RECT RECT =新的矩形();
                paintText.getTextBounds(文字,0,text.length(),RECT);
                paintText.setTextAlign(Paint.Align.CENTER);                paintText.setTypeface(Typeface.DEFAULT_BOLD);                paintText.setColor(Color.BLACK);                的GeoPoint sector1 =新的GeoPoint((中间体(getLatitude()* 1e6个电子)customPolygon.getTextLocation()),(INT)((customPolygon.getTextLocation()getLongitude())* 1e6个电子));
                MapView.getProjection()在toPixels(sector1,点1)。            }
        }        numberofdraw ++;
        canvas.drawText(文字,point1.x,point1.y,paintText);
    }    Log.e(Config.log_id,画没有。+ numberofdraw +);
}


解决方案

有很多方法可以改善它,我会建议你一个。

缓冲:
对于每个多边形创建一个新的画布,一个新的位图

 帆布myBufferCanvas;
位图myBufferBitmap = Bitmap.createBitmap(宽度,高度,Config.ARGB_8888);
myBufferCanvas =新的Canvas(myBufferBitmap);

然后只调用时,多边形的变化得出,调用战平 myBufferCanvas ,然后调用drawBitmap真正的画布上。

此方法的优点是性能,这将是非常快!缺点是记忆,如果你有很多的多边形,你可以杀器。只是尝试重用他们一些回家,你将被罚款。
只要记住,你可以申请任何转换到你的缓冲图像不重绘。

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+"");
}

解决方案

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.

这篇关于Android的图形页面消耗太多的多边形后减慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆