简体   繁体   English

Android:基于DPI请求外部图像

[英]Android: Requesting external images based on DPI

When you've got bundled graphical assets you place them in drawable-hdpi , drawable-xhdpi etc. so the right image is loaded based on the device's DPI. 捆绑了图形资产后,可以将它们放置在drawable-hdpidrawable-xhdpi等中,以便根据设备的DPI加载正确的图像。

What's the norm when dealing with external images requested from the web? 处理网络请求的外部图像时有什么规范?

The solution I have in mind is to define the URL in resource folders using configuration qualifiers. 我想到的解决方案是使用配置限定符在资源文件夹中定义URL。 Eg. 例如。

values-ldpi
-- http://website.com/logo24x24.jpg
values-mdpi
-- http://website.com/logo32x32.jpg
values-hdpi
-- http://website.com/logo48x48.jpg
values-xhdpi
-- http://website.com/logo64x64.jpg

Then in the layout XML define the image as being 32x32dp. 然后在布局XML中将图像定义为32x32dp。

What's the common way of doing this? 常见的做法是什么? Doing it programmatically rather than using config qualifiers? 以编程方式而不是使用配置限定符进行?

If you have the ability to run a servlet (or any server side logic) it would be best to send the DPI as a parameter and make the decisions on the filenames on the server. 如果您具有运行Servlet(或任何服务器端逻辑)的能力,则最好将DPI作为参数发送并根据服务器上的文件名做出决定。 This will allow for easier changes without the need of updating the client and also will ease the process of writing a long list of values in the client. 这将允许更轻松的更改,而无需更新客户端,还将简化在客户端中写入一长串值的过程。

WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
                Display display = wm.getDefaultDisplay();
                int width = display.getWidth();  


                URL url = null;

                if (width < 360) {
                    url = new URL(start + "source/images/thumbnails/mid/" + urls.get(i));
                } else if (width < 1280) {
                    url = new URL(start + "source/images/thumbnails/big/" + urls.get(i));
                } else if (width < 1920){
                    url = new URL(start + "source/images/" + urls.get(i));
                } 

It is based on own opinion, u could use different sizes 根据自己的意见,您可以使用不同的大小

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

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