【问题标题】:Application crashes displaying a GroundOverlay made with a Canvas in Google Maps Android API v2在 Google Maps Android API v2 中显示使用 Canvas 制作的 GroundOverlay 的应用程序崩溃
【发布时间】:2013-04-22 17:49:05
【问题描述】:

我在我的地图上显示了一个 GroundOverlay,它的图像是用画布制作的,我在画布上画了一个弧线,但我遇到了一些问题:首先,应用程序在一段时间后崩溃(它给我是一个 java.lang.OutOfMemoryError),它没有显示覆盖。我尝试在叠加层的图片中放置白色背景并显示它,所以我猜问题来自弧线,但我不知道我做错了什么。有人知道吗?

Projection projection = map.getProjection();

                    Point point1 = projection.toScreenLocation(latlng1);
                    Point point2 = projection.toScreenLocation(latlng2);

                    float startAngle = (float) (Math.atan2(point1.y - point2.y,
                            point1.x - point2.x));
                    float sweepAngle = (float) (GenericNdData.getLateralTrajectory(
                            T_FplnType.ACTIVE.getId()).getSegment(i).getAngle());

                    float radius = FloatMath.sqrt((float) (Math.pow(
                            (point1.x - point2.x), 2) + Math.pow(
                            (point1.y - point2.y), 2)));
                    RectF rectangle = new RectF(point2.x - radius, point2.y
                            - radius, point2.x + radius, point2.y + radius);

                    Paint paint = new Paint();

                    paint.setARGB(250, 0, 255, 0);
                    paint.setAntiAlias(true);
                    paint.setSubpixelText(true);
                    paint.setFakeBoldText(true);
                    paint.setStrokeWidth(4f * Configuration.General.getScreenFactor());

                    paint.setStyle(Paint.Style.STROKE);

                    Bitmap arc = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);

                    Canvas canvas = new Canvas(arc);
                    canvas.drawColor(0xFFFFFFFF);
                    canvas.drawArc(rectangle,
                            (float) (Math.toDegrees(startAngle)),
                            (float) (Math.toDegrees(sweepAngle)), false, paint);

                    GroundOverlay groundArc = map.addGroundOverlay(new GroundOverlayOptions()
                     .image(BitmapDescriptorFactory.fromBitmap(arc))
                     .position(latlng2, 10000));

提前致谢。

【问题讨论】:

  • 这段代码在哪里调用?
  • 在显示地图的 Fragment 中(不是 MapFragment,我自己制作的)
  • 在 onResume 或类似的地方?我想知道这段代码是否被意外调用了多次。
  • 哦,在每次有更新时调用的函数中(我的应用程序显示从另一个程序获取的信息)。基本上每次对另一个应用程序进行修改时,都会再次调用该函数,并且弧会发生变化。

标签: android google-maps android-canvas google-maps-android-api-2


【解决方案1】:

与标记一起使用时,存在与BitmapDescriptorFactory.fromBitmap() 相关的已知内存泄漏问题。这可能是一个问题,但首先尝试:

GroundOverlay.remove()

调用GoogleMap.addGroundOverlay之前添加的对象。

【讨论】:

  • 我刚刚添加了这个,它似乎正在工作,谢谢!但是,我仍然有弧线没有显示的问题。
  • 可能是笔画宽度值的问题?尝试硬编码 100 作为宽度。
  • 好的,我自己解决了这个问题:您需要替换“RectF rectangle = new RectF(point2.x - radius, point2.y - radius, point2.x + radius, point2.y + 半径);" by : "RectF 矩形 = new RectF(0, 0, radius*2, radius*2);" (与上一行一样,圆弧是在屏幕位置对应latlng坐标绘制的,但是由于我们使用的是canvas,所以必须在(0,0)坐标处绘制圆弧)
猜你喜欢
  • 1970-01-01
  • 2013-10-03
  • 2013-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-13
  • 2012-11-23
  • 1970-01-01
相关资源
最近更新 更多