2015-02-09 127 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() {} 
    } 
相关问题