Spanish Una ayuda

roy89

Member
Aqui les pongo mi proyecto para que lo prueben y me ayuden solo agregue 2 fotos en los assets y dos fotos en una subcarpeta del proyecto para hacer menos pesado el proyecto al subirlo bueno mi problema es el siguiente al precionar en cada boton del menu quiero cambiar de layout o de actividad para que muestre otra categoria de imagenes pero ahi es donde estoy perdido me siguen abriendo las mismas imagenes y cuando cambio de actividad o de layout aparece en blanco si alguien puede tirarme una ayuda aqui le subo mi trabajo se lo voy a agradecer les pongo mis codigos porque la pagina no me deja subir el archivo ya que pesa mas de 23 megas

actividad (Main.bas)

#Region Project Attributes
#ApplicationLabel: Wallpaper
#VersionCode: 1
#VersionName: 1.0
#SupportedOrientations: portrait
#CanInstallToExternalStorage: False
#End Region

#Region Activity Attributes
#FullScreen: False
#IncludeTitle: False
#End Region

#AdditionalJar: android-support-v4

Sub Process_Globals
Private xui As XUI
Private targetWidth As Int = 200
Private targetHeight As Int = 300
Private fullScreenWidth As Int = 1080
Private fullScreenHeight As Int = 1920
Private isFullScreen As Boolean = False
Private Timer1 As Timer
Private currentImageIndex As Int = 1
Private maxImages As Int = 134
Private ButtonAnimationTimer As Timer
Private buttonAnimationActive As Boolean = False
Private buttonAnimationAlpha As Int = 255
Private buttonAnimationDirection As Int = -1

End Sub

Sub Globals
Private clvImagenes As CustomListView
Private imagenes As List
Private pnlFullScreen As Panel
Private btnAccion As Button
Private imgFullScreen As ImageView
Private currentPosition As Int
Private WobbleMenu1 As WobbleMenu

End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("layout")


'Menu personalizado
WobbleMenu1.SetTabTextIcon(1,"Animales",Chr(0xF1B0),Typeface.FONTAWESOME)
WobbleMenu1.SetTabTextIcon(2,"Ánime",Chr(0xF1D0),Typeface.FONTAWESOME)
WobbleMenu1.SetTabTextIcon(3,"Variado",Chr(0xF0E8),Typeface.FONTAWESOME)
WobbleMenu1.SetTabTextIcon(4,"Tecnología",Chr(0xF2DB),Typeface.FONTAWESOME)
WobbleMenu1.SetTabTextIcon(5,"Juegos",Chr(0xF11B),Typeface.FONTAWESOME)
WobbleMenu1.SetBadge(4,0,Colors.White,Colors.Blue)






' Configurar el fondo de la actividad
Activity.Color = Colors.ARGB(255, 240, 240, 240) ' Gris claro para el fondo

' Verificar que el CustomListView se cargó correctamente
If clvImagenes.IsInitialized = False Then
Log("Error: CustomListView no se inicializó correctamente")
Return
End If

' Inicialización de componentes
imagenes.Initialize

' Configuración del panel de pantalla completa
pnlFullScreen.Initialize("pnlFullScreen")
imgFullScreen.Initialize("imgFullScreen")
btnAccion.Initialize("btnAccion")

' Configurar propiedades del botón
btnAccion.Text = "Establecer"
btnAccion.TextSize = 16
btnAccion.TextColor = Colors.White
btnAccion.Color = Colors.ARGB(200, 50, 150, 255)

' Añadir vistas al panel
pnlFullScreen.AddView(imgFullScreen, 0, 0, 100%x, 100%y)
pnlFullScreen.AddView(btnAccion, 50%x - 50dip, 90%y - 60dip, 100dip, 50dip)
btnAccion.BringToFront





' Cargar imágenes
LoadImages
End Sub



Sub Activity_Resume
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean
If KeyCode = KeyCodes.KEYCODE_BACK Then
If isFullScreen Then
CloseFullScreen
Return True
End If
End If
Return False
End Sub

Sub LoadImages
Timer1.Initialize("Timer1", 20) ' Reducido el intervalo para carga más rápida
Timer1.Enabled = True
currentImageIndex = 1
End Sub

Sub Timer1_Tick
If currentImageIndex <= maxImages Then
Dim imageName As String = currentImageIndex & ".jpg"
If File.Exists(File.DirAssets, imageName) Then
Dim bmp As Bitmap = LoadBitmapSample(File.DirAssets, imageName, targetWidth, targetHeight)
imagenes.Add(bmp)

' Actualizar la galería cada 4 imágenes
If currentImageIndex Mod 4 = 0 Then
CallSubDelayed(Me, "ConfigureGallery")
End If
End If
currentImageIndex = currentImageIndex + 1
Else
Timer1.Enabled = False
CallSubDelayed(Me, "ConfigureGallery")
End If
End Sub

Sub ConfigureGallery
clvImagenes.Clear
For i = 0 To imagenes.Size - 1 Step 2
Dim pnl As B4XView = xui.CreatePanel("")
pnl.SetLayoutAnimated(0, 0, 0, 100%x, 200dip)

Dim margin As Int = 1%x
Dim imageWidth As Int = (100%x - 3 * margin) / 2
Dim imageHeight As Int = 200dip

' Primera imagen
Dim img1 As ImageView
img1.Initialize("imgItem")
pnl.AddView(img1, margin, 1dip, imageWidth, imageHeight)
img1.Bitmap = imagenes.Get(i)
img1.Tag = i
img1.Gravity = Gravity.FILL

' Segunda imagen (si existe)
If i + 1 < imagenes.Size Then
Dim img2 As ImageView
img2.Initialize("imgItem")
pnl.AddView(img2, 2 * margin + imageWidth, 1dip, imageWidth, imageHeight)
img2.Bitmap = imagenes.Get(i + 1)
img2.Tag = i + 1
img2.Gravity = Gravity.FILL
End If

clvImagenes.Add(pnl, 210dip)
Next
End Sub

Sub imgItem_Click
Dim v As View = Sender
currentPosition = v.Tag
ShowFullScreenImage(imagenes.Get(currentPosition))
End Sub

Sub ShowFullScreenImage(bmp As Bitmap)
' Verificar inicialización de componentes
If pnlFullScreen.IsInitialized = False Or imgFullScreen.IsInitialized = False Then
Log("Error: Componentes no inicializados")
Return
End If





' Cargar la imagen en alta calidad para pantalla completa
Dim imageName As String = (currentPosition + 1) & ".jpg"
If File.Exists(File.DirAssets, imageName) Then
imgFullScreen.Bitmap = LoadBitmapSample(File.DirAssets, imageName, fullScreenWidth, fullScreenHeight)
Else
imgFullScreen.Bitmap = bmp
End If

imgFullScreen.Gravity = Gravity.FILL

' Solo añadir si no está ya añadido
If pnlFullScreen.Parent = Null Then
Activity.AddView(pnlFullScreen, 0, 0, 100%x, 100%y)
End If

pnlFullScreen.Color = Colors.Black
pnlFullScreen.Visible = True
isFullScreen = True
End Sub

Sub CloseFullScreen
' Verificación más robusta
If pnlFullScreen = Null Or pnlFullScreen.IsInitialized = False Then
Log("Panel no inicializado, no se puede cerrar")
isFullScreen = False
Return
End If

' Primero ocultar el panel
pnlFullScreen.Visible = False
isFullScreen = False





' Método 100% seguro para remover la vista
Try
' Verificar si el panel está añadido a la actividad
For i = 0 To Activity.NumberOfViews - 1
Dim v As View = Activity.GetView(i)
If v = pnlFullScreen Then
Activity.RemoveViewAt(i)
Exit
End If
Next
Catch
Log("Error al remover vista: " & LastException.Message)
End Try

' Otra alternativa segura
If pnlFullScreen.IsInitialized Then
pnlFullScreen.Visible = False
End If
End Sub

Sub pnlFullScreen_Touch (Action As Int, X As Float, Y As Float) As Boolean
Return True ' Captura todos los toques y no los deja pasar
End Sub

' ===== FUNCIÓN NUEVA PARA CAMBIAR EL FONDO DE PANTALLA =====
Sub btnAccion_Click
' Iniciar animación del botón
buttonAnimationActive = True
buttonAnimationAlpha = 255
buttonAnimationDirection = -1
ButtonAnimationTimer.Initialize("ButtonAnimationTimer", 100) ' Más lento que la animación del título
ButtonAnimationTimer.Enabled = True

' Cambiar el fondo de pantalla con la imagen actualmente mostrada
SetWallpaper(imgFullScreen.Bitmap)

' Detener animación después de un breve retraso para asegurar que se vea
Sleep(500)
buttonAnimationActive = False
btnAccion.Visible = True ' Asegurar que el botón sea visible al final
CloseFullScreen
End Sub

Sub ButtonAnimationTimer_Tick
If buttonAnimationActive = False Then
ButtonAnimationTimer.Enabled = False
Return
End If

buttonAnimationAlpha = buttonAnimationAlpha + (25 * buttonAnimationDirection)

If buttonAnimationAlpha <= 50 Then
buttonAnimationDirection = 1
Else If buttonAnimationAlpha >= 255 Then
buttonAnimationDirection = -1
End If

btnAccion.Color = Colors.ARGB(buttonAnimationAlpha, 50, 150, 255)
End Sub

Sub SetWallpaper(Bmp As Bitmap)
Try
Dim r As Reflector
r.Target = r.RunStaticMethod("android.app.WallpaperManager", "getInstance", _
Array As Object(r.GetContext), Array As String("android.content.Context"))
r.RunMethod4("setBitmap", Array As Object(Bmp), Array As String("android.graphics.Bitmap"))
ToastMessageShow("Fondo de pantalla cambiado con éxito", False)
Catch
Log("Error al cambiar wallpaper: " & LastException.Message)
ToastMessageShow("Error al cambiar fondo de pantalla", False)
End Try
End Sub

Private Sub WobbleMenu1_Tab5Click

StartActivity("juego")

End Sub
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Actividad (juego.bas)

#Region Project Attributes





#End Region

#Region Activity Attributes
#FullScreen: False
#IncludeTitle: False
#End Region



Sub Process_Globals
Private xui As XUI
Private targetWidth As Int = 200
Private targetHeight As Int = 300
Private fullScreenWidth As Int = 1080
Private fullScreenHeight As Int = 1920
Private isFullScreen As Boolean = False
Private Timer1 As Timer
Private currentImageIndex As Int = 1
Private maxImages As Int = 38
Private ButtonAnimationTimer As Timer
Private buttonAnimationActive As Boolean = False
Private buttonAnimationAlpha As Int = 255
Private buttonAnimationDirection As Int = -1
Private imageSubFolder As String = "juegos"

End Sub

Sub Globals
Private clvImagenes1 As CustomListView
Private imagenes As List
Private pnlFullScreen As Panel
Private btnAccion As Button
Private imgFullScreen As ImageView
Private currentPosition As Int
Private WobbleMenu2 As WobbleMenu

End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("juegos")


'Menu personalizado
WobbleMenu2.SetTabTextIcon(1,"Animales",Chr(0xF1B0),Typeface.FONTAWESOME)
WobbleMenu2.SetTabTextIcon(2,"Ánime",Chr(0xF1D0),Typeface.FONTAWESOME)
WobbleMenu2.SetTabTextIcon(3,"Variado",Chr(0xF0E8),Typeface.FONTAWESOME)
WobbleMenu2.SetTabTextIcon(4,"Tecnología",Chr(0xF2DB),Typeface.FONTAWESOME)
WobbleMenu2.SetTabTextIcon(5,"Juegos",Chr(0xF11B),Typeface.FONTAWESOME)
WobbleMenu2.SetBadge(4,0,Colors.White,Colors.Blue)






' Configurar el fondo de la actividad
Activity.Color = Colors.ARGB(255, 240, 240, 240) ' Gris claro para el fondo

' Verificar que el CustomListView se cargó correctamente
If clvImagenes1.IsInitialized = False Then
Log("Error: CustomListView no se inicializó correctamente")
Return
End If

' Inicialización de componentes
imagenes.Initialize

' Configuración del panel de pantalla completa
pnlFullScreen.Initialize("pnlFullScreen")
imgFullScreen.Initialize("imgFullScreen")
btnAccion.Initialize("btnAccion")

' Configurar propiedades del botón
btnAccion.Text = "Establecer"
btnAccion.TextSize = 16
btnAccion.TextColor = Colors.White
btnAccion.Color = Colors.ARGB(200, 50, 150, 255)

' Añadir vistas al panel
pnlFullScreen.AddView(imgFullScreen, 0, 0, 100%x, 100%y)
pnlFullScreen.AddView(btnAccion, 50%x - 50dip, 90%y - 60dip, 100dip, 50dip)
btnAccion.BringToFront





' Cargar imágenes
LoadImages
End Sub



Sub Activity_Resume
End Sub

Sub Activity_KeyPress (KeyCode As Int) As Boolean
If KeyCode = KeyCodes.KEYCODE_BACK Then
If isFullScreen Then
CloseFullScreen
Return True
End If
End If
Return False
End Sub

Sub LoadImages
Timer1.Initialize("Timer1", 20) ' Reducido el intervalo para carga más rápida
Timer1.Enabled = True
currentImageIndex = 1
End Sub

Sub Timer1_Tick
If currentImageIndex <= maxImages Then
Dim imageName As String = currentImageIndex & ".jpg"
' Cambia esta línea para buscar en la subcarpeta:
If File.Exists(File.DirAssets & "/" & imageSubFolder, imageName) Then
Dim bmp As Bitmap = LoadBitmapSample(File.DirAssets & "/" & imageSubFolder, imageName, targetWidth, targetHeight)
imagenes.Add(bmp)

' Actualizar la galería cada 4 imágenes
If currentImageIndex Mod 4 = 0 Then
CallSubDelayed(Me, "ConfigureGallery")
End If
End If
currentImageIndex = currentImageIndex + 1
Else
Timer1.Enabled = False
CallSubDelayed(Me, "ConfigureGallery")
End If
End Sub

Sub ConfigureGallery
clvImagenes1.Clear
For i = 0 To imagenes.Size - 1 Step 2
Dim pnl As B4XView = xui.CreatePanel("")
pnl.SetLayoutAnimated(0, 0, 0, 100%x, 200dip)

Dim margin As Int = 1%x
Dim imageWidth As Int = (100%x - 3 * margin) / 2
Dim imageHeight As Int = 200dip

' Primera imagen
Dim img1 As ImageView
img1.Initialize("imgItem")
pnl.AddView(img1, margin, 1dip, imageWidth, imageHeight)
img1.Bitmap = imagenes.Get(i)
img1.Tag = i
img1.Gravity = Gravity.FILL

' Segunda imagen (si existe)
If i + 1 < imagenes.Size Then
Dim img2 As ImageView
img2.Initialize("imgItem")
pnl.AddView(img2, 2 * margin + imageWidth, 1dip, imageWidth, imageHeight)
img2.Bitmap = imagenes.Get(i + 1)
img2.Tag = i + 1
img2.Gravity = Gravity.FILL
End If

clvImagenes1.Add(pnl, 210dip)
Next
End Sub

Sub imgItem_Click
Dim v As View = Sender
currentPosition = v.Tag
ShowFullScreenImage(imagenes.Get(currentPosition))
End Sub

Sub ShowFullScreenImage(bmp As Bitmap)
' Verificar inicialización de componentes
If pnlFullScreen.IsInitialized = False Or imgFullScreen.IsInitialized = False Then
Log("Error: Componentes no inicializados")
Return
End If

' Cargar la imagen en alta calidad para pantalla completa
Dim imageName As String = (currentPosition + 1) & ".jpg"
' Cambia esta línea para buscar en la subcarpeta:
If File.Exists(File.DirAssets & "/" & imageSubFolder, imageName) Then
imgFullScreen.Bitmap = LoadBitmapSample(File.DirAssets & "/" & imageSubFolder, imageName, fullScreenWidth, fullScreenHeight)
Else
imgFullScreen.Bitmap = bmp
End If

' Resto del código permanece igual...
imgFullScreen.Gravity = Gravity.FILL

If pnlFullScreen.Parent = Null Then
Activity.AddView(pnlFullScreen, 0, 0, 100%x, 100%y)
End If

pnlFullScreen.Color = Colors.Black
pnlFullScreen.Visible = True
isFullScreen = True
End Sub

Sub CloseFullScreen
' Verificación más robusta
If pnlFullScreen = Null Or pnlFullScreen.IsInitialized = False Then
Log("Panel no inicializado, no se puede cerrar")
isFullScreen = False
Return
End If

' Primero ocultar el panel
pnlFullScreen.Visible = False
isFullScreen = False





' Método 100% seguro para remover la vista
Try
' Verificar si el panel está añadido a la actividad
For i = 0 To Activity.NumberOfViews - 1
Dim v As View = Activity.GetView(i)
If v = pnlFullScreen Then
Activity.RemoveViewAt(i)
Exit
End If
Next
Catch
Log("Error al remover vista: " & LastException.Message)
End Try

' Otra alternativa segura
If pnlFullScreen.IsInitialized Then
pnlFullScreen.Visible = False
End If
End Sub

Sub pnlFullScreen_Touch (Action As Int, X As Float, Y As Float) As Boolean
Return True ' Captura todos los toques y no los deja pasar
End Sub

' ===== FUNCIÓN NUEVA PARA CAMBIAR EL FONDO DE PANTALLA =====
Sub btnAccion_Click
' Iniciar animación del botón
buttonAnimationActive = True
buttonAnimationAlpha = 255
buttonAnimationDirection = -1
ButtonAnimationTimer.Initialize("ButtonAnimationTimer", 100)
ButtonAnimationTimer.Enabled = True

' Cargar la imagen en alta resolución desde la subcarpeta
Dim imageName As String = (currentPosition + 1) & ".jpg"
Dim bmp As Bitmap
If File.Exists(File.DirAssets & "/" & imageSubFolder, imageName) Then
bmp = LoadBitmap(File.DirAssets & "/" & imageSubFolder, imageName)
Else
bmp = imagenes.Get(currentPosition)
End If

' Cambiar el fondo de pantalla
SetWallpaper(bmp)

' Detener animación después de un breve retraso
Sleep(500)
buttonAnimationActive = False
btnAccion.Visible = True
CloseFullScreen
End Sub

Sub ButtonAnimationTimer_Tick
If buttonAnimationActive = False Then
ButtonAnimationTimer.Enabled = False
Return
End If

buttonAnimationAlpha = buttonAnimationAlpha + (25 * buttonAnimationDirection)

If buttonAnimationAlpha <= 50 Then
buttonAnimationDirection = 1
Else If buttonAnimationAlpha >= 255 Then
buttonAnimationDirection = -1
End If

btnAccion.Color = Colors.ARGB(buttonAnimationAlpha, 50, 150, 255)
End Sub

Sub SetWallpaper(Bmp As Bitmap)
Try
Dim r As Reflector
r.Target = r.RunStaticMethod("android.app.WallpaperManager", "getInstance", _
Array As Object(r.GetContext), Array As String("android.content.Context"))
r.RunMethod4("setBitmap", Array As Object(Bmp), Array As String("android.graphics.Bitmap"))
ToastMessageShow("Fondo de pantalla cambiado con éxito", False)
Catch
Log("Error al cambiar wallpaper: " & LastException.Message)
ToastMessageShow("Error al cambiar fondo de pantalla", False)
End Try
End Sub

Private Sub WobbleMenu1_Tab1Click



End Sub
 

josejad

Expert
Licensed User
Longtime User
Hola Roy:

Con el proyecto así, es difícil ayudarte. Súbelo a algún servicio en la nube (dropbox, google drive) y pon el enlace. Supongo que lo pesado serán las imágenes de Wallpaper, también podrías subir el proyecto sin ellas.

Sería bueno también que el asunto del hilo, sea algo descriptivo de tu problema, para que así pueda ayudar a más gente. "Una ayuda" me temo que no es muy descriptivo.

saludos,
 

roy89

Member
Vale tienes razón ahora me pongo a ello la verdad me urge darle solución ya que como soy cubano y el internet es pésimo podré ayudar a muchos para que puedan tener wallpaper en sus teléfonos en cuanto a al proyecto no se porque es el propio proyecto que se lleva como 20 megas las imágenes solo pesan 2 megas y le puse solo 2 para probar
 

roy89

Member
Hola Roy:

Con el proyecto así, es difícil ayudarte. Súbelo a algún servicio en la nube (dropbox, google drive) y pon el enlace. Supongo que lo pesado serán las imágenes de Wallpaper, también podrías subir el proyecto sin ellas.

Sería bueno también que el asunto del hilo, sea algo descriptivo de tu problema, para que así pueda ayudar a más gente. "Una ayuda" me temo que no es muy descriptivo.

saludos,
aqui te envio amigo josejad el proyecto guardado en googledrive espero me puedas ayudar
 

josejad

Expert
Licensed User
Longtime User
Hola Roy:

Tu problema está en usar subcarpetas dentro de la carpeta DirAssets.
Mi consejo sería por ejemplo anteponer la categoría a cada imagen y dejarlas todas en la misma carpeta (ejemplo juegos1.jpg, juegos2.jpg, animales1.jpg, etc...) o bien copiarlas a File.DirInternal.

Creo que te sería también más fácil si usaras B4XPages para el proyecto.

saludos,
 
Top