Resize image on QVGA screens

烂漫一生 提交于 2019-12-25 07:19:34

问题


I have an app with a login screen. On that screen, I have de company logo.

First I made one layout folder and one drawable folder. I tested and all worked ok.

Then I have added the 4 layout folders (normal, large, small and xlarge) and the 4 drawable folders (hdpi, ldpi, mdpi, xhdpi). I just copied my logo.png and my main_layout.xml to each folder (thats wrong, but was just for test). Off course, when I test the app on the each differents screen size emulators, It shows the logo image in wrong position or deformed (Some times bigger or smaller, other times in bad position, etc). And thats is right, because I didn't have created the different png files and I didn't have adapted the layouts for diferent sizes.

So, the next step it was talk with the desing area and ask for the 4 png files on each density and check the "4 layouts".

Once I have the 4 png files and the layouts fixed, I have tested again the app using the 4 emulator (one for normal screens, other for large screens, other for extra large screens and one for small screens). And still not work like I want.

On some cases, the logo.png it shows bigger that what I want. In others It show smaller. I don't understand what I have to do. I use wrap_content, I create the 4 drawable folders (hdpi, ldpi, mdpi, xhdpi), I have 4 logo.png files with different sizes.

But when I see the image on a QVGA Emulator (for example), it show me a huge image.

If the different layouts is for the screen size, and the different pngs is for the different density, What I have to do to use small or bigger image (pngs)??

thanks and sorry for my poor english


回答1:


This type of problem usually is very case specific.

But the general approach is to have ONE and only ONE layout. Any dimension values inside this layout, put in a dimens.xml file inside the /values/ folder. Create the 4 .png (ldpi, mdpi, hdpi, xhdpi) and make sure their proportion is correct! (Double check the Android developers website to see the correct proportions.

And after you tested with this setup, IF there's something not aligning you'll create a dimens.xml for that screen size (small, large, etc) and only add the dimension you actually want to change.

Do not add all dimensions or all layouts before testing. Is usually simpler to start with the basic and add more resources as needed.




回答2:


I was having a similar issue, which may or may not help you. I was trying to read an mjpeg stream at a certain size, but it never perfectly fit the window I sized it for. The issue came down to the fact that pixel densities of many devices are not exactly equal to what the OS reports. For example, the OS might report mdpi to be 160 dpi, but the actual dpi of the device is 164. Therefore, if you wish to use 1 very large *.png and resize it at runtime, do not use the "standard" scaling factors (i.e. 1, 2, 4, etc) and instead use

float factor = getResources().getDisplayMetrics().density;

//To get width/height of the View 
int width = view.getWidth(); //a fixed width would be preferred over this
int realwidth = (int) Math.round(width/factor);

Note that resizing is done automatically, but sometimes you don't get the desired result if you let the OS handle it.

This isn't exactly what you asked, but if you do decide to go the route of having only one drawable, this would hopefully avoid some headaches.



来源:https://stackoverflow.com/questions/15881633/resize-image-on-qvga-screens

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!