B4A Library [B4X] B4XGifView - Cross platform animated gif view

1591098949811.png


I was missing a cross platform, animated gif view, so created one.

Usage is simple:
Add with the visual designer and call SetGif to set the gif file.


The B4i implementation depends on FLAnimatedImage open source project: https://github.com/Flipboard/FLAnimatedImage
The FLAnimatedImage library was uploaded to the hosted builders. If you are using a local Mac then download FLAnimatedImage-Mac and copy the files to the Libs folder.
The B4A implementation is based on this open source project: https://github.com/koral--/android-gif-drawable
The two dependent aars are attached. Copy them to B4A additional libraries folder.

Updates:

1.12 - SetGif2 - Allows loading an animated gif from an array of bytes.
1.11 - GIF image ratio is preserved.
1.10 - New B4A implementation based on Android GIF Drawable. It provides better performance. Note that the Activity_Resume method has been removed. Don't miss the B4A-Dependencies zip file.
 

Attachments

  • B4A-Dependencies.zip
    168.2 KB · Views: 3,140
  • Example.zip
    256.4 KB · Views: 1,793
  • B4XGifView.b4xlib
    1.5 KB · Views: 2,048
  • FLAnimatedImage-Mac.zip
    140 KB · Views: 438
Last edited:

aeric

Expert
Licensed User
Longtime User
On line #52, is this a bug?
B4X:
#Else is B4A

I think it should be:
B4X:
#Else If B4A

B4XGifView (version 1.11):
Sub Class_Globals
    Private mEventName As String 'ignore
    Private mCallBack As Object 'ignore
    Public mBase As B4XView
    Private xui As XUI 'ignore
    Public Tag As Object
    #if B4J
    Private iv As ImageView
    #Else If B4A
    Private iv As ImageView
    Public GifDrawable As JavaObject
    #Else If B4i
    Private AnimatedImageView As B4XView
    #End If
End Sub

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

Public Sub DesignerCreateView (Base As Object, Lbl As Label, Props As Map)
    mBase = Base
    Tag = mBase.Tag
    mBase.Tag = Me
    #if B4J
    iv.Initialize("")
    mBase.AddView(iv, 0, 0, mBase.Width, mBase.Height)
    #Else If B4i
    Dim no As NativeObject
    AnimatedImageView = no.Initialize("FLAnimatedImageView").RunMethod("new", Null)
    mBase.AddView(AnimatedImageView, 0, 0, mBase.Width, mBase.Height)
    Dim iv As ImageView = AnimatedImageView
    iv.ContentMode = iv.MODE_FIT
    #Else If B4A
    iv.Initialize("")
    mBase.AddView(iv, 0, 0, mBase.Width, mBase.Height)
    #End If
End Sub

Public Sub SetGif(Dir As String, FileName As String)
    #if B4i
    Dim image As NativeObject
    image = image.Initialize("FLAnimatedImage").RunMethod("animatedImageWithGIFData:", Array(image.ArrayToNSData(File.ReadBytes(Dir, FileName))))
    Dim no As NativeObject = AnimatedImageView
    no.RunMethod("setAnimatedImage:", Array(image))
    #Else if B4J
    iv.SetImage(xui.LoadBitmap(Dir, FileName))
    iv.PreserveRatio = True
    Dim bmp As B4XBitmap = iv.GetImage
    ResizeBasedOnImage(iv, bmp.Width / bmp.Height)
    #Else is B4A
    Dim GifDrawable As JavaObject
    If Dir = File.DirAssets Then
        Dim b() As Byte = File.ReadBytes(Dir, FileName)
        GifDrawable.InitializeNewInstance("pl.droidsonroids.gif.GifDrawable", Array(b))
    Else
        GifDrawable.InitializeNewInstance("pl.droidsonroids.gif.GifDrawable", Array(File.Combine(Dir, FileName)))
    End If
    iv.Background = GifDrawable
    Dim jo As JavaObject = GifDrawable
    Dim w As Int = jo.RunMethod("getIntrinsicWidth", Null)
    Dim h As Int = jo.RunMethod("getIntrinsicHeight", Null)
    ResizeBasedOnImage(iv, w / h)
    #End If
End Sub

Private Sub ResizeBasedOnImage(xiv As B4XView, BmpRatio As Float)
    Dim viewRatio As Float = mBase.Width / mBase.Height
    Dim Height, Width As Int
    If viewRatio > BmpRatio Then
        Height = mBase.Height
        Width = mBase.Height * BmpRatio
    Else
        Width = mBase.Width
        Height = mBase.Width / BmpRatio
    End If
    xiv.SetLayoutAnimated(0, mBase.Width / 2 - Width / 2, mBase.Height / 2 - Height / 2, Width, Height)
End Sub

Public Sub Base_Resize (Width As Double, Height As Double)
      #if B4A
    iv.SetLayoutAnimated(0, 0, 0, Width, Height)
    #Else If B4J
    If iv.GetImage.IsInitialized Then
        Dim bmp As B4XBitmap = iv.GetImage
        ResizeBasedOnImage(iv, bmp.Width / bmp.Height)
    End If
    #Else If B4i
    AnimatedImageView.SetLayoutAnimated(0, 0, 0, Width, Height)
    #End If
End Sub
 

LucaMs

Expert
Licensed User
Longtime User
I tried to make it work with B4i but no success.
In B4i, when I don't have an image loaded, the click event is fired but once I loaded a Gif file, the click event is not fired.
Erel will probably be able to fix it to work with B4i as well.
I haven't even tried it because I don't have B4i; however, you can see that the ImageView with B4i is specially created so it does not have an event name associated with it.
 

hwatech

Member
Licensed User
Longtime User
This is a very nice thing but I'm wondering if I can use this as a busy-cursor of sorts? I would like to use it as I populate lists from the database and as soon as I start making database calls the animation stops. Is there a way to make this work in the background or make my database access non-blocking? I'm not using anything fancy for database access, just a small sqlite db
 

aeric

Expert
Licensed User
Longtime User
This is a very nice thing but I'm wondering if I can use this as a busy-cursor of sorts? I would like to use it as I populate lists from the database and as soon as I start making database calls the animation stops. Is there a way to make this work in the background or make my database access non-blocking? I'm not using anything fancy for database access, just a small sqlite db
I use B4XGifView for Splash screen when initialize the app settings.
 

jaymagpayo

New Member
I need help with libraries missing on my b4a, I am first time building my apps but they have a bit of a problem because of the issue that I have no b4xgifview libraries in my b4a. pls send me an attached file because I have no money that can upgrade my b4a, I am 4-year students "IT" who want to finish my study.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User

AnandGupta

Expert
Licensed User
Longtime User
1.12 - SetGif2 - Allows loading an animated gif from an array of bytes.
I think it will be more obvious to a developer if the method is named as, say, 'SetGifFromBytes'.
We may forget what SetGif2 does and may have ask in the Forum, I think.
 

Hamied Abou Hulaikah

Well-Known Member
Licensed User
Longtime User
Where is Visible property programmatically? it is important.
How can i hide/show gif image programmatically?
 
Top