调整android中的图像大小

时间:2014-05-08 20:43:04

标签: java android

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

private Bitmap bitmap,bitmap1;
private float factor;

WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics displaymetrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(displaymetrics);
    width = displaymetrics.widthPixels;
    height = displaymetrics.heightPixels;

factor = (float)height/1080; 

Int bitmapheight,bitmapwidth;
bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.picture),(int)(factor*BitmapFactory.decodeResource(getResources(), R.drawable.picture).getwidth() ,(int)(factor*BitmapFactory.decodeResource(getResources(), R.drawable.picture).getHeight(), true);

canvas.drawBitmap(bitmap,.....

最后我的nexus的高度未调整为768/1080 * bitmapheight,我不知道为什么。注意其他一切都有效。 Mathmatics表示每部手机的高度应该相同。

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

第一张图片:

enter image description here

第二

enter image description here

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

1 个答案:

答案 0 :(得分:0)

对于任何有兴趣的人,为什么它不起作用。最后我发现了。

现在

BitmapFactory.decodeResource(getResources(), R.drawable.picture)

不仅可以加载图片,还可以根据您的分辨率进行缩放(不仅仅是在某些分辨率上),这就是为什么它让我感到困惑。我以为它只是加载该死的图片。现在该如何解决这个问题:

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inScaled = false;

BitmapFactory.decodeResource(getResources(), R.drawable.picture,opts)
相关问题