Question

I need to know the relation between points and pixels and how it affects different BB 7.0 and lower version devices.

I have a project which parses values of width and height of components to be displayed in points and I have converted them into pixels and shown on different devices using the following formula.

fldwidth  = fldwidth*Display.getWidth()/100
fldheight = fldheight*Display.getHeight()/100

where initially the values of fldwidth and fldheight has pt values in decimal.

Am I correct

Was it helpful?

Solution

A point is, by definition, 1/72 of an inch - see Wikipedia Point_(typography)

The size of pixel is dependent on the screen resolution on the device. Just to be clear, this is resolution normally stated in dots per inch (dpi). This is not the common usage for the term resolution which is the pixel height and width of the screen. People use resolution in this way incorrectly. Resolution is the density of dots on the screen, not the number of pixels on the screen.

The point here is that there is NO relationship between the number of pixels displayed on the screen with the number of pixels that are required for a point. You can not use the conversion that you are attempting.

To determine the number of pixels that match 1 point, you must get the resolution of the screen. BB provides two methods for this:

Display.getHorizontalResolution();
Display.getVerticalResolution();

Fortunately, these will give you the same value on all BBOS (Java) devices, as all BBOS devices have the same vertical and horizontal resolution.

The value supplied is the number of pixels in one metre. So all you need to do is determine how many 1/72s of an inch there are in 1 metre, divide one of these values by that number, and then you have the number of pixels in a point.

Because of integer arithmetic, when doing this calculation, I would multiply by the point size you are trying to achieve before doing the division. For example:

int pixelSizeReqd = pointSizeReq * 
Display.getHorizontalResolution() / pointsInOneMetre;

And by the way, just call Display.getHorizontalResolution() once and reuse the returned value. I am not sure about getHorizontalResolution(), but I do know that some Display methods, for example, getHeight() and getWdith() are 'expensive' so should be avoided if possible. The value is not going to change anyway!

Update following this comment: Can you explain in an example . Suppose I got a device 8520 (320x240 resolution) i have a point (say 57pt) what would be its corresponding pixel value as per your formula ... int pixelSizeReqd = pointSizeReq * Display.getHorizontalResolution() / pointsInOneMetre

Answer: Note that the 8520 has a screen size of 320 x 240. That is not its screen resolution for the purposes of this discussion. Got that?

You want a size of 57 points. So the calculation is:

int pixelSizeReqd = 57 * Display.getHorizontalResolution() / pointsInOneMetre;

You shouldn't replace Display.getHorizontalResolution() with a figure - it will be different on different devices and there is no need for you to try to fix this value for yourself.

How many points are there in 1 metre? You can do the math, convert a 1/72 inch into metres and then divide 1 metre by this. Or you can type into Google "how many points in a meter" and get the answer 2,834.64567. We don't need the accuracy, so we just use integer arithmetic to give us this:

int pixelSizeReqd = 57 * Display.getHorizontalResolution() / 2834;

Job done - that wasn't too hard was it?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top