2014-06-24 60 views
5

我使用通用图像加载程序加载图像,并且我想缩放它,以便宽度是屏幕的宽度并且相应地缩放高度,这意味着之前我加载图像我知道我想要的宽度,但不是高度。所以当我加载图像时,我想要获取图像的高度和宽度,并使用它根据屏幕宽度进行缩放。我用它来做到这一点的代码是这样的:使用通用图像加载程序加载后调整位图大小

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 

什么是使用通用图像装载机,甚至更好的调整位图的最好办法,是没有办法,我只能指定宽度的方式和位图进行缩放正确地基于它的比例?

+0

http://stackoverflow.com/questions/4837715/how-to-resize -a-bitmap-in-android – kupsef

+0

我建议你使用[毕加索图书馆](http://square.github.io/picasso/)。它允许非常简单的图像加载和操作,同时仍然灵活。 –

+0

我的解决方案基于@ nitesh-goel代码和@zhaoyuanjie'DisplayImageOptions',但是具有'ImageScaleType.EXACTLY_STRETCHED'。请注意,如果您将图片放大太多,图片看起来会模糊不清。 – Leukipp

回答

1

使用可以使用

// 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 
    } 
}); 
0

我没有看过你的代码,但确定的高度计算是很容易的。我们想要的是保持图像的纵横比不变。因此,首先计算ASPECT_RATIO即

imageHeight/imageWidth = aspt_ratio; 

然后借此ASPECT_RATIO并与当前屏幕的宽度乘以这个高度。

scaledHeight = aspt_ratio*screen_width; 

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

0

这将工作,因为你需要

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); 
相关问题