为什么程序读取图像文件后分辨率会变小? [英] Why do resolutions become smaller after a image file be read by program?
问题描述
我遇到一个问题:
我有不同大小的图像文件. (hdpi,xhdpi,xxhdpi,xxxhdpi)
I have images file having different sizes. (hdpi, xhdpi, xxhdpi, xxxhdpi)
当我在hdpi,xhdpi,xxhdpi设备中使用图像文件时,这很好.
When I used the image files in the in the hdpi,xhdpi,xxhdpi devices, it's good.
但是当设备为xxxhdpi时,图像文件的分辨率变小了.
But when the device is xxxhdpi, the resolutions of the image files was became smaller.
我不知道为什么会这样.
I don't know why it had happened.
例如:
我有2台设备,一个设备的分辨率为xxhdpi,另一个为xxxhdpi.
I have 2 devices, one's resolution is xxhdpi, another one is xxxhdpi.
我有2个图像文件,其中一个放在"drawable-xxhdpi"文件夹中,其分辨率为1080x1920.另一个放置在文件夹"drawable-xxxhdpi"中,其分辨率为1440x2560.
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.
xxhdpi的情况: 原始图片为1080x1920,读取的图片仍为1080x1920.
The case xxhdpi: The original picture is 1080x1920, and image that was read is 1080x1920 still.
xxxhdpi情况: 原始图片为1440x2560,但读取的图像变为1260x2240.
这是我的代码:
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.
然后我在xxhdpi设备中得到了结果:
And I got the result in xxhdpi device:
SettingCardFragment: Screen resolution: 1080x1776
SettingCardFragment: (Drawable)Background.png resolution: 1080x1920
SettingCardFragment: (Bitmap)Background.png resolution: 1080x1920
,然后在xxxhdpi设备中显示结果:
SettingCardFragment: Screen resolution: 1440x2392
SettingCardFragment: (Drawable)Background.png resolution: 1260x2240
SettingCardFragment: (Bitmap)Background.png resolution: 1260x2240
为什么分辨率从1440x2560变为1260x2240?
Why did the resolution became 1260x2240 from 1440x2560?
如果谁知道这一点,请帮助我〜
If who know this, help me please~
推荐答案
我想我明白了.您正在使用密度为3.5的设备(例如Nexus 6p).您可以使用此代码getResources().getDisplayMetrics().density
检查浓度.在这类设备中,使用xxxhdpi然后进行缩放.
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.
因此,如果我们进行缩放数学运算-image_size / xxxhdpi_density * device_density
,您将获得所看到的尺寸:
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
2560 / 4 * 3.5 = 2240
1440/4 * 3.5 = 1260
1440 / 4 * 3.5 = 1260
这篇关于为什么程序读取图像文件后分辨率会变小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!