Android Question ruller and centimeters

Kapelis

Member
Licensed User
Longtime User
Hello friends, I kindly ask for your help to solve a problem. I would like to build a simple ruler on the screen (landscape oriented), with ten simple bars that would represent centimeters. Obviously the distances on the screen should match the reality, on my screen (Galaxy S3) as well as on all screens. I'll post the code I used. The bars of centimeters on the screen of my cell does not correspond to the truth, between 0 and 1 cm are almost right, but at the tenth cm the difference is so great, about 3-4 mm. Where am I wrong? Thank you for your help.
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
Dim Cel As Phone
    Private imgRosso As ImageView
    Private imgBlu As ImageView
Dim dpi As Float
    Dim dpi2 As Float
Dim S As Float
Private img1 As ImageView
    Private img2 As ImageView
    Private img3 As ImageView
    Private img4 As ImageView
    Private img5 As ImageView
    Private img6 As ImageView
    Private img7 As ImageView
    Private img8 As ImageView
    Private img9 As ImageView
Private img10 As ImageView
Private img11 As ImageView
    Private Panel1 As Panel
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Cel.SetScreenOrientation(0)

    S=GetDeviceLayoutValues.Scale ' find screen dpi
    If S= 0.75 Then
        dpi=120
    Else If  S= 1 Then
        dpi=160
    Else If  S= 1.5 Then
        dpi=240
    Else If S=2 Then
        dpi=320
    End If

    Dim Cm As Int
' dpi in 1 Cm. If in one inch (25.4 mm) I have 320 dpi, in 1 cm (10 mm) I will have the dpi obtained with the following formula:

    Cm = dpi*10/25.4

    ' centimetri
    img1.Left=0dip
    img2.Left= Cm*1
    img3.Left= Cm*2
    img4.Left= Cm*3
    img5.Left= Cm*4
    img6.Left=Cm*5
    img7.Left=Cm*6
    img8.Left=Cm*7
    img9.Left=Cm*8
    img10.Left=Cm*9
img11.Left=Cm*10

End Sub
 

LucaMs

Expert
Licensed User
Longtime User


It seems to me that this function returns the same values of the group of IF statements.

At the very least, for my device (1.5) and for the standard (1).

However, if I have XDpi = YDpi = 240, then I have DpiPerCm = XDpi / 2:54, right?

Why, with a variant identical to my device, get I drawn cm shorter than 1 cm?

http://www.b4x.com/android/forum/threads/righello-e-centimetri.41379/#post-248923
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…