从画布确定绘制的Rect(确定OverlayItem的位置)

时间:2012-08-22 15:15:52

标签: android google-maps canvas overlay itemizedoverlay

我正在尝试绘制可见的OverlayItems,这就是为什么我确定地图可见的矩形,但是我无法确定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);
        }
    }

我不想绘制其他OverlayItems,我想知道我的画布是否在地图视图的可见区域内绘制了一些内容 因为如果不是我不画这个画布 这样做是为了加速具有近2000个叠加项目的mapview

2 个答案:

答案 0 :(得分:0)

如果您要绘制这些叠加层:

所以基本上你要做的就是在画布的帮助下将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);

}

答案 1 :(得分:0)

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

@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);
            }
        }       
    } 
相关问题