B4J Question B4XImageView : I need to get the image coordinates, not the view's

Cableguy

Expert
Licensed User
Longtime User
Hi guys.

I am using a B4XImageView to take advantage of some of its unique features, but I'm having trouble with the following, for which I request some help:
The images are loaded to fit the available space while keeping their scale.
I need to get the image.left value and not the container, my B4XImageView's one... But I just can't see how.

1698590320831.png

The Red square is my B4XImageView and the Blue Square is the size I need to get (Left and Width)

Any Help????
 
Solution
The image height = container height
The image width = (original Width / original Height) * container height
The left space = (container width - image width) / 2

Cableguy

Expert
Licensed User
Longtime User
The image height = container height
The image width = (original Width / original Height) * container height
The left space = (container width - image width) / 2
Thank YOU sir, worked like a charm!!!
(I should have been more attentive in school!)
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
You may also check the width / height ratio to see if the width or the height fits the container dimensions.
That is automatically taken into account by the B4XImageView "gravity" settings
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Are you sure ?
What happens when the with / height ratio of the image is bigger than the width / height ratio of the B4XImageView ?
In that case the equations above will give wrong image coordinates.
The B4XImageView is sized to be in landscape mode
Case#1 : The loaded image is in portrait size
1698598991930.png


Case#2 : The loaded image is in landscape size
1698599157648.png
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
In the first image the top coordinate of the image is 0 and the left coordinate is > 0
In the second image the left coordinate of the image is 0 and the top coordinate is > 0 !
This is my concern.
Yes, but the "ratio" Heigth/Width stays the same for each image, so the given formula works
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
As @klaus pointed out, the formula needs modification to make it valid for all cases.

When an image is scaled to fit the container and aspect ratio is preserved...

let OR = original w/h ratio
Let CR = container w/h ratio

if OR is less than CR then there is left space
if OR is greater than CR then there is top space
if OR = CR then no space

If left space then
left space = (container width - container height * OR) / 2

If top space then
top space = (container height - container width / OR) / 2
 
Upvote 0

William Lancee

Well-Known Member
Licensed User
Longtime User
Just to make sure, I did some tests.

B4X:
Private Sub computeSpace(origWidth As Int, origHeight As Int, panelWidth As Int, panelHeight As Int) As Int()
    Dim origR As Float = origWidth / origHeight
    Dim panelR As Float = panelWidth / panelHeight
    Dim leftSpace As Int = IIf(origR < panelR, panelWidth - panelHeight * origR, 0)
    Dim topSpace As Int = IIf(origR > panelR, panelHeight - panelWidth / origR, 0)
    Return Array As Int(leftSpace, topSpace)
End Sub
B4X:
'    Dim test() As Int = computeSpace(1000, 2000, 500, 300)
'    Log(test(0) & TAB & test(1))                            '    350    0

'    Dim test() As Int = computeSpace(1000, 2000, 500, 500)
'    Log(test(0) & TAB & test(1))                            '    250    0

'    Dim test() As Int = computeSpace(1000, 2000, 500, 600)
'    Log(test(0) & TAB & test(1))                            '    200    0

'    Dim test() As Int = computeSpace(2000, 2000, 500, 500)
'    Log(test(0) & TAB & test(1))                            '    0    0

'    Dim test() As Int = computeSpace(4000, 2000, 500, 300)
'    Log(test(0) & TAB & test(1))                            '    0    50

'    Dim test() As Int = computeSpace(4000, 2000, 500, 500)
'    Log(test(0) & TAB & test(1))                            '    0    250

'    Dim test() As Int = computeSpace(4000, 2000, 500, 600)
'    Log(test(0) & TAB & test(1))                            '    0    350
 
Upvote 0
Top