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,115
  • Example.zip
    256.4 KB · Views: 1,769
  • B4XGifView.b4xlib
    1.5 KB · Views: 2,020
  • FLAnimatedImage-Mac.zip
    140 KB · Views: 418
Last edited:

Unobtainius

Active Member
Licensed User
Longtime User
Thanks Erel. I think I tried displaying animated gifs via the webview recently but I may well have given up. So a thumbs up from me.
 

CaptKronos

Active Member
Licensed User
Longtime User
Hi Erel,
I'm getting memory problems if a reasonably large (~1mb) gif is loaded more than once e.g. from rotating the phone running your GifViewExample.
B4X:
b4xgifview_v6 (java line: 118)
java.lang.OutOfMemoryError: Failed to allocate a 1638412 byte allocation with 1032504 free bytes and 1008KB until OOM
    at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
    at android.graphics.Bitmap.nativeCreate(Native Method)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:904)
    at android.graphics.Bitmap.createBitmap(Bitmap.java:928)
    at anywheresoftware.b4a.agraham.gifdecoder.GifDecoder.SetPixels(GifDecoder.java:298)
I have managed to fix the problem by adding to B4XGifView.b4xlib the sub:
B4X:
Public Sub ClearBitmaps
    bitmaps.clear
End Sub
and calling ClearBitmaps before B4XGifView1.SetGif or in Activity_Pause.
 

CaptKronos

Active Member
Licensed User
Longtime User
I haven't changed your GifViewExample apart from loading a larger gif.
B4X:
B4XGifView1.SetGif(File.DirAssets, "giphy.gif")
Then on rotating the phone I get the memory error.
Also, for an artificial example, this gives the memory error:
B4X:
B4XGifView1.SetGif(File.DirAssets, "giphy.gif")
B4XGifView1.SetGif(File.DirAssets, "giphy.gif")
But this is fine:
B4X:
B4XGifView1.SetGif(File.DirAssets, "giphy.gif")
B4XGifView1.ClearBitmaps
B4XGifView1.SetGif(File.DirAssets, "giphy.gif")
 

CaptKronos

Active Member
Licensed User
Longtime User
What I have just realised is that I under-reported the filesize of the gif. It was a 8mb (not 1mb) gif that was causing the problem on a YotaPhone 2 (2 GB RAM). On a Samsung S3, a 1mb gif causes the problem. On a Samsung S8, the 8mb gif is fine.
Do you think it could be just the way I call ClearBitmaps before the class is released, gives the garbage handling more time to run?
 

CaptKronos

Active Member
Licensed User
Longtime User
With the new version I'm no longer seeing the memory problem. Also, with the first version I was seeing some display corruption when showing large animated gifs. This is no longer the case. It seems perfect now.
 

AnandGupta

Expert
Licensed User
Longtime User
v1.11 - GIF image ratio is preserved.
May be it was bug earlier, but I was using it show in full activity area, in any phone size,
gvGIF.SetGif(File.DirAssets, "clouds.gif")

Now the ratio is preserved and does not look good.

How to make it full screen like before without restoring to older library ?

Regards,

Anand
 

Unobtainius

Active Member
Licensed User
Longtime User
preserve aspect ratio is normally a toggle
Is the source available?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User

AnandGupta

Expert
Licensed User
Longtime User
In most cases images look better when the ratio is preserved.
Yes, but here I was using the bug to show full screen, something which looks good in any shape clouds :)

You will need to use the source code and remove the call to ResizeBasedOnImage.
Ahh..the dreaded path..

I know the source is there, but I am still a novice as far as changing library source is concerned. Actually I am afraid as I may dismantle something else. Will give it a try.
Any hint how to add an option so that the library produces both results ?

Regards,

Anand
 
Top