开发者

How to make sure information in pixels work on all screen sizes?

I have a Gallery with images of text, and a database that contains each text's position in the images..

The position is represented in pixels, and thats the problem:

How can I make sure that the information works on each screen resolution?

The images are 300x400..

NOTE: the database already contain these values in pixel.. I cant change them开发者_如何转开发.


Don't specify the positions in pixels, use dp instead http://developer.android.com/guide/topics/resources/more-resources.html#Dimension

and read this for more in formation on how to support multiple screens: http://developer.android.com/guide/practices/screens_support.html


If you are unable to change to dp values, you can instead check the DisplayMetrics with:

metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);

with metrics.densityDpi you get the current Density.

Then implement a switch statement for each density adding a scaling factor to all your px values, similar to this:

switch (density) {
    case 120:
        scaleFactor = 0.75;
        break;

    case 160:
        scaleFactor = 1;
        break;

    case 240:
        scaleFactor = 1.5;
        break;

    case 320:
        scaleFactor = 2;
        break;

    default:
        break;
    }


I prefer you following the standard sizes for hdpi,ldpi and mdpi. They are 480x800, 320x480 and 240x400 and place them in their respective folders. Your current resolution will make the image stretched to suit the devices. Name all the images with the same name. Android will take care of the rest. :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜