为什么图像在下采样后旋转?

时间:2017-11-27 14:44:48

标签: android android-image

我正在使用以下代码下载图像

  private static Bitmap downsample(String path) {
    final BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
    bitmapOptions.inTargetDensity = 1;
    bitmapOptions.inSampleSize = 1;
    bitmapOptions.inDensity = 1;
    bitmapOptions.inTargetDensity = 1;

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);
    int imageHeight = options.outHeight;
    int imageWidth = options.outWidth;
    int reqWidth = 800, reqHeight = 800;
    final int newWidth = imageWidth / bitmapOptions.inSampleSize, newHeight =
        imageHeight / bitmapOptions.inSampleSize;
    if (newWidth > reqWidth || newHeight > reqHeight) {
      if (newWidth * reqHeight > newHeight * reqWidth) {
        // prefer width, as the width ratio is larger
        bitmapOptions.inTargetDensity = reqWidth;
        bitmapOptions.inDensity = newWidth;
      } else {
        // prefer height
        bitmapOptions.inTargetDensity = reqHeight;
        bitmapOptions.inDensity = newHeight;
      }
    }
    final Bitmap scaledBitmap = BitmapFactory.decodeFile(path, bitmapOptions);
    scaledBitmap.setDensity(Bitmap.DENSITY_NONE);
    return scaledBitmap;
  }

并以滑行加载

Glide.with(getActivity())
          .load(MainActivity.mProfile.getImg())
          .override(avatarSize, avatarSize)
          .centerCrop()
          .error(R.drawable.placeholder_photo)
          .into(imAvatar);

然而,如果原始高度大于宽度,则生成的位图旋转90度。

0 个答案:

没有答案