Android align ImageViews in different resolutions/

2019-07-26 03:24发布

I have received a PSD layout from my client, and I it contains some fixed background images, and below those images there should be some Buttons, TextViews, etc.

The problem is, that for example the Game Over screen has a background, then there should be image with "You win" or "You lose", above the background in specific offset from top left corner. If I design the interface for 480x800 phones, and specify the offset in pixels, everything's OK. But if I deploy it into 320x480 phone, it of course doesn't fit, because pixel offsets are different now and the background is scaled.

Using dip units doesn't help, because the smaller phones have physically smaller screens too.

here is a sketch of what I'd like to do. All and it should look similar on phones from 240x320 do 540x960. Is it possible to somehow do this in single XML layout file or do I have to hand-code offsets for every resolution that comes to my mind?

The main problem is that on the picture, the red frame around "you win" is a place where it fits into the orange background, because that's the way the graphics is designed. And when the app is run on phone with smaller resolution, I don't exactly know how to align the "you win" picture on the orange one. enter image description here.

3条回答
淡お忘
2楼-- · 2019-07-26 03:48

I managed the multi-resolution by getting offset of screen size.

DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        height = displaymetrics.heightPixels;
        width = displaymetrics.widthPixels;

        getDensityName(displaymetrics.density);

        int dens=displaymetrics.densityDpi;
        double wi=(double)width;
        double hi=(double)height;
        double x = Math.pow(wi,2);
        double y = Math.pow(hi,2);
        double screenInches = Math.sqrt(x+y)/(double)dens;

        if(screenInches<3.7)
        {
            offset = 0.7f;  
        }
        else if(screenInches<4.2)
        {
            offset = 1.1f;
        }
        else if(screenInches<5.0)
        {
            offset = 1.5f;
        }
        else if(screenInches<6.0)
        {
            offset = 1.7f;
        }
        else
        {
            offset = 2f;
        }

I hope this may help others.Thanks!

查看更多
唯我独甜
3楼-- · 2019-07-26 03:52

@Axarydax if you want want your application to support different screen sizes you will have design them . I had the same issue placed images in drawable-hdpi for high density device , in drawable-ldpi for low density device and in drawable-mdpi for medium density device and then

what i did that i created three diffrent layout folders like for 320x480 layout folder

for screen size 240x320

layout-port-320x240

for screen size 480x800

layout-port-480x800

and created the respective layout with same name Once you have done compilation for all android will automatically will pick the respective layout for that i created emulators of the resolution and then adjusted the layouts as per my requirement

查看更多
啃猪蹄的小仙女
4楼-- · 2019-07-26 03:53

create different size images from provided PSD for different screen size layout, and create different layout xml file for different screen size, in different layout file you can provide reference of images which fit for layout.

For better aprroach you should go here and also this

查看更多
登录 后发表回答