Exemplo n.º 1
0
  public void display(
      ImageView imageView, String uri, Bitmap loadingBitmap, CompressFormat compressFormat) {
    BitmapDisplayConfig displayConfig = displayConfigMap.get(String.valueOf(loadingBitmap));
    if (displayConfig == null) {
      displayConfig = globalConfig.getDefaultDisplayConfig();
      displayConfig.setLoadingBitmap(loadingBitmap);
      displayConfigMap.put(String.valueOf(loadingBitmap), displayConfig);
    }

    doDisplay(imageView, uri, displayConfig, compressFormat);
  }
Exemplo n.º 2
0
  public void display(
      ImageView imageView,
      String uri,
      int imageWidth,
      int imageHeight,
      CompressFormat compressFormat) {
    BitmapDisplayConfig displayConfig = displayConfigMap.get(imageWidth + "_" + imageHeight);
    if (displayConfig == null) {
      displayConfig = globalConfig.getDefaultDisplayConfig();
      displayConfig.setBitmapHeight(imageHeight);
      displayConfig.setBitmapWidth(imageWidth);
      displayConfigMap.put(imageWidth + "_" + imageHeight, displayConfig);
    }

    doDisplay(imageView, uri, displayConfig, compressFormat);
  }
Exemplo n.º 3
0
  private void doDisplay(
      ImageView imageView,
      String uri,
      BitmapDisplayConfig displayConfig,
      CompressFormat compressFormat) {
    if (TextUtils.isEmpty(uri) || imageView == null) {
      return;
    }

    if (displayConfig == null) {
      displayConfig = globalConfig.getDefaultDisplayConfig();
    }

    Bitmap bitmap = null;

    bitmap = globalConfig.getBitmapCache().getBitmapFromMemCache(uri);

    if (bitmap != null) {
      imageView.setImageBitmap(bitmap);

    } else if (!bitmapLoadTaskExist(imageView, uri)) {

      final BitmapLoadTask loadTask = new BitmapLoadTask(imageView, displayConfig);
      // set loading image
      final AsyncBitmapDrawable asyncBitmapDrawable =
          new AsyncBitmapDrawable(
              context.getResources(), displayConfig.getLoadingBitmap(), loadTask);
      imageView.setImageDrawable(asyncBitmapDrawable);

      // load bitmap from uri or diskCache
      loadTask.executeOnExecutor(globalConfig.getBitmapLoadExecutor(), uri, compressFormat);
    }
  }
Exemplo n.º 4
0
  public <T extends View> void display(
      T container, String uri, BitmapDisplayConfig displayConfig, BitmapLoadCallBack<T> callBack) {
    if (container == null) {
      return;
    }

    container.clearAnimation();

    if (callBack == null) {
      callBack = new SimpleBitmapLoadCallBack<T>();
    }

    if (displayConfig == null || displayConfig == defaultDisplayConfig) {
      displayConfig = defaultDisplayConfig.cloneNew();
    }

    // Optimize Max Size
    BitmapSize size = displayConfig.getBitmapMaxSize();
    displayConfig.setBitmapMaxSize(
        BitmapCommonUtils.optimizeMaxSizeByView(container, size.getWidth(), size.getHeight()));

    callBack.onPreLoad(container, uri, displayConfig);

    if (TextUtils.isEmpty(uri)) {
      callBack.onLoadFailed(container, uri, displayConfig.getLoadFailedDrawable());
      return;
    }

    Bitmap bitmap = globalConfig.getBitmapCache().getBitmapFromMemCache(uri, displayConfig);

    if (bitmap != null) {
      callBack.onLoadStarted(container, uri, displayConfig);
      callBack.onLoadCompleted(container, uri, bitmap, displayConfig, BitmapLoadFrom.MEMORY_CACHE);
    } else if (!bitmapLoadTaskExist(container, uri, callBack)) {

      final BitmapLoadTask<T> loadTask =
          new BitmapLoadTask<T>(container, uri, displayConfig, callBack);
      // set loading image
      final AsyncDrawable<T> asyncDrawable =
          new AsyncDrawable<T>(displayConfig.getLoadingDrawable(), loadTask);
      callBack.setDrawable(container, asyncDrawable);

      // load bitmap from uri or diskCache
      loadTask.executeOnExecutor(globalConfig.getBitmapLoadExecutor());
    }
  }
 @Override
 public void onLoadCompleted(
     T container, String uri, Bitmap bitmap, BitmapDisplayConfig config, BitmapLoadFrom from) {
   this.setBitmap(container, bitmap);
   Animation animation = config.getAnimation();
   if (animation != null) {
     animationDisplay(container, animation);
   }
 }
Exemplo n.º 6
0
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.image);

    ViewUtils.inject(this);

    String imgUrl = getIntent().getStringExtra("url");

    bitmapUtils = BitmapFragment.bitmapUtils;
    if (bitmapUtils == null) {
      bitmapUtils = BitmapHelp.getBitmapUtils(this.getApplicationContext());
    }

    bigPicDisplayConfig = new BitmapDisplayConfig();
    // bigPicDisplayConfig.setShowOriginal(true); // 显示原始图片,不压缩, 尽量不要使用, 图片太大时容易OOM。
    bigPicDisplayConfig.setBitmapConfig(Bitmap.Config.RGB_565);
    bigPicDisplayConfig.setBitmapMaxSize(BitmapCommonUtils.getScreenSize(this));

    BitmapLoadCallBack<ImageView> callback =
        new SimpleBitmapLoadCallBack<ImageView>() {
          @Override
          public void onLoadStarted(ImageView container, String uri, BitmapDisplayConfig config) {
            super.onLoadStarted(container, uri, config);
            Toast.makeText(getApplicationContext(), uri, 300).show();
          }

          @Override
          public void onLoadCompleted(
              ImageView container,
              String url,
              Bitmap bitmap,
              BitmapDisplayConfig config,
              BitmapLoadFrom from) {
            super.onLoadCompleted(container, url, bitmap, config, from);
            Toast.makeText(
                    getApplicationContext(), bitmap.getWidth() + "*" + bitmap.getHeight(), 300)
                .show();
          }
        };
    callback.setBitmapSetter(BitmapCommonUtils.sDefaultImageViewSetter);

    bitmapUtils.display(bigImage, imgUrl, bigPicDisplayConfig, callback);
  }
Exemplo n.º 7
0
 @Override
 protected void onPostExecute(Bitmap bitmap) {
   final T container = this.getTargetContainer();
   if (container != null) {
     if (bitmap != null) {
       callBack.onLoadCompleted(container, this.uri, bitmap, displayConfig, from);
     } else {
       callBack.onLoadFailed(container, this.uri, displayConfig.getLoadFailedDrawable());
     }
   }
 }
Exemplo n.º 8
0
  /**
   * Get the bitmap from disk cache.
   *
   * @param uri
   * @param config
   * @return
   */
  public Bitmap getBitmapFromDiskCache(String uri, BitmapDisplayConfig config) {
    if (uri == null || !globalConfig.isDiskCacheEnabled()) return null;
    synchronized (mDiskCacheLock) {
      while (!isDiskCacheReadied) {
        try {
          mDiskCacheLock.wait();
        } catch (Throwable e) {
        }
      }
    }
    if (mDiskLruCache != null) {
      LruDiskCache.Snapshot snapshot = null;
      try {
        snapshot = mDiskLruCache.get(uri);
        if (snapshot != null) {
          Bitmap bitmap = null;
          if (config == null || config.isShowOriginal()) {
            bitmap =
                BitmapDecoder.decodeFileDescriptor(
                    snapshot.getInputStream(DISK_CACHE_INDEX).getFD());
          } else {
            bitmap =
                BitmapDecoder.decodeSampledBitmapFromDescriptor(
                    snapshot.getInputStream(DISK_CACHE_INDEX).getFD(),
                    config.getBitmapMaxSize(),
                    config.getBitmapConfig());
          }

          bitmap = rotateBitmapIfNeeded(uri, config, bitmap);
          addBitmapToMemoryCache(uri, config, bitmap, mDiskLruCache.getExpiryTimestamp(uri));
          return bitmap;
        }
      } catch (Throwable e) {
        LogUtils.e(e.getMessage(), e);
      } finally {
        IOUtils.closeQuietly(snapshot);
      }
    }
    return null;
  }
Exemplo n.º 9
0
 private Bitmap decodeBitmapMeta(BitmapMeta bitmapMeta, BitmapDisplayConfig config)
     throws IOException {
   if (bitmapMeta == null) return null;
   Bitmap bitmap = null;
   if (bitmapMeta.inputStream != null) {
     if (config == null || config.isShowOriginal()) {
       bitmap = BitmapDecoder.decodeFileDescriptor(bitmapMeta.inputStream.getFD());
     } else {
       bitmap =
           BitmapDecoder.decodeSampledBitmapFromDescriptor(
               bitmapMeta.inputStream.getFD(),
               config.getBitmapMaxSize(),
               config.getBitmapConfig());
     }
   } else if (bitmapMeta.data != null) {
     if (config == null || config.isShowOriginal()) {
       bitmap = BitmapDecoder.decodeByteArray(bitmapMeta.data);
     } else {
       bitmap =
           BitmapDecoder.decodeSampledBitmapFromByteArray(
               bitmapMeta.data, config.getBitmapMaxSize(), config.getBitmapConfig());
     }
   }
   return bitmap;
 }
Exemplo n.º 10
0
    // 获取图片任务完成
    @Override
    protected void onPostExecute(Bitmap bitmap) {
      if (isCancelled() || pauseTask) {
        bitmap = null;
      }

      final ImageView imageView = getAttachedImageView();
      if (bitmap != null && imageView != null) { // 显示图片
        globalConfig.getImageLoadCallBack().loadCompleted(imageView, bitmap, displayConfig);
      } else if (bitmap == null && imageView != null) { // 显示获取错误图片
        globalConfig
            .getImageLoadCallBack()
            .loadFailed(imageView, displayConfig.getLoadFailedBitmap());
      }
    }
Exemplo n.º 11
0
  public void display(
      ImageView imageView,
      String uri,
      int imageWidth,
      int imageHeight,
      Bitmap loadingBitmap,
      Bitmap loadFailedBitmap,
      CompressFormat compressFormat) {
    BitmapDisplayConfig displayConfig =
        displayConfigMap.get(
            imageWidth
                + "_"
                + imageHeight
                + "_"
                + String.valueOf(loadingBitmap)
                + "_"
                + String.valueOf(loadFailedBitmap));
    if (displayConfig == null) {
      displayConfig = globalConfig.getDefaultDisplayConfig();
      displayConfig.setBitmapHeight(imageHeight);
      displayConfig.setBitmapWidth(imageWidth);
      displayConfig.setLoadingBitmap(loadingBitmap);
      displayConfig.setLoadFailedBitmap(loadFailedBitmap);
      displayConfigMap.put(
          imageWidth
              + "_"
              + imageHeight
              + "_"
              + String.valueOf(loadingBitmap)
              + "_"
              + String.valueOf(loadFailedBitmap),
          displayConfig);
    }

    doDisplay(imageView, uri, displayConfig, compressFormat);
  }
Exemplo n.º 12
0
 private Bitmap rotateBitmapIfNeeded(String uri, BitmapDisplayConfig config, Bitmap bitmap) {
   Bitmap result = bitmap;
   if (config != null && config.isAutoRotation()) {
     File bitmapFile = this.getBitmapFileFromDiskCache(uri);
     if (bitmapFile != null && bitmapFile.exists()) {
       ExifInterface exif = null;
       try {
         exif = new ExifInterface(bitmapFile.getPath());
       } catch (Throwable e) {
         return result;
       }
       int orientation =
           exif.getAttributeInt(
               ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);
       int angle = 0;
       switch (orientation) {
         case ExifInterface.ORIENTATION_ROTATE_90:
           angle = 90;
           break;
         case ExifInterface.ORIENTATION_ROTATE_180:
           angle = 180;
           break;
         case ExifInterface.ORIENTATION_ROTATE_270:
           angle = 270;
           break;
         default:
           angle = 0;
           break;
       }
       if (angle != 0) {
         Matrix m = new Matrix();
         m.postRotate(angle);
         result =
             Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
         bitmap.recycle();
         bitmap = null;
       }
     }
   }
   return result;
 }
Exemplo n.º 13
0
  public Bitmap downloadBitmap(
      String uri, BitmapDisplayConfig config, final BitmapUtils.BitmapLoadTask<?> task) {

    BitmapMeta bitmapMeta = new BitmapMeta();

    OutputStream outputStream = null;
    LruDiskCache.Snapshot snapshot = null;

    try {
      Bitmap bitmap = null;
      // try download to disk
      if (globalConfig.isDiskCacheEnabled()) {
        synchronized (mDiskCacheLock) {
          // Wait for disk cache to initialize
          while (!isDiskCacheReadied) {
            try {
              mDiskCacheLock.wait();
            } catch (Throwable e) {
            }
          }
        }

        if (mDiskLruCache != null) {
          try {
            snapshot = mDiskLruCache.get(uri);
            if (snapshot == null) {
              LruDiskCache.Editor editor = mDiskLruCache.edit(uri);
              if (editor != null) {
                outputStream = editor.newOutputStream(DISK_CACHE_INDEX);
                bitmapMeta.expiryTimestamp =
                    globalConfig.getDownloader().downloadToStream(uri, outputStream, task);
                if (bitmapMeta.expiryTimestamp < 0) {
                  editor.abort();
                  return null;
                } else {
                  editor.setEntryExpiryTimestamp(bitmapMeta.expiryTimestamp);
                  editor.commit();
                }
                snapshot = mDiskLruCache.get(uri);
              }
            }
            if (snapshot != null) {
              bitmapMeta.inputStream = snapshot.getInputStream(DISK_CACHE_INDEX);
              bitmap = decodeBitmapMeta(bitmapMeta, config);
              if (bitmap == null) {
                bitmapMeta.inputStream = null;
                mDiskLruCache.remove(uri);
              }
            }
          } catch (Throwable e) {
            LogUtils.e(e.getMessage(), e);
          }
        }
      }

      // try download to memory stream
      if (bitmap == null) {
        outputStream = new ByteArrayOutputStream();
        bitmapMeta.expiryTimestamp =
            globalConfig.getDownloader().downloadToStream(uri, outputStream, task);
        if (bitmapMeta.expiryTimestamp < 0) {
          return null;
        } else {
          bitmapMeta.data = ((ByteArrayOutputStream) outputStream).toByteArray();
          bitmap = decodeBitmapMeta(bitmapMeta, config);
        }
      }

      if (bitmap != null) {
        bitmap = rotateBitmapIfNeeded(uri, config, bitmap);
        if (config != null && config.getImageFactory() != null) {
          bitmap = config.getImageFactory().createBitmap(bitmap);
        }
        addBitmapToMemoryCache(uri, config, bitmap, bitmapMeta.expiryTimestamp);
      }
      return bitmap;
    } catch (Throwable e) {
      LogUtils.e(e.getMessage(), e);
    } finally {
      IOUtils.closeQuietly(outputStream);
      IOUtils.closeQuietly(snapshot);
    }

    return null;
  }
  private void initView() {
    initHomeWorkDetailsData();

    bitmapUtils = new BitmapUtils(this);
    bigPicDisplayConfig = new BitmapDisplayConfig();
    bigPicDisplayConfig.setLoadingDrawable(getResources().getDrawable(R.drawable.defaulticon));
    bigPicDisplayConfig.setLoadFailedDrawable(getResources().getDrawable(R.drawable.msg_point));
    bigPicDisplayConfig.setBitmapConfig(Bitmap.Config.RGB_565);
    bigPicDisplayConfig.setBitmapMaxSize(BitmapCommonUtils.getScreenSize(mContext));

    bitmapUtils.display(
        imghead, listHomeWorDetailsData.get(0).get("imghead").toString(), bigPicDisplayConfig);
    txtusername.setText(listHomeWorDetailsData.get(0).get("username").toString());
    txtgrade.setText(listHomeWorDetailsData.get(0).get("grade").toString());
    txttime.setText(listHomeWorDetailsData.get(0).get("time").toString());
    txtquestionname.setText(listHomeWorDetailsData.get(0).get("questionname").toString());
    txtquestiondetail.setText(listHomeWorDetailsData.get(0).get("questiondetail").toString());

    imgquestionpic1.setVisibility(View.GONE);
    imgquestionpic2.setVisibility(View.GONE);
    imgquestionpic3.setVisibility(View.GONE);
    imgquestionpic4.setVisibility(View.GONE);

    Map<Integer, Object> map =
        (Map<Integer, Object>) listHomeWorDetailsData.get(0).get("questionpic");

    for (Integer key : map.keySet()) {
      System.out.println("_____" + key + "____" + map.get(key).toString());

      switch (key) {
        case 0:
          imgquestionpic1.setVisibility(View.VISIBLE);
          bitmapUtils.display(imgquestionpic1, map.get(key).toString(), bigPicDisplayConfig);

          break;
        case 1:
          imgquestionpic2.setVisibility(View.VISIBLE);
          bitmapUtils.display(imgquestionpic2, map.get(key).toString(), bigPicDisplayConfig);

          break;
        case 2:
          imgquestionpic3.setVisibility(View.VISIBLE);
          bitmapUtils.display(imgquestionpic3, map.get(key).toString(), bigPicDisplayConfig);

          break;
        case 3:
          imgquestionpic4.setVisibility(View.VISIBLE);
          bitmapUtils.display(imgquestionpic4, map.get(key).toString(), bigPicDisplayConfig);

          break;

        default:
          imgquestionpic1.setVisibility(View.GONE);
          imgquestionpic2.setVisibility(View.GONE);
          imgquestionpic3.setVisibility(View.GONE);
          imgquestionpic4.setVisibility(View.GONE);
          break;
      }
    }

    HomeWorkDetails_Adapter homewordetails_adapter =
        new HomeWorkDetails_Adapter(
            (List<Map<String, Object>>) listHomeWorDetailsData.get(0).get("answerteachers"),
            this,
            listhomeworkdetails);

    listhomeworkdetails.setAdapter(homewordetails_adapter);
  }
Exemplo n.º 15
0
 public BitmapUtils configDefaultLoadFailedImage(Bitmap bitmap) {
   defaultDisplayConfig.setLoadFailedDrawable(new BitmapDrawable(context.getResources(), bitmap));
   return this;
 }
Exemplo n.º 16
0
 public BitmapUtils configDefaultBitmapMaxSize(int maxWidth, int maxHeight) {
   defaultDisplayConfig.setBitmapMaxSize(new BitmapSize(maxWidth, maxHeight));
   return this;
 }
Exemplo n.º 17
0
 public BitmapUtils configDefaultLoadingImage(int resId) {
   defaultDisplayConfig.setLoadingDrawable(context.getResources().getDrawable(resId));
   return this;
 }
Exemplo n.º 18
0
 public BitmapUtils configDefaultBitmapMaxSize(BitmapSize maxSize) {
   defaultDisplayConfig.setBitmapMaxSize(maxSize);
   return this;
 }
Exemplo n.º 19
0
 public BitmapUtils configDefaultImageLoadAnimation(Animation animation) {
   defaultDisplayConfig.setAnimation(animation);
   return this;
 }
Exemplo n.º 20
0
 public BitmapUtils configDefaultBitmapConfig(Bitmap.Config config) {
   defaultDisplayConfig.setBitmapConfig(config);
   return this;
 }
Exemplo n.º 21
0
 public BitmapUtils configDefaultShowOriginal(boolean showOriginal) {
   defaultDisplayConfig.setShowOriginal(showOriginal);
   return this;
 }
Exemplo n.º 22
0
 public BitmapUtils configDefaultAutoRotation(boolean autoRotation) {
   defaultDisplayConfig.setAutoRotation(autoRotation);
   return this;
 }
Exemplo n.º 23
0
 private MemoryCacheKey(String uri, BitmapDisplayConfig config) {
   this.uri = uri;
   this.subKey = config == null ? null : config.toString();
 }
Exemplo n.º 24
0
 public BitmapUtils configDefaultLoadingImage(Drawable drawable) {
   defaultDisplayConfig.setLoadingDrawable(drawable);
   return this;
 }