Android Question Canvas DrawText and Device Scale

tdocs2

Well-Known Member
Licensed User
Longtime User
Greetings, all, and thank you in advance for replying to my question.

I seem to have an issue with Canvas DrawText and Device Scale.

B4X:
        Canvas1.DrawText("X", tc(0)*1dip, tc(1)*1dip, Typeface.DEFAULT, 14, Colors.red,"CENTER")

Note: tc(0) is the x coordinate in dips; tc(1) is the y coordinate in dips.

This code works in all my devices which have a scale with no decimal points. Scales tested: 1.0, 2.0 with varying resolutions: 1280 by 800, 1920 by 1080, 2560 by 1600, and others.

This code does not work with devices with scales with decimal points: 1.33 and 1.5 with resolutions 1280 by 800 and 1920 by 1200. It basically defaults to a scale of 1.

I have develop a workaround by establishing a multiplier for the x and y coordinates and multiplying tc(0) and tc(1) for their respective axis, but it is not as clean as the code above.

Any help will be welcomed.

Best regards.

Sandy
 

klaus

Expert
Licensed User
Longtime User
Unfortunately you don't give enough information.
What variable type are tc(0) and tc(1), how are they defined ?
I'm afraid that you are going the wrong way.
In my mind, you shoud define for tc(0) and tc(1) decive independant coordinates and calculate a scale factor depending on the dimensions of the container view.
But without knowing what exactly you want to do it's difficult to give a concrete advice.
 
Upvote 0

tdocs2

Well-Known Member
Licensed User
Longtime User
Unfortunately you don't give enough information.
What variable type are tc(0) and tc(1), how are they defined ?
I'm afraid that you are going the wrong way.
In my mind, you shoud define for tc(0) and tc(1) decive independant coordinates and calculate a scale factor depending on the dimensions of the container view.
But without knowing what exactly you want to do it's difficult to give a concrete advice.

Thank you, Klaus.

The target coordinates, tc, are read from a list which is populated from a csv which I constructed based on a scale of 1 - 160dip. The tc(0) is the x coordinate in dips; tc(1) is the y coordinate in dips - they are based on a scale of 1 - 160dip.

My question or the point I was making is that the solution in this code:

B4X:
Canvas1.DrawText("X", tc(0)*1dip, tc(1)*1dip, Typeface.DEFAULT, 14, Colors.red,"CENTER")

works in several devices with varying resolutions and scales AS LONG AS THE SCALE does not contain decimal points like scale 1.5 (Asus Transformer 700) and 1.33 (Nexus 7). For these two devices, the code behaves as a scale of 1.0 and not 1.5 or 1.33. Why would this inconsistency take place? Does the Canvas.DrawText treats the device scale as an integer type and not float or double?

Your advice is generous and solid as it always is. I have developed a workaround alongside your advice based
on the dimensions of the container view.
The question still nags me a bit since the concept of dip is foundational.

Very best regards to you, Klaus, and my thanks for all of your contributions to the B4A community.

Sandy
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
B4X:
Canvas1.DrawText("X", tc(0)*1dip, tc(1)*1dip, Typeface.DEFAULT, 14, Colors.red,"CENTER")
The problem is that dip returns an Int, so the fractional part is lost !
Use this instead.
B4X:
Dim scale = 100dip / 100 As Float
Canvas1.DrawText("X", tc(0)*scale, tc(1)*scale, Typeface.DEFAULT, 14, Colors.red,"CENTER")
 
Upvote 0

tdocs2

Well-Known Member
Licensed User
Longtime User
B4X:
Canvas1.DrawText("X", tc(0)*1dip, tc(1)*1dip, Typeface.DEFAULT, 14, Colors.red,"CENTER")
The problem is that dip returns an Int, so the fractional part is lost !
Use this instead.
B4X:
Dim scale = 100dip / 100 As Float
Canvas1.DrawText("X", tc(0)*scale, tc(1)*scale, Typeface.DEFAULT, 14, Colors.red,"CENTER")

Brilliant and simple!

Thank you, Klaus.

Sandy
 
Upvote 0
Top