【问题标题】:Determine drawn Rect from Canvas (Determine position of OverlayItem)从 Canvas 确定绘制的 Rect(确定 OverlayItem 的位置)
【发布时间】:2012-08-22 15:15:52
【问题描述】:

我正在尝试仅绘制可见的 OverlayItems,这就是为什么我确定地图可见 rect 但我无法确定 Canvas 将在其中绘制 OverlayItem 的 Rect。

这就是我到目前为止所做的(逐项叠加中的方法).. 但是 getClipBounds() 没有返回正确的 Rect

@Override
    public void draw(Canvas canvas, MapView map, boolean shadow) {
        if (getMapBounds().intersect(canvas.getClipBounds())) {
            super.draw(canvas, map, false);
        }
    }

我不想绘制其他 OverlayItem,我想知道我的画布是否在地图视图的可见矩形内绘制了一些东西 因为如果不是我不画这个画布 这样做是为了加快具有近 2000 个叠加项的地图视图

【问题讨论】:

  • 问题看不懂,有详细解释吗?

标签: android google-maps canvas overlay itemizedoverlay


【解决方案1】:

如果您想绘制类似这样的叠加层:

所以基本上你要做的就是在画布的帮助下将 customImage 强加在背景框上。使用此实现,您可以有效地从画布创建 BitmapDrawable,然后您可以将其指定为“ItemizedOverlay”的标记。如果这是您要查找的逐项叠加类型,则无需覆盖逐项叠加类的绘制函数。您需要做的就是使用以下代码创建一个 BitmapDrawable,您可以在其构造函数中将其分配给 ItemizedOverlay。这是执行此操作的函数:

public BitmapDrawable imageOnDrawable(int drawableBackground, Bitmap customImage)
{
//The following line is optional but I'd advise you to minimize the size of 
//the size of the bitmap (using a thumbnail) in order to improve draw
//performance of the overlays (especially if you are creating a lot of overlays).

Bitmap customImageThumbnail = ThumbnailUtils.extractThumbnail(
                                                customImage, 100, 100); 

Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId);
bm = Bitmap.createScaledBitmap(bm, 112, 120, false);

Canvas canvas = new Canvas(bm);
canvas.drawBitmap(bm, 0, 0, null);
// The 6,6 in the below line refer to the offset of the customImage/Thumbnail
// from the top-left corner of the background box (or whatever you want to use
// as your background) 
canvas.drawBitmap(customImageThumbnail, 6, 6, null); 

return new BitmapDrawable(bm);

}

【讨论】:

  • 我有 OverlayItems 我正在绘制我不知道的是这个画布是否在地图视图的可见矩形内绘制覆盖项......因为如果不是我不会绘制覆盖项提高地图视图的速度
  • 这似乎很难仅仅基于画布来确定,因为画布没有它自己的地理方位,而包含在逐项覆盖中的覆盖有自己的地理点。所以我想知道您是使用逐项叠加作为多个叠加的容器,还是为每个叠加使用不同的逐项叠加对象?
  • 一个 Itemized Overlay 包含多个 OverlayItems 但有不止一个 Itemized Overlay... 那么在 OverlayItem 的 Draw 中这样做更好/更容易吗?如果是,我怎样才能在那里得到绘制的矩形?
【解决方案2】:

我现在通过遍历项目并检查它们是否在地图上来做到这一点就像这样:

@Override
    public void draw(Canvas canvas, MapView map, boolean shadow) {
        for (int i = 0; i < mOverlays.size(); i++) {
            if (isLocationVisible(mOverlays.get(i).getPoint())) {
                super.draw(canvas, map, false);
            }
        }       
    } 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-28
    • 2014-06-23
    • 2016-11-02
    相关资源
    最近更新 更多