#Region Shared Files
#CustomBuildAction: folders ready, %WINDIR%\System32\Robocopy.exe,"..\..\Shared Files" "..\Files"
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
#End Region
'Ctrl + click to export as zip: ide://run?File=%B4X%\Zipper.jar&Args=%PROJECT_NAME%.zip
Sub Class_Globals
Private Root As B4XView
Private xui As XUI
Private videoPlayer As VideoView
Private VideoReady As Boolean = False
Private Const VIDEO_FILENAME As String = "Sfondo_animato.mp4"
End Sub
Public Sub Initialize
' B4XPages.GetManager.LogEvents = True
End Sub
'This event will be called once, before the page becomes visible.
Private Sub B4XPage_Created (Root1 As B4XView)
Root = Root1
Root.LoadLayout("MainPage")
End Sub
'You can see the list of page related events in the B4XPagesManager object. The event name is B4XPage.
Private Sub CreaSfondoFullSeServe
If videoPlayer.IsInitialized Then Return
' Copia il file (se non esiste già)
If File.Exists(File.DirInternal, VIDEO_FILENAME) = False Then
Try
File.Copy(File.DirAssets, VIDEO_FILENAME, File.DirInternal, VIDEO_FILENAME)
Log("Video copiato con successo")
Catch
Log("Errore copia file: " & LastException.Message)
Return
End Try
End If
videoPlayer.Initialize("videoPlayer")
videoPlayer.Visible = True
Root.AddView(videoPlayer, 0, 0, Root.Width, Root.Height)
videoPlayer.SendToBack
Try
videoPlayer.LoadVideo(File.DirInternal, VIDEO_FILENAME)
Log("Caricamento video avviato da: " & File.DirInternal)
Catch
Log("Errore caricamento: " & LastException.Message)
End Try
' Fallback immediato (anche se non è ancora pronto)
' ApplicaCenterCrop
End Sub
Private Sub videoPlayer_Ready
VideoReady = True
Try
videoPlayer.Play
Catch
Log("Errore videoPlayer_Ready: " & LastException.Message)
End Try
' Crop reale
End Sub
Private Sub videoPlayer_Prepared
VideoReady = True
Try
videoPlayer.Play
Catch
Log("Errore videoPlayer_Prepared: " & LastException.Message)
End Try
End Sub
Private Sub ApplicaCenterCrop
If videoPlayer.IsInitialized = False Then Return
If Root.IsInitialized = False Then Return
'Wrap dell'istanza VideoView (NON static)
Dim jo As JavaObject = videoPlayer
Dim vwPx As Int = jo.RunMethod("getVideoWidth", Null)
Dim vhPx As Int = jo.RunMethod("getVideoHeight", Null)
If vwPx <= 0 Or vhPx <= 0 Then Return
'px -> dip
Dim scale As Float = GetDeviceLayoutValues.Scale
Dim vw As Float = vwPx / scale
Dim vh As Float = vhPx / scale
Dim rw As Float = Root.Width
Dim rh As Float = Root.Height
'Cover: riempi tutto (taglia il minimo)
Dim s As Float = Max(rw / vw, rh / vh)
Dim newW As Float = vw * s
Dim newH As Float = vh * s
Dim left As Float = (rw - newW) / 2
Dim top As Float = (rh - newH) / 2
videoPlayer.SetLayoutAnimated(0, left, top, newW, newH)
videoPlayer.SendToBack
End Sub
Private Sub B4XPage_Appear
CreaSfondoFullSeServe
If VideoReady Then ApplicaCenterCrop
End Sub
Private Sub B4XPage_Resize (Width As Int, Height As Int)
If videoPlayer.IsInitialized Then
'fallback fullscreen
videoPlayer.SetLayoutAnimated(0, 0, 0, Width, Height)
If VideoReady Then ApplicaCenterCrop
End If
End Sub
Private Sub Button1_Click
' xui.MsgboxAsync("Hello world!", "B4X")
End Sub