gpt4 book ai didi

Android 谷歌地图绘制道路性能问题 10.000+ 覆盖

转载 作者:行者123 更新时间:2023-11-29 18:12:54 27 4
gpt4 key购买 nike

我正在从事一个项目,该项目正在启动绘制道路并显示有关道路的一些信息。问题是我使用了太多的地理点(5.000-10.000 +)并画线点到点并用不同的颜色显示道路,所以 map 太慢了。我做了一些关于我的配置应用程序,但仍然太慢。您对解决我的问题并提高性能有什么想法吗?

这是我的代码。

for (int t = 0; t < roads.size(); t++) {

for (int i = 0; i < roads.get(t).size() - 1; i++) {
//bounds up-bottom-right-left to draw roads
if (boundBox[0] >= roads.get(t).get(i)
.getLatitudeE6()
&& boundBox[1] >= roads.get(t).get(i)
.getLongitudeE6()
&& boundBox[2] <= roads.get(t).get(i)
.getLatitudeE6()
&& boundBox[3] <= roads.get(t).get(i)
.getLongitudeE6()) {


MyOverlay mOverlay = new MyOverlay();
mOverlay.setColor(Color.GREEN);

mOverlay.setWidth(4);
mOverlay.setPair(roads.get(t).get(i),
roads.get(t).get(i + 1));
mapOverlays.add(mOverlay);
}
}
}

 class MyOverlay extends Overlay {

GeoPoint gp1 = new GeoPoint(0, 0);
GeoPoint gp2 = new GeoPoint(0, 0);
int colr=0,width=0;



public MyOverlay() {

}

public void draw(Canvas canvas, MapView mapv, boolean shadow) {
super.draw(canvas, mapv, false);

Paint mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(colr);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(width);

Point p1 = new Point();
Point p2 = new Point();

Path path = new Path();

Projection projection = mapv.getProjection();
projection.toPixels(gp1, p1);
projection.toPixels(gp2, p2);

path.moveTo((float) p2.x, (float) p2.y);
path.lineTo((float) p1.x, (float) p1.y);


// canvas.drawBitmap(markerBitmap, point.x, point.y, null);

canvas.drawPath(path, mPaint);
//canvas.drawBitmap(bitmap, src, dst, paint);

}

public void setPair(GeoPoint gpone, GeoPoint gptwo) {

gp1 = gpone;
gp2 = gptwo;

}

public void setColor(int clr)
{
colr=clr;
}

public void setWidth(int w)
{
width=w;
}


}

有人能解决我的问题吗?

最佳答案

您可以采取一些措施来提高效率。

您的第一个代码块可以稍微更有效率:

for (int t = 0, size = roads.size(); t < size; t++) { //Avoid calling '.size()' over and over
for (int i = 0; i < roads.get(t).size() - 1; i++) {//Avoid calling '.size()' over and over
final GeoPoint road = roads.get(t).get(i); //Reduce the number of get() calls.
if (boundBox[0] >= road.getLatitudeE6()
&& boundBox[1] >= road.getLongitudeE6()
&& boundBox[2] <= road.getLatitudeE6()
&& boundBox[3] <= road.getLongitudeE6()) {
MyOverlay mOverlay = new MyOverlay();
mOverlay.setColor(Color.GREEN);
mOverlay.setWidth(4);
mOverlay.setPair(road, roads.get(t).get(i + 1));
mapOverlays.add(mOverlay);
}
}
}

但最重要的是,我在您的代码中看到的最大性能消耗是每次调用 draw() 时您都在分配新的渲染对象(Paint、Path、Point)。这可以重构,因此您可以重用相同的 Paint 实例:

class MyOverlay extends Overlay {
GeoPoint gp1 = new GeoPoint(0, 0);
GeoPoint gp2 = new GeoPoint(0, 0);
Point p1 = new Point();
Point p2 = new Point();
Path path = new Path();
int colr=0,width=0;

public MyOverlay() {
Paint mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(colr);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(width);
}

public void draw(Canvas canvas, MapView mapv, boolean shadow) {
super.draw(canvas, mapv, false);

path.reset();

Projection projection = mapv.getProjection();
projection.toPixels(gp1, p1);
projection.toPixels(gp2, p2);

path.moveTo((float) p2.x, (float) p2.y);
path.lineTo((float) p1.x, (float) p1.y);
canvas.drawPath(path, mPaint);
}
}

有关更多信息,请参阅此处文章的“注意事项”部分:http://android-developers.blogspot.com.au/2011/03/android-30-hardware-acceleration.html .

文章的相关要点是:“不要在绘制方法中创建渲染对象:一个常见的错误是每次调用渲染方法时都创建一个新的 Paint 或一个新的 Path。这是不仅浪费,迫使系统更频繁地运行 GC,还绕过了硬件管道中的缓存和优化。”

关于Android 谷歌地图绘制道路性能问题 10.000+ 覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9729951/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com