我有这个自定义类,它扩展了我的mapview中添加的Overlay。我只有一个这个类,它将我的所有多边形和文本添加到此覆盖类中。然而,这导致了非常慢的mapview。我添加了一个绘制整数,并测试了每次调用ondraw函数时该类将绘制84次。有没有解决方案有助于降低mapview的加载速度?现在mapview非常慢,每次我向左移动甚至缩放都会很慢。看看android catlog,在我看来,每隔一秒调用覆盖类ondraw?我应该看另一种类型的图层而不是使用叠加层吗?
@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+"");
}
答案 0 :(得分:3)
有很多方法可以改进它,我会建议你一个。
<强>缓冲:强> 对于每个多边形,创建一个新画布和一个新位图
Canvas myBufferCanvas;
Bitmap myBufferBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
myBufferCanvas = new Canvas(myBufferBitmap);
然后只在更改多边形时调用draw,使用myBufferCanvas
调用draw,然后在真实画布上调用drawBitmap。
这种方法的优点是性能,它会非常快!缺点是内存,如果你有很多多边形,你可以杀死设备。试着把它们重新用在家里,你会没事的。 请记住,您可以对缓冲的图像应用任何变换,而无需重新绘制它。
答案 1 :(得分:1)
我认为您应该使用自定义ItemizedOverlay来管理MapView上的绘图项。除此之外,您可以对现有代码进行一些更改,我相信这些更改会加快速度。在draw()方法中,每次都要创建新的Paint对象,设置它们的属性然后处理它们。更糟糕的是,在draw()方法中,您遍历多边形列表两次,创建更多Paint对象或TextPaint对象。相反,您可以在初始化Overlay时创建一个Paint对象,然后在循环中重复使用它们。您也可以在一个循环中完成所有事情。
答案 2 :(得分:0)
处理数据,然后构建路径并最终在地图上绘制路径,请参阅此post中TWiStErRob的响应。