[英]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.