【问题标题】:Resizing Bitmap after Loading it with Universal Image Loader使用通用图像加载器加载位图后调整位图大小
【发布时间】:2014-08-15 05:50:00
【问题描述】:

我正在使用 Universal Image Loader 加载图像,我想对其进行缩放,以使宽度为屏幕的宽度,并且相应地缩放高度,这意味着在我加载图像之前,我知道宽度我想要,但不是高度。因此,当我加载图像时,我想获取图像的高度和宽度,并根据屏幕的宽度使用它来缩放它。我用来做这个的代码是这样的:

try {
    display.getSize(size);
    scaledWidth = size.x;
} catch (java.lang.NoSuchMethodError ignore) {
    scaledWidth = display.getWidth();
}

String filePath = "file://" + getBaseContext().getFilesDir().getPath().toString() + "/" + imagePath + ".png";
Bitmap bitmap = imageLoader.loadImageSync(filePath);
int height = bitmap.getHeight();
int width = bitmap.getWidth();
scaledHeight =  (int) (((scaledWidth * 1.0) / width) * height);
//Code to resize Bitmap using scaledWidth and scaledHeight

使用通用图像加载器调整位图大小的最佳方法是什么,或者甚至更好,有没有一种方法可以只指定宽度并根据其比例正确缩放位图?

【问题讨论】:

  • 我建议您使用Picasso library。它允许非常轻松的图像加载和操作,同时仍然很灵活。
  • 我的解决方案是基于@nitesh-goel 代码和@zhaoyuanjie DisplayImageOptions 但使用ImageScaleType.EXACTLY_STRETCHED。请注意,如果放大太多,图像会显得模糊。

标签: android bitmap universal-image-loader


【解决方案1】:

这将根据您的需要工作

scaledWidth = size.x;

String filePath = "file://" + getBaseContext().getFilesDir().getPath().toString() + "/" + imagePath + ".png";

    android.graphics.BitmapFactory.Options options= new Options();
        options.inJustDecodeBounds=true;
 //Just gets image size without allocating memory
BitmapFactory.decodeFile(filePath, options);


int height = options.outHeight;
int width = bitmap.outWidth;
scaledHeight =  (int) (((scaledWidth * 1.0) / width) * height);


ImageSize targetSize = new ImageSize(scaledWidth, scaledHeight); 
Bitmap bmp = imageLoader.loadImageSync(imageUri, targetSize, displayOptions);

【讨论】:

    【解决方案2】:

    你不需要自己做。考虑使用 ImageScaleType.EXACTLY

    new DisplayImageOptions.Builder().imageScaleType(ImageScaleType.EXACTLY)
    

    https://github.com/nostra13/Android-Universal-Image-Loader/blob/master/library/src/com/nostra13/universalimageloader/core/assist/ImageScaleType.java

    【讨论】:

      【解决方案3】:

      我没有阅读您的代码,但确定高度的计算非常简单。我们想要的是保持图像的纵横比不变。所以首先计算 aspect_ratio 即

      imageHeight/imageWidth = aspt_ratio;
      

      然后将这个 aspect_ratio 与当前屏幕宽度相乘得到高度。

      scaledHeight = aspt_ratio*screen_width;
      

      因为我们知道图像的缩放宽度将始终等于屏幕宽度,根据您的要求。

      【讨论】:

        【解决方案4】:

        使用可以使用

        // Load image, decode it to Bitmap and return Bitmap to callback
        ImageSize targetSize = new ImageSize(120, 80); // result Bitmap will be fit to this size
        imageLoader.loadImage(imageUri, targetSize, displayOptions, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                // Do whatever you want with Bitmap
            }
        });
        

        【讨论】:

          猜你喜欢
          • 2015-12-29
          • 1970-01-01
          • 2013-07-14
          • 1970-01-01
          • 2017-04-07
          • 1970-01-01
          • 1970-01-01
          • 2013-08-14
          • 1970-01-01
          相关资源
          最近更新 更多