' Compute draw params for either FIT or FILL (all DIP)
Public Sub CalcDrawParams(xiv As B4XImageView, bmp As B4XBitmap, isFill As Boolean) As DrawParams
Dim VW As Float = xiv.mBase.Width
Dim VH As Float = xiv.mBase.Height
Dim IW As Float = bmp.Width / xui.Scale
Dim IH As Float = bmp.Height / xui.Scale
Dim s As Float
If isFill Then
s = Max(VW / IW, VH / IH) ' AspectFill
Else
s = Min(VW / IW, VH / IH) ' AspectFit
End If
Dim dispW As Float = IW * s
Dim dispH As Float = IH * s
Dim offX As Float = (VW - dispW) / 2 ' note: negative in FILL if dispW > VW
Dim offY As Float = (VH - dispH) / 2 ' note: negative in FILL if dispH > VH
Dim p As DrawParams
p.Initialize
p.s = s
p.offX = offX
p.offY = offY
p.dispW = dispW
p.dispH = dispH
Return p
End Sub
' Place a horizontal slider using the params (works in FIT or FILL)
Public Sub PlaceSliderWithParams(xiv As B4XImageView, hostParent As B4XView, _
slider As B4XView, normY As Float, p As DrawParams, isFill As Boolean)
normY = Max(0, Min(1, normY))
Dim VW As Float = xiv.mBase.Width
Dim VH As Float = xiv.mBase.Height
Dim baseLeft As Float = hostParent.Left + xiv.mBase.Left
Dim baseTop As Float = hostParent.Top + xiv.mBase.Top
Dim centerY As Float
If isFill = False Then
' FIT (AspectFit): letterboxed; use dp.offY/dispH directly
Dim visTop As Float = baseTop + p.offY ' offY >= 0
centerY = visTop + normY * p.dispH
slider.Left = baseLeft + p.offX
slider.Width = p.dispW
Else
' FILL (AspectFill): cropped; map full-image normY into the visible slice [0..1] of the view height
' Compute crop in normalized-image units
Dim cropTopNorm As Float = -p.offY / p.dispH
Dim cropBottomNorm As Float = (VH - p.offY) / p.dispH
cropTopNorm = Max(0, Min(1, cropTopNorm))
cropBottomNorm = Max(0, Min(1, cropBottomNorm))
' Remap full-image normY -> visible-slice norm (0..1 inside the view)
Dim adj As Float
If cropBottomNorm > cropTopNorm Then
adj = (normY - cropTopNorm) / (cropBottomNorm - cropTopNorm)
Else
adj = 0.0
End If
adj = Max(0, Min(1, adj))
centerY = baseTop + adj * VH
' In FILL, image covers the whole view horizontally
slider.Left = baseLeft
slider.Width = VW
End If
slider.Top = Round(centerY - slider.Height / 2)
End Sub