Android Question My first app is slow ..

Himred

Member
Licensed User
Longtime User
Hi !
As a total B4a beguinner I apologize for stupid questions I will have or that are already answered in the guides I try to digest.

I coded my first training thing today it's a simple array of buttons that can have 3 states represented by 3 differents images.

It works, but when running it on my galaxy notes 2 tab it is somehow slow and not very responsive.
Can someone look at the code and tell me what's I'm doing wrong ?
I can't believe the galaxy tab can't handle displaying 120 buttons and updating the background of one of them when clicked.
Or maybe it is still interpreted on my galaxy tab ?

Thanks in advance for any help you can provide.

Also if someone can tell me why when I change the orientation of my tablet my state array is reseted

B4X:
    #Region Module Attributes
    #FullScreen: False
    #IncludeTitle: True
    #ApplicationLabel: MineSweeper
    #VersionCode: 0
    #VersionName:
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

Sub Process_Globals
    Dim sizeX As Int
    Dim sizeY As Int
    sizeX = 10
    sizeY = 12
    Dim BlockX As Int
    Dim BlockY As Int
    BlockX=60
    BlockY=60
End Sub

Sub Globals
    Dim buttons(sizeX,sizeY) As Button
    Dim imgon, imgoff, imgnone As Bitmap
    Dim state(sizeX,sizeY) As Int
End Sub

Sub UpdateButtonBackground (x As Int,y As Int)
    If state(x,y) = 0 Then buttons(x,y).SetBackgroundImage(imgoff)
    If state(x,y) = 1 Then buttons(x,y).SetBackgroundImage(imgon)
    If state(x,y) = 2 Then buttons(x,y).SetBackgroundImage(imgnone)
End Sub

Sub Activity_Create(FirstTime As Boolean)
    imgoff.Initialize(File.DirAssets, "0.png")
    imgon.Initialize(File.DirAssets, "1.png")
    imgnone.Initialize(File.DirAssets, "2.png")
    For x=0 To sizeX-1
        For y=0 To sizeY-1  
            buttons(x,y).Initialize("ButtonEvent")
            buttons(x,y).Tag=y*sizeY+x
            If FirstTime = True Then state(x,y)=0
            Activity.AddView(buttons(x,y), 100+BlockX*x, 100+BlockY*y, BlockX, BlockY)
            UpdateButtonBackground(x,y)
        Next
    Next      
  
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub ButtonEvent_Click
    Dim btn As Button
    btn = Sender
    Dim x,y As Int
    y=Floor(btn.Tag/sizeY)
    x=btn.Tag-y*sizeY
    state(x,y)=state(x,y)+1
    If state(x,y)>2 Then state(x,y)=0
    UpdateButtonBackground(x,y)
    Activity.Title = state(x,y)
End Sub
 

Attachments

  • MineSweeper.zip
    30.8 KB · Views: 282

Himred

Member
Licensed User
Longtime User
Oh you are right, now it's a lot faster !
Sorry for my first (but not the last i guess) stupid question.
Thanks Erel.
 
Upvote 0

Himred

Member
Licensed User
Longtime User
:)
Same here, it's quite a boring game as it is.
Yes the code started 3 hours ago, time to figure how b4a works.
I will finish it as a training to understand the beast.
Still have to figure out a lot of things, like determining the screen resolution, disabling orientation, scaling stuff and so on.
Thanks for your help.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Also if someone can tell me why when I change the orientation of my tablet my state array is reseted
You must Dim state(sizeX,sizeY) As Int in Process_Globals instead of Globals.

You should use dip values instaed of pixels for the dimensions.
 
Upvote 0

Himred

Member
Licensed User
Longtime User
Thanks Klauss, it works as you stated.
Will dig the dip values to understand what they means in the guide.
Regards,
 
Upvote 0
Top