简体   繁体   English

如何缩放高分辨率位图以匹配设备屏幕的分辨率和密度?

[英]How Scale high resolution bitmaps to match device screen resolution and density?

I'm developing an android app with multiple fragment which every one contains multiple different bitmaps from other fragments. 我正在开发一个具有多个片段的android应用,每个片段都包含来自其他片段的多个不同的位图。 Due to the large size of the .apk file providing those bitmaps for every drawable density folder, i've decided to provide a single high resolution bitmap (a single high res bitmap for every different one from every different fragment) allocated in drawable-xxhdpi and scale it down at runtime depending on device screen resolution and/or density. 由于.apk文件的大小很大,因此它为每个可绘制密度文件夹提供了这些位图,因此我决定提供一个可绘制xxhdpi中分配的单个高分辨率位图(每个不同片段中每个不同分辨率的位图)并在运行时根据设备屏幕分辨率和/或密度将其按比例缩小。

I'm a bit confused about scaling bitmaps according to Andropid developer guide about Displaying Bitmaps Efficiently I can't understand if this code scale bitmaps to screen resolution in pixels, or screen density, or both. 我对根据有效地显示位图的 Andropid开发人员指南缩放位图感到有些困惑,我无法理解此代码是否将位图缩放为像素的屏幕分辨率,屏幕密度或两者。

As i can see in this code: 正如我在这段代码中看到的:

mImageView.setImageBitmap( decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100)); mImageView.setImageBitmap(encodeSampledBitmapFromResource(getResources(),R.id.myimage,100,100));

It creates a 100x100 image thumbnail, so i supose i have to pass screen resolution or screen density width and height in these parameters, but not sure... 它会创建一个100x100的图像缩略图,因此我想我必须在这些参数中传递屏幕分辨率或屏幕密度的宽度和高度,但不确定...

How can I match scaled bitmaps to screen configuration? 如何将缩放的位图与屏幕配置匹配?

Thanks to every answer. 感谢每个答案。

First of all, I would highly discourage not using the built in system of Android which automatically loads an image which appears in more than one drawable folder according to the screen size / density. 首先,我强烈不建议您不要使用内置的Android系统,该系统会根据屏幕尺寸/密度自动加载显示在多个可绘制文件夹中的图像。 It's true that doing this will increase the size of your APK, but loading a single bitmap and scaling it down / up during runtime will reduce performance in your app, and may very possibly lead to wasteful memory allocation issues and a wide variety of other bugs. 的确,这样做会增加APK的大小,但是在运行时加载单个位图并将其缩小/放大会降低应用程序的性能,并且很可能导致浪费的内存分配问题和其他各种错误。

If you insist on scaling bitmaps on your own, then the 1st problem I can see with your method, is that the parameter your passing (100, 100) is the size in pixels . 如果您坚持要自行缩放位图,那么我看到的第一个问题就是您传递的参数(100,100)是像素大小。 Devices of different density will have a different amount of pixels, and so 100x100 may appear very large on one device, but very small on a device with better density. 不同密度的设备将具有不同数量的像素,因此100x100在一个设备上可能看起来非常大,但在密度更好的设备上看起来很小。

The way to go here would be using device-independant-pixels (dip, or dp). 去这里的方法是使用与设备无关的像素(dip或dp)。 Devices with the same screen size will always have the same dp for their width & height. 屏幕尺寸相同的设备的宽度和高度将始终具有相同的dp。 Since this method you specified requires pixels, then a good way to convert between dp and pixels is shown here 由于您指定的此方法需要像素,因此此处显示在dp和像素之间转换的好方法

Resources r = getResources();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, amountInDp, r.getDisplayMetrics());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM