Android Question how we can add click event on the custom gifviewer?

ilan

Expert
Licensed User
Longtime User
Upvote 0

omarruben

Active Member
Licensed User
Longtime User
I got the custom view from https://www.b4x.com/android/forum/threads/custom-view-gifviewer.82104/#content
It uses a panel

B4X:
'v1.20
'Custom View class
#Event: SingleClick


Sub Class_Globals
   Private mEventName As String 'ignore
   Private mCallBack As Object 'ignore
   Private mBase As Panel
   Private Const DefaultColorConstant As Int = -984833 'ignore
   Private WorkIndex As Int
   Private BDGravity As Int
   Private bitmaps As List
   Private totalFrames As Int
   Type BitmapAndDelay (bmp As Bitmap, Delay As Int)
   
End Sub

Public Sub Initialize (Callback As Object, EventName As String)
   mEventName = EventName
   mCallBack = Callback
   
End Sub

Public Sub DesignerCreateView (Base As Panel, Lbl As Label, Props As Map)
   mBase = Base
   mBase.Visible = False
End Sub

Public Sub SetGif (Dir As String, FileName As String)
   'Log("swetgif")
   If Dir = File.DirAssets Then
       File.Copy(File.DirAssets, FileName, File.DirInternal, FileName)
       Dir = File.DirInternal
   End If
   Dim path As String = File.Combine(Dir, FileName)
   'If Main.GifsCache.IsInitialized = False Then Main.GifsCache.Initialize
   'If Main.GifsCache.ContainsKey(path) Then
   '   bitmaps = Main.GifsCache.Get(path)
   'Else
       Dim gd As GifDecoder
       gd.Load(Dir, FileName)
       bitmaps.Initialize
       totalFrames = gd.FrameCount
       'Log(totalFrames)
       For i = 0 To gd.FrameCount - 1
           Dim bd As BitmapAndDelay
           bd.Initialize
           bd.bmp = gd.Frame(i)
           bd.Delay = 0 ' gd.Delay(i)
           bitmaps.Add(bd)
       Next
       'Main.GifsCache.Put(path, bitmaps)
   'End If

   Dim FirstFrame As BitmapAndDelay = bitmaps.Get(0)
   Dim FirstBmp As Bitmap = FirstFrame.bmp
   If FirstBmp.Width < mBase.Width Or FirstBmp.Height < mBase.Height Then
       BDGravity = Gravity.CENTER
   Else
       BDGravity = Gravity.FILL
   End If
   
   BDGravity = Gravity.FILL
   
End Sub

Public Sub Start
   mBase.Visible = True
   WorkIndex = WorkIndex + 1
   ShowImpl(WorkIndex)
End Sub

Public Sub Stop
   mBase.Visible = False
   WorkIndex = WorkIndex + 1
End Sub

Public Sub CallFromResume
   If mBase.Visible Then Start
End Sub

Private Sub ShowImpl(MyWorkIndex As Int)
   Dim FrameIndex As Int = 0
   Dim xx As Int
   'Do While MyWorkIndex = WorkIndex
   For xx=0 To totalFrames-1
       'FrameIndex = (FrameIndex + 1) Mod bitmaps.Size
       Dim bdrawable As BitmapDrawable
       Dim bd As BitmapAndDelay = bitmaps.Get(xx)
       bdrawable.Initialize(bd.bmp)
       bdrawable.Gravity = BDGravity
       mBase.Background = bdrawable
       Sleep(bd.Delay)
   Next
   'Loop
End Sub

Public Sub GetBase As Panel
   Return mBase
End Sub

Private Sub mBase_MouseClicked
   CallSub(mBase, mEventName & "_" & "SingleClick")
End Sub
 
Upvote 0
Top