2014-10-17 76 views
1

嗨,大家好我正在开发一个应用程序与地图,我做了四个TextView和一个ImageView自定义infowindow。现在我知道一个InfoWindow是一个预渲染的图像,所以我的问题是我想为每个标记出现一个个性化的图像:它的工作原理很简单,但是在第一次点击时,第二次点击时只有一个空白区域,图像显示成功。我该如何解决ImageView问题?

第一次轻触:first_tap

然后当我点击标记上的第二次: second_tap

我显然想避免敲击两次,使PIC show..it是一个大问题。我使用Picasso库从网络加载图像,并保留来自四个xml文件的图像URL。所以我明白问题是,当系统缓存图像时,系统可以加载图像。所以我想到了这个解决方法:当应用程序开始调用Asynctask以在不可见的ImageView上加载图像时,但是这只适用于它从xml保留的最后一个URL,而不是其他标记。 所以我想问你,如果你有一些想法做到这一点。

下面我的代码:

在开始的时候我打电话的AsyncTask:

View v = this.getWindow().getDecorView().findViewById(R.id.immaginenascosta); 
CacheTask cacheTask = new CacheTask(v); 
cacheTask.execute(); 

CacheTask,从互联网延伸的AsyncTask下载xml文件并保存在SD卡(后台)类的话,就从读取XML(SD卡上)和上飘ImageView的加载图像:

CacheTask

public class CacheTask extends AsyncTask<Void, Void, Void>{ 

    String [] imgurlarray = {}; 
    int dim = 0; 

    String root = "http://www.emmebistudio.com/markers_Predappio_living/"; 
    String [] arraymarkers = {"markers_history.xml","markers_hotel.xml","markers_restaurant.xml","markers_souvenir.xml"}; 

    String pathdirectory = "/sdcard/PredappioLiving"; 

    private View view; 


    public CacheTask(View view){ 
     this.view = view; 

    } 

    public void MakeCache (View v) 
    { 

    } 


    @Override 
    protected Void doInBackground(Void... params) { 


     for (int i = 0; i < arraymarkers.length; i++) 
     { 
      String urlcompleta = root+arraymarkers[i]; 

      //Estraggo la stringa del nome dall'url-------------------------------------------------------------- 

      String stringurl = urlcompleta.toString();   // Prende: http://www.emmebistudio.com/Predappio_living/markers.xml e lo mette in array 
      String [] urlarray = stringurl.split("/"); //   ^^  ^    ^   ^
                 //   | |   |     |    | 
                 //   | |   |     |    | 
      String nomefile = urlarray[4];    //   0 1   2     3    4 

      //Adattare a seconda della posizione definitiva dei file XML 

      //---------------------------------------------------------------------------------------------------- 


      try { 
       URL url = new URL(urlcompleta); 

       HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); 
       urlConnection.setRequestMethod("GET"); 
       urlConnection.setDoOutput(true); 
       urlConnection.connect(); 

       File dir = new File(pathdirectory); 
       dir.mkdirs(); 

       File fileWithinMyDir = new File(dir, nomefile); 
       FileOutputStream fileOutput = new FileOutputStream(fileWithinMyDir); 

       InputStream inputStream = urlConnection.getInputStream(); 

       int totalSize = urlConnection.getContentLength(); //mi salvo la lunghezza del file da salvare 
       int downloadedSize = 0; 
       byte[] buffer = new byte[1024]; 
       int bufferLength = 0; //per salvare usiamo una dimensione temporanea del buffer 
             // salvo attraverso il buffer di input e scrivo il contenuto nel file. 
       while((bufferLength = inputStream.read(buffer)) > 0){ 
        fileOutput.write(buffer,0,bufferLength); 
        downloadedSize += bufferLength; 
        int progress = (int)(downloadedSize*100/totalSize); 
       } 

       fileOutput.close(); 
      } 

      catch(Exception e){ 
       e.printStackTrace(); 
      } 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     for (int i = 0; i < arraymarkers.length; i++) 
     { 
      String urlcompleta = root+arraymarkers[i]; 

      //Estraggo la stringa del nome dall'url-------------------------------------------------------------- 

      String stringurl = urlcompleta.toString();   // Prende: http://www.emmebistudio.com/Predappio_living/markers.xml e lo mette in array 
      String [] urlarray = stringurl.split("/"); //   ^^  ^    ^   ^
                 //   | |   |     |    | 
                 //   | |   |     |    | 
      String nomefile = urlarray[4];    //   0 1   2     3    4 

      //Adattare a seconda della posizione definitiva dei file XML 

      //---------------------------------------------------------------------------------------------------- 

      try{ 
       /* * * * * Inizio Parsing * * * * */ 
       /***************************************/ 
       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
       DocumentBuilder builder = factory.newDocumentBuilder(); 
       Document doc = builder.parse(new FileInputStream(pathdirectory+"/"+nomefile)); 
       doc.getDocumentElement().normalize(); 

       NodeList markers = doc.getElementsByTagName("marker"); 

       ImageView []image = new ImageView[100]; 
       image[i]=(ImageView) view.findViewById(R.id.immaginenascosta); 
       //ImageView image = (ImageView) view.findViewById(R.id.immaginenascosta); 
       for (int j=0; j<markers.getLength(); j++){ 
        Element item = (Element) markers.item(i); 
        String urlimmagine = item.getAttribute("immagine"); 

        Picasso.with(view.getContext()) 
         .load(urlimmagine) 
         .error(R.drawable.ic_launcher) //in caso di errore fa vedere questa immagine (un triangolo penserei) 
         .resize(150, 110) 
         .into(image[i]);      

       } 
      /***************************************/ 
       /* * * * * Fine Parsing  * * * * */ 
      }catch(Exception e){ 
       e.printStackTrace(); 
      } 


     super.onPostExecute(result); 
    } 
} 
} 

这就是 “飘” 的ImageView的布局:

<ImageView 
    android:id="@+id/immaginenascosta" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:visibility="gone"/> 

这是setInfoWindowAdapter:

map.setInfoWindowAdapter(new InfoWindowAdapter() { 

    View v = getLayoutInflater().inflate(R.layout.custom_info_window, null); 



    @Override 
    public View getInfoWindow(Marker marker) { 
     return null; 
    } 

    @Override 
    public View getInfoContents(Marker marker) { 

    //Prova immagine custom 
    /***********************************/ 
    //final ProgressDialog dialog = ProgressDialog.show(MainActivity.this, "Attendere...", "Caricamento delle immagini in corso..", true); 

    BuildInfoMatrix req = new BuildInfoMatrix(); 

    String nome = marker.getTitle(); 
    String currentUrl = ""; 
    //String currentUrl=req.findImageUrl(nome); 
    int vuoto = -15; 
    try{ 
     currentUrl=req.findImageUrl(nome); 
    }catch(Exception e){ 
     currentUrl = "http://upload.wikimedia.org/wikipedia/commons/b/bb/XScreenSaver_simulating_Windows_9x_BSOD.png"; 
    } 
    if (currentUrl == null) 
    { 
     currentUrl = "http://upload.wikimedia.org/wikipedia/commons/b/bb/XScreenSaver_simulating_Windows_9x_BSOD.png"; 
    } 
    ImageView image; 
    image = (ImageView) v.findViewById(R.id.image_nuvoletta); 

    Picasso.with(v.getContext()) 
     .load(currentUrl) 
     .error(R.drawable.ic_launcher) //in caso di errore fa vedere questa immagine (un triangolo penserei) 
     .resize(150, 110) 
     .into(image, new Callback(){ 

     @Override 
     public void onSuccess(){ 

     } 

     @Override 
     public void onError(){ 

     } 
}); 


    /***********************************/ 


    /* Distanza a piedi e macchina sulla nuvoletta */ 
    /***********************************************/ 
    GPSTracker gpsTracker = new GPSTracker(MainActivity.this); 

    if (gpsTracker.canGetLocation()) 
    { 
     String stringLatitude = String.valueOf(gpsTracker.latitude); 
     String stringLongitude = String.valueOf(gpsTracker.longitude); 
     double currentLat = Double.parseDouble(stringLatitude); 
     double currentLng = Double.parseDouble(stringLongitude); 

     double destLat = marker.getPosition().latitude; 
     double destLng = marker.getPosition().longitude; 

     final float[] results = new float[3]; 
     Location.distanceBetween(currentLat, currentLng, destLat, destLng, results); 

     float metri = results[0]; 
     float km = Math.round((double)metri/1000); 

     int minuti_persona = (int)Math.round(metri/125); //125 metri al minuto -> velocità media di 2,5 m/s 
     int minuti_auto = (int)Math.round(km/0.7);   //700 metri al minuto -> velocità media di 42 km/h 


     /***********************************************/ 



     TextView tvTitle = (TextView) v.findViewById(R.id.title); 
     TextView tvSnippet = (TextView) v.findViewById(R.id.snippet); 
     tvSnippet.setTypeface(tvSnippet.getTypeface(), Typeface.ITALIC); //indirizzo in corsivo 
     TextView tvPedonal_distance = (TextView) v.findViewById(R.id.pedonal_time); 
     TextView tvCar_distance = (TextView) v.findViewById(R.id.car_time); 
     tvTitle.setText(marker.getTitle()); 
     tvSnippet.setText(marker.getSnippet()); 

     if(minuti_persona <=0)   // Stampa tempo per coprire la distanza 
     { 
      tvCar_distance.setText("A piedi: meno di un minuto"); 
     }else 
     { 
      tvPedonal_distance.setText("A piedi: "+minuti_persona+ " minuti"); 
     } 

     if(minuti_auto <= 0) 
     { 
      tvCar_distance.setText("In auto: meno di un minuto");         
     }else 
     { 
      tvCar_distance.setText("In auto: " +minuti_auto+ " minuti"); 
     } 

     return v; 
    }else 
    { 
     TextView tvTitle = (TextView) v.findViewById(R.id.title); 
     TextView tvSnippet = (TextView) v.findViewById(R.id.snippet); 
     tvTitle.setText(marker.getTitle()); 
     tvSnippet.setText(marker.getSnippet()); 
     return v; 
    } 

} 

}); 

谢谢大家。

+0

为什么它的知名度没有了吗?当你点击第二次气球时发生了什么? – Blackbelt 2014-10-17 09:43:58

+0

我做了一个不可见的图像预览图像InfoWindow的ImageViews。当我点击气球出现一个对话框 – 2014-10-17 09:53:18

回答

0

由于图像加载的异步性质,诀窍是在图像加载到内存后,将显示的信息窗口“无效”。为了做到这一点,您必须跟踪最后一次点击的标记,并检查它是否仍在显示其信息窗口。然后再打电话showInfoWindow()强制信息窗口的视觉更新:

if (markerShowingInfoWindow != null && markerShowingInfoWindow.isShowingInfoWindow()) { 
    markerShowingInfoWindow.showInfoWindow(); 
} 

有关详细信息,请参阅: