Why do resolutions become smaller after a image file be read by program?

早过忘川 提交于 2019-12-24 06:47:49

问题


I meet a problem:

I have images file having different sizes. (hdpi, xhdpi, xxhdpi, xxxhdpi)

When I used the image files in the in the hdpi,xhdpi,xxhdpi devices, it's good.

But when the device is xxxhdpi, the resolutions of the image files was became smaller.

I don't know why it had happened.

For example:

I have 2 devices, one's resolution is xxhdpi, another one is xxxhdpi.

And I have 2 image files, one is put in the folder "drawable-xxhdpi", it's resolution is 1080x1920. Another one is put in the folder "drawable-xxxhdpi", it's resolution is 1440x2560.

When I read the image files, problem was appeared.

The case xxhdpi: The original picture is 1080x1920, and image that was read is 1080x1920 still.

The case xxxhdpi: The original picture is 1440x2560, but the image that was read became 1260x2240.

This is my code:

DisplayMetrics metrics = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
            Log.i("SettingCardFragment", String.format("%37s%dx%d",
                    "Screen resolution: ", metrics.widthPixels, metrics.heightPixels));

            Drawable drawable = ContextCompat.getDrawable(getActivity(),R.drawable.background);
            Log.i("SettingCardFragment", String.format("%37s%dx%d",
                    "(Drawable)Background.png resolution: ", drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()));

            Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.background);
            Log.i("SettingCardFragment", String.format("%37s%dx%d",
                    "(Bitmap)Background.png resolution: ", bitmap.getWidth(), bitmap.getHeight()));

I use the code to check the resolution of the picture that was read.

And I got the result in xxhdpi device:

SettingCardFragment:                   Screen resolution: 1080x1776
SettingCardFragment: (Drawable)Background.png resolution: 1080x1920
SettingCardFragment:   (Bitmap)Background.png resolution: 1080x1920

And the result in xxxhdpi device:

SettingCardFragment:                   Screen resolution: 1440x2392
SettingCardFragment: (Drawable)Background.png resolution: 1260x2240
SettingCardFragment:   (Bitmap)Background.png resolution: 1260x2240

Why did the resolution became 1260x2240 from 1440x2560?

If who know this, help me please~


回答1:


I think I figured it out. You are using a device with density 3.5 (like the Nexus 6p). You can check the density with this code getResources().getDisplayMetrics().density. In these kind of devices xxxhdpi is used and then scaled.

So if we do the scaling math - image_size / xxxhdpi_density * device_density you get the dimensions you are seeing:

2560 / 4 * 3.5 = 2240

1440 / 4 * 3.5 = 1260




回答2:


Bitmap bitmap = BitmapFactory.decodeResource(getResources()

decodeResource will adapt the resoluten for your device.

If you want the original resolution then first open an InputStream for the resource and then use BitmapFactory.decodeFromStream().



来源:https://stackoverflow.com/questions/43894183/why-do-resolutions-become-smaller-after-a-image-file-be-read-by-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!