Android Question Reduce Gif Image size

KZero

Active Member
Licensed User
Longtime User
try this code
needs 2 external libraries
-GIFDecoder
-GIFEncoder

B4X:
Sub ResizeGIF
    Dim EncGIF As AnimatedGifEncoder
    Dim DecGIF As GifDecoder
    Dim dir As String =rp.GetSafeDirDefaultExternal("") '<< replace with dir=File.DirDefaultExternal if you don't wanna use runtime permissions

    DecGIF.Load(File.DirAssets,"test.gif")'<< original gif path

    Dim out As OutputStream
    out = File.OpenOutput(dir, "resized.gif", False) '<< resized gif path
    EncGIF.Initialize("gif")
    EncGIF.Quality=10
    EncGIF.start(out)
    
    Dim b As Bitmap
    For i = 0 To DecGIF.FrameCount-1
        b=DecGIF.Frame(i)
        b=b.Resize(150,150,True)
        EncGIF.addFrame(b)
        EncGIF.Delay=DecGIF.Delay(i)
    Next
    EncGIF.Repeat=DecGIF.LoopCount
    EncGIF.finish
    out.Close
    ToastMessageShow("GIF RESIZED",True)
End Sub

i didn't test it well
 
Last edited:
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
try this code
needs 2 external libraries
-GIFDecoder
-GIFEncoder

B4X:
Sub ResizeGIF
    Dim EncGIF As AnimatedGifEncoder
    Dim DecGIF As GifDecoder
    Dim dir As String =rp.GetSafeDirDefaultExternal("") '<< replace with dir=File.DirDefaultExternal if you don't wanna use runtime permissions

    DecGIF.Load(File.DirAssets,"test.gif")'<< original gif path

    Dim out As OutputStream
    out = File.OpenOutput(dir, "resized.gif", False) '<< resized gif path
    EncGIF.Initialize("gif")
    EncGIF.Quality=10
    EncGIF.start(out)
   
    Dim b As Bitmap
    For i = 0 To DecGIF.FrameCount-1
        b=DecGIF.Frame(i)
        b=b.Resize(150,150,True)
        EncGIF.addFrame(b)
        EncGIF.Delay=DecGIF.Delay(i)
    Next
    EncGIF.Repeat=DecGIF.LoopCount
    EncGIF.finish
    out.Close
    ToastMessageShow("GIF RESIZED",True)
End Sub

i didn't test it well

Worked! Thanks my friend!

Later I'll create a library from this in order to automatically resize gifs. A implementation that I'm already adding to this code is to count the number of frames and, if greater that a number, cutting some of them in destination gif helping also to reduce the target gif size...
I'll post here any modifications.

Thanks again!!!!
 
Upvote 0
Top