调整Android中的图像大小

时间:2014-05-08 19:00:20

标签: java android bitmap

我想调整我的图像大小(原始大小为1080p),但他们没有正确调整大小,我不知道为什么。图像有时没有合适的尺寸。在我的模拟器和我的旧800 * 480智能手机上工作正常但在我的nexus 4上有1280 * 768的东西看起来不对。读取正确的屏幕分辨率没有问题。我的调整大小程序只有一个错误。请帮帮我。

private float smaller;
smaller = height/1080;   //height is screenheight; in my case its 768 because of landscape

object.bitmap = Bitmap.createScaledBitmap(bitmap,(int)(smaller*bitmap.getWidth()) ,(int)(smaller*bitmap.getHeight()), true);

最后高度不会调整为768/1080 * bitmapheight,我也不知道为什么。

编辑:

这些是我的程序截图,显示图像高度不同

第一张图片:

imgur.com/STSgAOd,Wh3fVdX

第二

imgur.com/STSgAOd,Wh3fVdX#1

如您所见,图像在高度方面不相同。在我的模拟器和我的旧智能手机上,他们看起来正确。图像不应该触及底部,而是触及我的nexus 4。

也试过双倍:

private double factor;
factor = ((double)screenheight/(double)1080);
objekte.bitmap1 = Bitmap.createScaledBitmap(bitmap,(int)(factor*bitmap.getWidth()) ,(int)(factor*bitmap.getHeight()), true);

同样糟糕的结果

2 个答案:

答案 0 :(得分:0)

您认为高度需要调整最多(查看您的身高/ 1080)。我可能认为宽度必须调整最多。我用它来缩放它们:

//Calculate what scale is needed
double xFactor = (double)image.Width/(double)ScreenWidth;
double yFactor = (double)image.Height/(double)ScreenHeight;
double factor = xFactor;
if(yFactor>xFactor){
    factor = yFactor;
}

int imageWidth = Convert.ToInt32(im.Width / factor);
int imageHeight = Convert.ToInt32(im.Height / factor);

注意:这是用C#编写的。它需要一些改变才能工作。 注意2:这可以确保图像全屏显示(尽可能,因为它已被屏蔽)

答案 1 :(得分:0)

它是由整数除法引起的,你可以看到 -

public static void main(String[] args) {
    int height = 768;
    float smaller = (float) height / 1080; // <-- force float
    float test = height / 1080;            // <-- integer division, 
                                       // and assign the int result to the float.
    System.out.println("test: " + test);
    System.out.println("smaller: " + smaller);
}

输出

test: 0.0
smaller: 0.7111111