예제 #1
0
  /**
   * 加载图片
   *
   * @param path
   * @param imageView
   */
  public void loadImage(final String path, final ImageView imageView) {
    // 防止未初始化
    if (init < 1) return;
    // set tag
    imageView.setTag(path);
    // UI线程
    if (mHandler == null) {
      mHandler =
          new Handler() {
            @Override
            public void handleMessage(Message msg) {
              ImgBeanHolder holder = (ImgBeanHolder) msg.obj;
              ImageView imageView = holder.imageView;
              Bitmap bm = holder.bitmap;
              String path = holder.path;
              if (imageView.getTag().toString().equals(path)) {
                imageView.setImageBitmap(bm);
              }
            }
          };
    }

    Bitmap bm = getBitmapFromLruCache(path);
    if (bm != null) {
      ImgBeanHolder holder = new ImgBeanHolder();
      holder.bitmap = bm;
      holder.imageView = imageView;
      holder.path = path;
      Message message = Message.obtain();
      message.obj = holder;
      mHandler.sendMessage(message);
    } else {
      addTask(
          new Runnable() {
            @Override
            public void run() {

              ImageSize imageSize = getImageViewWidth(imageView);

              int reqWidth = imageSize.width;
              int reqHeight = imageSize.height;

              Bitmap bm = decodeSampledBitmapFromResource(path, reqWidth, reqHeight);
              addBitmapToLruCache(path, bm);
              ImgBeanHolder holder = new ImgBeanHolder();
              holder.bitmap = getBitmapFromLruCache(path);
              holder.imageView = imageView;
              holder.path = path;
              Message message = Message.obtain();
              message.obj = holder;
              // Log.e("TAG", "mHandler.sendMessage(message);");
              mHandler.sendMessage(message);
              mPoolSemaphore.release();
            }
          });
    }
  }