2

この先どうしたのか気になります。私のアプリは、URL を使用して画像を読み込みます。ロードと表示に成功しました。問題は、画像が表示されたときに間違った画像が表示されることです...画像で表示する方が良いです:

ここに画像の説明を入力

最初は、他の画像を読み込んでいる間に、正しい画像が表示されます[TOP IMAGE]。しかし、すべての画像が読み込まれた後、img2複製して置き換えますimg1が、まだ img1 の寸法を使用していますか?

これは、画像をロードするときに使用するコードです。

  public class DrawableBackgroundDownloader {    

  private final Map<String, Drawable> mCache = new HashMap<String, Drawable>();   
  private final LinkedList <Drawable> mChacheController = new LinkedList <Drawable> ();
  private ExecutorService mThreadPool;  
  private final Map<ImageView, String> mImageViews = Collections.synchronizedMap(new WeakHashMap<ImageView, String>());  
  public static int MAX_CACHE_SIZE = 80; 
  public int THREAD_POOL_SIZE = 3;



  /**
   * Constructor
   */
  public DrawableBackgroundDownloader() {  
      mThreadPool = Executors.newFixedThreadPool(THREAD_POOL_SIZE);  
  }  


  /**
   * Clears all instance data and stops running threads
  */ 
  public void Reset() {
      ExecutorService oldThreadPool = mThreadPool;
      mThreadPool = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
      oldThreadPool.shutdownNow();

      mChacheController.clear();
      mCache.clear();
      mImageViews.clear();
  }  

  /**
   * Load the drawable associated to a url and assign it to an image, you can set a placeholder to replace this drawable.
   * @param url Is the url of the image.
   * @param imageView The image to assign the drawable.
   * @param placeholder A drawable that is show during the image is downloading.
   */
  public void loadDrawable(final String url, final ImageView imageView,Drawable placeholder) {  
      if(!mImageViews.containsKey(url))
          mImageViews.put(imageView, url);  
      Drawable drawable = getDrawableFromCache(url);  

      // check in UI thread, so no concurrency issues  
      if (drawable != null) {  
          //Log.d(null, "Item loaded from mCache: " + url);  
          imageView.setImageDrawable(drawable);  
      } else {  
          imageView.setImageDrawable(placeholder);  
          queueJob(url, imageView, placeholder);  
      }  
  } 


  /**
   * Return a drawable from the cache.
   * @param url url of the image.
   * @return a Drawable in case that the image exist in the cache, else returns null.
   */
  public Drawable getDrawableFromCache(String url) {  
      if (mCache.containsKey(url)) {  
          return mCache.get(url);  
      }  

      return null;  
  }


  /**
   * Save the image to cache memory.
   * @param url The image url
   * @param drawable The drawable to save.
   */
  private synchronized void putDrawableInCache(String url,Drawable drawable) {  
      int chacheControllerSize = mChacheController.size();
      if (chacheControllerSize > MAX_CACHE_SIZE) 
          mChacheController.subList(0, MAX_CACHE_SIZE/2).clear();

      mChacheController.addLast(drawable);
      mCache.put(url, drawable);

  }  

  /**
   * Queue the job to download the image.
   * @param url Image url.
   * @param imageView The ImageView where is assigned the drawable.
   * @param placeholder The drawable that is show during the image is downloading. 
   */
  private void queueJob(final String url, final ImageView imageView,final Drawable placeholder) {  
      /* Create handler in UI thread.  */
      final Handler handler = new Handler() {  
          @Override  
          public void handleMessage(Message msg) {  
              String tag = mImageViews.get(imageView);  
              if (tag != null && tag.equals(url)) {
                  if (imageView.isShown())
                      if (msg.obj != null) {
                          imageView.setImageDrawable((Drawable) msg.obj);  
                      } else {  
                          imageView.setImageDrawable(placeholder);  
                          //Log.d(null, "fail " + url);  
                      } 
              }  
          }  
      };  

      mThreadPool.submit(new Runnable() {  
          public void run() {  
              final Drawable bmp = downloadDrawable(url);
              // if the view is not visible anymore, the image will be ready for next time in cache
              if (imageView.isShown())
              {
                  Message message = Message.obtain();  
                  message.obj = bmp;
                  //Log.d(null, "Item downloaded: " + url);  

                  handler.sendMessage(message);
              }
          }  
      });  
  }  


  /**
   * Method that download the image
   * @param url The url image.
   * @return Returns the drawable associated to this image.
   */
  private Drawable downloadDrawable(String url) {  
      try {  
          InputStream is = getInputStream(url);

          Drawable drawable = Drawable.createFromStream(is, url);
          putDrawableInCache(url,drawable);  
          return drawable;  

      } catch (MalformedURLException e) {  
          e.printStackTrace();  
      } catch (IOException e) {  
          e.printStackTrace();  
      }  

      return null;  
  }  

  /**
   * This method manage the connection to download the image.
   * @param urlString url of the image.
   * @return Returns an InputStream associated with the url image.
   * @throws MalformedURLException
   * @throws IOException
   */
  private InputStream getInputStream(String urlString) throws MalformedURLException, IOException {
      URL url = new URL(urlString);
      URLConnection connection;
      connection = url.openConnection();
      connection.setUseCaches(true); 
      connection.connect();
      InputStream response = connection.getInputStream();

      return response;
  }
  }

また、上記のコードを使用して画像をダウンロードしたところ、他のアクティビティで、すべての画像が読み込まれた後、水平スクロール (devsmart) が機能しなくなったことに気付きました。画像が読み込まれる前にスクロールしようとすると、まだ機能します。これに続いて別のものを使用しようとしましたが、スクロールは機能しますが、リークの問題のために画像を読み込めません...誰でもこの問題に関するアイデアを共有できますか? どんな助けでも大歓迎です。

4

1 に答える 1

0

そこで、このライブラリ、koushの UrlImageViewHelper を使用することにしました。これで私の問題は解決しました。画像の読み込みは私のものより安定しているようです。

于 2013-04-23T09:59:20.723 に答える