2012-06-04 88 views
0

我有一個應用程序,它使用自定義實現MyLocationOverlayMyLocationOverlay - 自定義圖像,但沒有陰影

在實現中,我通過Draw的重載設置了一個Bitmap屬性,該屬性在指定時使用。

它工作得很好,用我的自定義圖像,但它並沒有顯示陰影 - 如在ItemizedOverlay自動完成。

任何人都可以幫忙嗎?

這裏是(從相關的代碼)我的課:

public class LocationOverlay: MyLocationOverlay 
{ 
    /// <summary>Bitmap to use for indicating the current fixed location.</summary> 
    public Bitmap LocationMarker { get; set; } 

    /// <summary>Uses the custom marker bitmap if one has been specified. Otherwise, the default is used.</summary> 
    /// <param name="canvas"></param> 
    /// <param name="mapView"></param> 
    /// <param name="shadow"></param> 
    /// <param name="when"></param> 
    /// <returns></returns> 
    public override bool Draw(Canvas canvas, MapView mapView, bool shadow, long when) 
    { 
     var drawShadow = shadow; 

     if (LocationMarker != null && LastFix != null) 
     { 
      var screenPoint = new Point(); 
      var geoPoint = new GeoPoint((int)(LastFix.Latitude * 1E6), (int)(LastFix.Longitude * 1E6)); 
      mapView.Projection.ToPixels(geoPoint, screenPoint); 
      canvas.DrawBitmap(LocationMarker, screenPoint.X, screenPoint.Y - 32, null); 
      drawShadow = true; 
     } 

     base.Draw(canvas, mapView, drawShadow); 
     return true; 
    } 
} 

回答

1

明白了。您可以使用DrawAt自動繪製陰影。

所以我的財產變成了可繪製正是如此:

public Drawable MarkerResource 
{ 
    set 
    { 
     _locationMarker = value; 
     var widthOffset = _locationMarker.IntrinsicWidth/2; 
     _locationMarker.SetBounds(-widthOffset, -_locationMarker.IntrinsicHeight, widthOffset, 0); 
    } 
} 

和重載現在看起來是這樣的:

public override bool Draw(Canvas canvas, MapView mapView, bool shadow, long when) 
{ 
    if (LastFix == null) return false; 

    if (_locationMarker != null) 
    { 
     var screenPoint = new Point(); 
     var geoPoint = new GeoPoint((int)(LastFix.Latitude * 1E6), (int)(LastFix.Longitude * 1E6)); 
     mapView.Projection.ToPixels(geoPoint, screenPoint); 
     DrawAt(canvas, _locationMarker, screenPoint.X, screenPoint.Y, shadow); 
    } 
    else if (MyLocation != null) DrawMyLocation(canvas, mapView, LastFix, MyLocation, when); 

    if (IsCompassEnabled) DrawCompass(canvas, Orientation); 
    return false; 
} 

完美的作品。

這現在也尊重使指南針,如果沒有指定可繪製將借鑑使用默認的技術。

+0

我對他的[這裏](http://spiritmachineblog.tumblr.com/post/31148616147/customising-mylocationoverlay-in-mono-for-android)進行了博客,您可以在其中看到_MyLocationOverlay_的完全自定義實現。 – manadart

2

我想你錯過了陰影參數的點。地圖會調用draw()方法兩次,它會告訴您這是否是影子傳遞。它並沒有爲你畫出陰影。所以你的代碼看起來像這樣:

public override bool Draw(Canvas canvas, MapView mapView, bool shadow, long when) 
{ 
    if (LocationMarker != null && LastFix != null) 
    { 
     var screenPoint = new Point(); 
     var geoPoint = new GeoPoint((int)(LastFix.Latitude * 1E6), (int)(LastFix.Longitude * 1E6)); 
     mapView.Projection.ToPixels(geoPoint, screenPoint); 

     if(shadow) 
     { 
      // Draw your shadow bitmap here 
     } 
     else 
     { 
      canvas.DrawBitmap(LocationMarker, screenPoint.X, screenPoint.Y - 32, null); 
     } 
    } 

    return true; 
} 
+0

感謝您的澄清。那麼沒有辦法像使用ItemizedOverlay一樣利用自動繪製的陰影? – manadart