Exemple #1
0
  private Bitmap prepare(final Bitmap map) {
    int dstW = request.getRequiredWidth(), dstH = request.getRequiredHeight();
    if (dstW <= 0 || dstH <= 0 || request.isSkipScaleBeforeMemCache()) {
      if (imagesManager.debug) {
        Log.d(
            TAG,
            "Skip scaling for "
                + request.getKey()
                + " skip flag: "
                + request.isSkipScaleBeforeMemCache());
      }
      return map;
    }

    final int w = map.getWidth(), h = map.getHeight();

    if (w <= dstW && h <= dstH) {
      return map;
    }

    final double ratio = (double) w / h;
    if (w > h) {
      dstH = (int) (dstW / ratio);
    } else {
      dstW = (int) (dstH * ratio);
    }

    if (dstW <= 0 || dstH <= 0) {
      return map;
    }

    final Bitmap scaled = Bitmap.createScaledBitmap(map, dstW, dstH, true);
    scaled.setDensity(imagesManager.getResources().getDisplayMetrics().densityDpi);
    return scaled;
  }
Exemple #2
0
  @Override
  public Void call() {
    if (imagesManager.debug) {
      Log.d(TAG, "Start image task");
    }
    try {

      if (!imagesManager.waitForPause()) {
        // interrupted
        cancel();
        return null;
      }

      final ImageResult result = request.readImage();
      if (result == null) {
        throw new IllegalStateException("Image is not returned!");
      }
      safeImageSet(result);

      finish();

    } catch (final MalformedURLException e) {

      Log.e(TAG, "Bad URL: " + request.url + ". Loading canceled.", e);
      error(e);

    } catch (final IOException e) {

      if (imagesManager.debug) {
        Log.e(TAG, "IO error for " + request.url + ": " + e.getMessage());
      }
      error(e);

    } catch (final Exception e) {

      Log.e(TAG, "Cannot load image " + request.url, e);
      error(e);

    } finally {

      final boolean removed = imagesManager.currentLoads.remove(request.getKey(), this);
      if (imagesManager.debug) {
        Log.d(TAG, "Current loaders count: " + imagesManager.currentLoads.size());
        if (!removed) {
          Log.w(TAG, "Incorrect loader in currents for " + request.getKey());
        }
      }
    }
    return null;
  }
Exemple #3
0
  // main thread
  public void removeTarget(final ImageConsumer consumer) {
    if (imagesManager.debug) {
      Log.d(TAG, "Cancel request: " + request.getKey() + "\nLoader: " + this);
    }

    consumer.onCancel(request.url);

    synchronized (targets) {
      targets.remove(consumer);

      if (targets.isEmpty()) {
        if (!future.cancel(true)) {
          if (imagesManager.debug) {
            Log.d(TAG, "Can't cancel task so let's try to remove loader manually");
          }
          imagesManager.currentLoads.remove(request.getKey(), this);
        }
      }
    }
  }
Exemple #4
0
  // worker thread
  private void safeImageSet(final ImageResult result) {
    if (imagesManager.debug) {
      Log.v(ImagesManager.TAG, "Post setting drawable for " + request.getKey());
    }

    synchronized (targets) {
      if (this.result != null) {
        throw new IllegalStateException("Result is already set");
      }
      memCacheImage(result);
      this.result = result;
    }

    post(
        new Runnable() {
          @Override
          public void run() {
            if (imagesManager.debug) {
              Log.v(TAG, "Set drawable for " + request.getKey());
            }

            final ArrayList<ImageConsumer> targets = ImageLoader.this.targets;
            final int count = targets.size();
            if (count > 0) {
              //noinspection ForLoopReplaceableByForEach
              for (int i = 0; i < count; i++) {
                final ImageConsumer imageHolder = targets.get(i);
                if (imagesManager.debug) {
                  Log.d(TAG, "Try to set " + imageHolder + " - " + request.getKey());
                }
                setToConsumer(imageHolder, result);
              }
            } else if (imagesManager.debug) {
              Log.w(TAG, "set drawable: have no targets in list");
            }
          }
        });
  }