2017-04-18 33 views
0

我正在使用googlemap。我已經實現了自定義標記窗口,這個窗口包含了標記的照片。我將照片的網址作爲字符串,並使用線程將其設置爲imageview。但是,代碼不與用戶同步。當我點擊標記時,它會顯示我點擊過的上一個標記的照片。當我進行調試時,我可以看到真正的url值來自背景,但設置圖像進入imageview操作正變得遲。我怎麼解決這個問題?我嘗試過畢加索和滑翔,但仍然是同樣的問題。試圖設置Imageview與後臺操作

@Override 
public void onMapReady(GoogleMap map) { 

    this.Window = (ViewGroup) getLayoutInflater().inflate(R.layout.marker_window, null); 
    this.imageview = (ImageView) Window.findViewById(R.id.marker_photo); 
    googleMap = map; 
     googleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() { 
      @Override 
      public View getInfoWindow(Marker marker) { 
       return null; 
      } 

      @Override 
      public View getInfoContents(final Marker marker) { 
        createThread(imageview,URL).start(); 
        mapWrapperLayout.setMarkerWithInfoWindow(marker, infoWindow); 
        return infoWindow; 
       } 
       return null;} 
     }); 
} 

private synchronized Thread createThread(final ImageView imageView, final String URL) 
{ 
    Thread thread=new Thread(new Runnable() { 
     @Override 
     public void run() { 
      download_image(URL,imageView); 
     } 
    }); 
    return thread; 
} 

public void download_image(String url,ImageView imageview) { 
    URL newurl = null; 

    try { 
     newurl = new URL(url); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } 

    Bitmap bitmap = null; 

    try { 
     bitmap= BitmapFactory.decodeStream(newurl.openConnection().getInputStream()); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    imageview.setImageBitmap(bitmap); 
} 

回答

0

您正在將ImageView上的圖像設置爲脫離主UI線程。如果此代碼在Activity內運行,則可以使用imageview.setImageBitmap(bitmap);調用中的runOnUiThread(Runnable runnable);強制它在UI線程上發生。如果您不在活動中,則可以使用由主Looper或AsyncTask框架創建的Handler