简体   繁体   English

来自网址的Android图片具有不同的大小

[英]Android images from url with different sizes

I prepared images for my android app. 我为我的android应用准备了图片。 I put images which have different sizes to drawable folders because of different device sizes. 由于设备大小不同,我将大小不同的图像放入了可绘制的文件夹。 Now i want to get these from url. 现在我想从url获得这些。 I want to get the image that is most appropriate for the device's size. 我想获取最适合设备尺寸的图像。 How should I do this? 我应该怎么做? Is there any suggest? 有什么建议吗?

From the api, you need to send URI to images of all sizes, then in android you need to download the image that suits the device's dpi value, the code for that is like this: 从api中,您需要将URI发送到所有大小的图像,然后在android中,您需要下载适合设备dpi值的图像,其代码如下所示:

You can get the density value of your device like this 您可以像这样获取设备的密度值

float density = Resources.getSystem().getDisplayMetrics().density * 160f;

or 要么

float density = context.getResources().getDisplayMetrics().density * 160f;

Then you do a series of if else statements and download the appropriate image from your sever. 然后,您执行一系列if else语句,并从服务器上下载适当的映像。

if (density == DisplayMetrics.DENSITY_HIGH) {
    return downloadLargeImage();
} else if (density == DisplayMetrics.DENSITY_MEDIUM) {
    return downloadMediumImage();
} else if (density == DisplayMetrics.DENSITY_LOW) {
    return downloadSmallImage();
} else {
    return downloadMediumImage();
}

There are more density values which you can run the if else loop on, I usually just take care of these 3 cases 您可以在if else循环上运行更多的密度值,我通常只照顾这3种情况

Or there is another way too, what you can do is send the devices density value to the server along with the get request for the image, and do this if else calculation on the server and return the appropriate image file. 或者还有另一种方法,您可以执行的操作是将设备密度值与图像的获取请求一起发送到服务器,然后在服务器上进行其他计算,然后返回适当的图像文件。

Both methods are effectively same, but the 2nd method is semantically wrong, your server shouldn't be the one deciding which image to send, but instead your device determining which image size to download 两种方法实际上是相同的,但是第二种方法在语义上是错误的,您的服务器不应由服务器决定要发送哪个图像,而是由设备确定要下载哪个图像大小

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

相关问题 Android:如何创建不同大小和密度的图像? - Android : How to create images for different sizes and densities? 在列表视图Android中加载不同大小的图像 - Loading different sizes of images in list view android Android图片可在不同屏幕尺寸下正确缩放 - Android images to scale properly in different screen sizes 如何为不同的屏幕尺寸android使用不同的图像? - How to use different images for different screen sizes android? 如何动态加载视图分页器android中不同大小的图像 - how to dynamically load the images with different sizes in view pager android Android - 项目中具有不同大小图像的 ListView 使 ListView 混蛋 - Android - ListView with different sizes images in items makes the ListView jerk 在Android上重复使用不同屏幕尺寸和密度的可绘制图像 - Reuse drawable images for different screen sizes and densities on Android 如何为不同的android屏幕尺寸使用矢量图像 - how to use vector images for different android screen sizes 使用毕加索从服务器加载不同屏幕尺寸的图像吗? - Load images for different screen sizes from the server using picasso? 图像,大小和不同的屏幕(碎片) - Images, sizes and different screens (fragmentation)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM