2017-05-15 7 views
1

Googleマップのカスタムマーカーを作成する方法については、this tutorialに従っています。この部分は動作するようになっています。しかし、マーカーで使用している画像のサイズを変更するには、コードを変更する必要がありました。私はこのGoogleマップ画像が歪んでいる(Xamarin)

ような何かをするだろう、元のコードで lol waht i tyep hrer

private void UpdateMarkers(float zoom) 
    { 
     // Max zoom out => zoom = 3 
     // Max zoom in => zoom = 21 
     int dimension = (int)zoom * 10; 
     if (dimension == currentDimension) 
      return; 
     currentDimension = dimension; 
     map.Clear(); 
     foreach (var pin in customPins) 
     { 
      var immutableBitmap = BitmapFactory.DecodeResource(Context.Resources, Resource.Drawable.icon); 
      var mutableBitmap = immutableBitmap.Copy(Bitmap.Config.Argb8888, true); 
      mutableBitmap.Height = dimension; 
      mutableBitmap.Width = dimension; 
      BitmapDescriptorFactory.FromBitmap(mutableBitmap); 
      var img = BitmapDescriptorFactory.FromBitmap(mutableBitmap); 

      var marker = new MarkerOptions(); 
      marker.SetPosition(new LatLng(pin.Pin.Position.Latitude, pin.Pin.Position.Longitude)); 
      marker.SetTitle(pin.Pin.Label); 
      marker.SetSnippet(pin.Pin.Address); 
      marker.SetIcon(img); 

      map.AddMarker(marker); 
     } 

    } 

そして、ここではそれがどのように見えるかの写真は次のとおりです。

これは私がやって使用しています方法はサイズを変更で

var img = BitmapDescriptorFactory.FromResource(Resource.Drawable.icon); 

これは機能します。しかし、私は画像のサイズを変更したいので、私はのような方法を見つけました。

何が起こっているのでしょうか?

答えて

1

あなたはBitmapを持っていたら、Bitmap.CreateScaledBitmapを経由して、それを拡張することができます

var bitmap = BitmapFactory.DecodeResource(Context.Resources, Resource.Drawable.icon); 
var scaledBitmap = Bitmap.CreateScaledBitmap(bitmap, dimension, dimension, false); 
var img = BitmapDescriptorFactory.FromBitmap(scaledBitmap); 

注:メモリリークを避けるために、必ずRecycleDisposeあなたのビットマップを作成します。

メモBitmapFactoryのDecodeResourceCreateScaledBitmapは重いAPIコールです。foreach (var pin in customPins)ループで結果をキャッシュするのとは異なり、キャッシュすることができます。

+0

ありがとうございます!これは魅力のように機能します:)最適化のヒントのおかげで、間違いなくこれをやります! – mathkid91

関連する問題