Android Question Exact ruler is not exact

DALB

Active Member
Licensed User
Hello everyone,
Happy Chritmas, frohe Weihnachten, und so weiter...

About drawing a real ruler in centimeters.
I use Erel's sub (below):
physical dimensions (post #4). But when I control my rulers...
I observe that 1 cm is really 0.95 cm on my screen (see the screen capture)

The code for the horizontal ruler is this following one:
ruler X:
Sub tracerReglex
  
    'Dim ratx As Double=xdpi*2.54/xcm'     exactsize=pixels per cm
    'xcm=(Round2((100%x/xdpi*2.54),2))=largeur de l'écran
 
 Dim ratx As Double=xdpi/(xcm/2.54)
    cvsg.Initialize(pnlreglex)
    Dim n As Int=0
    Dim st As Int=(ratx/10)*1'dpi/mm
    For x=100 To 3500 Step (st*10)'cm
        cvsg.DrawLine(x,1dip,x,30dip,Colors.black,1dip)
        Dim lbr As Label
        lbr.Initialize("")
        lbr.TextSize=10
        lbr.TextColor=Colors.Black
        If n=3 Then
            lbr.Text=n & " cm"
        Else
            lbr.Text=n
        End If
        pnlreglex.addview(lbr,x+2dip,20dip,60dip,30dip)
        n=n+1
    Next
    For x=100 To 3500 Step (st*5)'demi cm
        cvsg.DrawLine(x,1dip,x,20dip,Colors.black,1dip)
    Next
    For x=100 To 3500 Step (st)'mm
        cvsg.DrawLine(x,1dip,x,10dip,Colors.black,1dip)
    Next

End Sub

What do I have to modify to obtain the correct dimensions for the X ans Y rulers ?
 

Attachments

  • Screenshot_2021-12-25_114813_200.png
    Screenshot_2021-12-25_114813_200.png
    146.4 KB · Views: 140

klaus

Expert
Licensed User
Longtime User
I am afraid that, at least, a part of the difference comes from the rounding to Int on some variables.
I tested the code below on both my devices, Samsung Galaxy S10 and Samsung Galaxy Tab S6 Lite.
And the result is OK..
But, as agraham already mentioned, the accuracy depends on the accuracy of the scale values defined by the manufacturer in getDisplayMetrics.

B4X:
Sub ExactSize
    Dim r As Reflector
    r.Target = r.GetContext
    r.Target = r.RunMethod("getResources")
    r.Target = r.RunMethod("getDisplayMetrics")
    xdpi = r.GetField("xdpi")
    ydpi = r.GetField("ydpi")
End Sub

Private Sub DrawRuler
    Private i, x1, x2, x3, x4, y1, y2, y3, y4 As Int
    Private ScreenWidthmm, ScreenHeightmm As Int
    Private xmm, ymm As Double
   
    ExactSize
   
    ScreenWidthmm = 100%x / xdpi * 25.4        'screen width in mm
    ScreenHeightmm = 100%y / ydpi * 25.4    'screen height is mm
    xmm = xdpi / 25.4                                            'number of X pixels per mm
    ymm = ydpi / 25.4                                            'number of Y pixels per mm
   
    y1 = 0dip
    y2 = y1 + 12dip
    y3 = y1 + 20dip
    y4 = y1 + 30dip
   
    For i = 0 To ScreenWidthmm
        x1 = i * xmm
        If i Mod 10 = 0 Then
            cvsRuler.DrawLine(x1, y1, x1, y4, Colors.Black, 1dip)
        Else If i Mod 5 = 0 Then
            cvsRuler.DrawLine(x1, y1, x1, y3, Colors.Black, 1dip)
        Else
            cvsRuler.DrawLine(x1, y1, x1, y2, Colors.Black, 1dip)
        End If
    Next
   
    x1 = 0dip
    x2 = x1 + 12dip
    x3 = x1 + 20dip
    x4 = x1 + 30dip

    For i = 0 To ScreenHeightmm
        y1 = 100%y - i * ymm
        If i Mod 10 = 0 Then
            cvsRuler.DrawLine(x1, y1, x4, y1, Colors.Black, 1dip)
        Else If i Mod 5 = 0 Then
            cvsRuler.DrawLine(x1, y1, x3, y1, Colors.Black, 1dip)
        Else
            cvsRuler.DrawLine(x1, y1, x2, y1, Colors.Black, 1dip)
        End If
    Next
    cvsRuler.Invalidate
End Sub
 

Attachments

  • TestRuler.zip
    9.2 KB · Views: 137
Upvote 1

DALB

Active Member
Licensed User
Thanks to you too.
I thinked also that calibration is needed, that means I have to create an action more.

I thought that the pixel dimensions were the same for all the devices. Maybe not.

I don't think that xdpi and ydpi are always exact. Every ruler app I've tried on the Play Store has a way for the user to calibrate to the exact size.
It is possible. I have a special magnifier with rulers, I'll see and verify this (on xiaomi redmi 9, 5.5 inches versus a lenovo tablet 8 inches)

I am afraid that, at least, a part of the difference comes from the rounding to Int on some variables.
It's true, I have modified my code.

So, after this, it seems to works better.

Happy to work with B4A, happy to be helped by fair poeple, and clever brains !
 
Upvote 0

DALB

Active Member
Licensed User
I've controlled the number of dots (lit points) in a visual matrix magnifier, and the sizes of them are not the same between my xiaomi redmi 9 phone and my tablet lenvo 8 inches. But in the two cases they are square.
I'll try to put a picture if I can.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
I thought that the pixel dimensions were the same for all the devices. Maybe not.
Oh no !!!
One reason why we use dip values is the difference between devices with their density values.
In Android the densities are standardized 120, 160, 240, 320, 480 dots per inch (dip) etc 160 dip is the base value.
And the corresponding scale values: 0.75, 1, 1,5, 2, 3 etc.
But the real physical dimensions are different from these 'standardized' values.
On my Samsung Galaxy S10 the values of xdpi and ydpi are a bit different:
xdpi = 415.6357727050781
ydpi = 413.656494140625

My device has a density of 3, which means a 'standardized dip value of 160 * 3 = 480.
But the real values are about 415.
And it seems that some manufacturers do, even, not give correct xdpi and ydpi values.
 
Upvote 0

DALB

Active Member
Licensed User
Klaus, you are right, I thought that the intervals between the dots were different. But with the pictures I took some minutes ago, I can see how it is arranged.
I didn't realize before having to work on this subject.
I post 3 pictures:
ruler reference (rulerRef.jpg) on which 100 graduations are 1 mm.
lenovo 8 inches, I find 160 dots per mm, or 406 per inch.
xiaomi redmi 9, I find 120 dots per mm, or 305 per inch.

I better understand what is what.
 

Attachments

  • lenovo8inches.jpg
    lenovo8inches.jpg
    250.4 KB · Views: 138
  • rulerRef.jpg
    rulerRef.jpg
    221.5 KB · Views: 134
  • xiaomiRedmi9.jpg
    xiaomiRedmi9.jpg
    90 KB · Views: 133
Upvote 0
Top