Exemplo n.º 1
0
  protected Bitmap considerExactScaleAndOrientaiton(
      Bitmap subsampledBitmap,
      ImageDecodingInfo decodingInfo,
      int rotation,
      boolean flipHorizontal) {
    Matrix m = new Matrix();
    // Scale to exact size if need
    ImageScaleType scaleType = decodingInfo.getImageScaleType();
    if (scaleType == ImageScaleType.EXACTLY || scaleType == ImageScaleType.EXACTLY_STRETCHED) {
      ImageSize srcSize =
          new ImageSize(subsampledBitmap.getWidth(), subsampledBitmap.getHeight(), rotation);
      float scale =
          ImageSizeUtils.computeImageScale(
              srcSize,
              decodingInfo.getTargetSize(),
              decodingInfo.getViewScaleType(),
              scaleType == ImageScaleType.EXACTLY_STRETCHED);
      if (Float.compare(scale, 1f) != 0) {
        m.setScale(scale, scale);

        if (loggingEnabled) {
          L.d(LOG_SCALE_IMAGE, srcSize, srcSize.scale(scale), scale, decodingInfo.getImageKey());
        }
      }
    }
    // Flip bitmap if need
    if (flipHorizontal) {
      m.postScale(-1, 1);

      if (loggingEnabled) L.d(LOG_FLIP_IMAGE, decodingInfo.getImageKey());
    }
    // Rotate bitmap if need
    if (rotation != 0) {
      m.postRotate(rotation);

      if (loggingEnabled) L.d(LOG_ROTATE_IMAGE, rotation, decodingInfo.getImageKey());
    }

    Bitmap finalBitmap =
        Bitmap.createBitmap(
            subsampledBitmap,
            0,
            0,
            subsampledBitmap.getWidth(),
            subsampledBitmap.getHeight(),
            m,
            true);
    if (finalBitmap != subsampledBitmap) {
      subsampledBitmap.recycle();
    }
    return finalBitmap;
  }