For an app that should run on Android and iOS an animation effect is needed.
A fullscreen image shall get an alternating zoom in/out animation. It is expected that after a continuous zoom-in the image stands for a short time and then zooms out again continuously.
This is solved in principle by means of a timer and "SetLayoutAnimated", but it still has the unpleasant side effect that the zoom of the image jumps.
What has to be done to get a smooth zoom in/out that works on Android and iOS alike?
A fullscreen image shall get an alternating zoom in/out animation. It is expected that after a continuous zoom-in the image stands for a short time and then zooms out again continuously.
This is solved in principle by means of a timer and "SetLayoutAnimated", but it still has the unpleasant side effect that the zoom of the image jumps.
What has to be done to get a smooth zoom in/out that works on Android and iOS alike?
B4X:
#Region Project Attributes
#ApplicationLabel: B4A Example
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: portrait
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: true
#IncludeTitle: false
#End Region
Sub Process_Globals
Private imagename1 As String = "shot-by-cerqueira-0o_GEzyargo-unsplash.jpg"
Private xui As XUI
Private ImageScaleAtStart As Float = 1.1
Private ImageScaleAtZoom As Float = 1.2
Private MovePaneBigTimer As Timer
Private MovePaneBigTimerInterval As Int = 3000
Private MovePaneBigZoomIn As Boolean = True
End Sub
Sub Globals
Private BigImageView1 As B4XView
Private MasterBack As B4XView
Private MovePaneBig As B4XView
Private InfoLabel1 As B4XView
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Layout1")
CallSubDelayed(Me, "FormFill")
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
' ---------------------------------------
Sub FillImageToView(bmp As B4XBitmap, ImageView As B4XView)
' Erel --> https://www.b4x.com/android/forum/threads/b4x-xui-fill-and-fit-images-without-distortion.86627/#content
Dim bmpRatio As Float = bmp.Width / bmp.Height
Dim viewRatio As Float = ImageView.Width / ImageView.Height
If viewRatio > bmpRatio Then
Dim NewHeight As Int = bmp.Width / viewRatio
bmp = bmp.Crop(0, bmp.Height / 2 - NewHeight / 2, bmp.Width, NewHeight)
Else if viewRatio < bmpRatio Then
Dim NewWidth As Int = bmp.Height * viewRatio
bmp = bmp.Crop(bmp.Width / 2 - NewWidth / 2, 0, NewWidth, bmp.Height)
End If
Dim scale As Float = 1
#if B4i
scale = GetDeviceLayoutValues.NonnormalizedScale
#End If
ImageView.SetBitmap(bmp.Resize(ImageView.Width * scale, ImageView.Height * scale, True))
End Sub
Sub FormFill
InfoLabel1.Text = "FormFill"
Dim bmp As B4XBitmap = xui.LoadBitmap(File.DirAssets, imagename1)
FillImageToView(bmp, BigImageView1)
MovePaneBigCalcSize(ImageScaleAtStart)
MovePaneBigTimer.Initialize("MovePaneBigTimer", MovePaneBigTimerInterval)
MovePaneBigTimer.Enabled = True
End Sub
Sub MovePaneBigTimer_Tick
If MovePaneBigZoomIn Then
MovePaneBigCalcSize(ImageScaleAtZoom)
Else
MovePaneBigCalcSize(ImageScaleAtStart)
End If
MovePaneBigZoomIn = Not(MovePaneBigZoomIn)
End Sub
Sub MovePaneBigCalcSize(ScaleFac As Float)
Dim NewW As Int = MovePaneBig.Width *ScaleFac
Dim NewH As Int = MovePaneBig.Height *ScaleFac
Dim NewL As Int = (MovePaneBig.Width -NewW) /2
Dim NewT As Int = (MovePaneBig.Height -NewH) /2
InfoLabel1.Text = $"SetLayoutAnimated with ScaleFac = $1.2{ScaleFac}"$
BigImageView1.SetLayoutAnimated(MovePaneBigTimerInterval, NewL, NewT, NewW, NewH)
'Sleep(MovePaneBigTimerInterval +10)
'Sleep(10)
End Sub