2015-02-09 126 views
0

我想在Google Maps v2的InfoWindow中加載動態圖像。我在加載內存中的圖像和getInfoContents返回要顯示的視圖之間有一個有趣的競爭條件。即使圖像位於內存中,但在位圖中,它可能無法在返回視圖時完成繪製。我在網上嘗試了很多解決方案,但都沒有100%。Google Maps InfoWindow ImageView

下面是我目前的代碼。我重寫GoogleMap.InfoWindowAdapter的getInfoContents方法。

任何人有一個想法如何處理這個?

@Override 
    public View getInfoContents(final Marker marker) { 
     final View view = LayoutInflater.from(context).inflate(R.layout.template_map_info_window, null); 

     final ImageView vehicleImage = (ImageView) view.findViewById(R.id.infowindow_image); 
     vehicleImage.setImageBitmap(myBitmap); 

     return view; 
    } 

回答

2

你可以從here獲得一些想法。

此外,還可以考慮使用這個庫Picasso

考慮器具Callback,你的圖像加載完成,這將是顯示。

示例代碼:

@Override 
     public View getInfoContents(Marker marker) { 

      ImageView imageView = (ImageView)myContentsView.findViewById(R.id.imgView); 
      if (not_first_time_showing_info_window) { 
       Picasso.with(MainActivity.this).load(YOU_IMAGE).into(imageView); 
      } else { // if it's the first time, load the image with the callback set 
       not_first_time_showing_info_window=true; 
       Picasso.with(MainActivity.this).load(YOU_IMAGE).into(imageView,new InfoWindowRefresher(marker)); 
      } 

      return myContentsView; 
     } 

private class InfoWindowRefresher implements Callback { 
     private Marker markerToRefresh; 

     private InfoWindowRefresher(Marker markerToRefresh) { 
      this.markerToRefresh = markerToRefresh; 
     } 

     @Override 
     public void onSuccess() { 
      markerToRefresh.showInfoWindow(); 
     } 

     @Override 
     public void onError() {} 
    } 
相關問題