Fork us on GitHub

Animated Gif Support

And vacation update...
Animated Gif Support

Animated Gif Support

So you know how you write a blog post just before you go on vacation, press publish and never check that it actually got published…​ Funny thing, that’s exactly what I did and the blog post mentioning that I was on "vacation" for a couple of weeks never got published. Anyway, other people have been busy while I was "away" but I got a couple of things done too including animated gif support.

Before we get to that Steve did a lot of work on Mac retina display support. This is a HUGE leap in usability if you use a retina Mac. It makes the iPhone 3gs skin tiny but you can now use the iPhone 5 skin without scaling…​ It looks great and uses the pixels on these Macs really well.

I also released a new cn1lib that implements animated GIF support in Codename One without the resource file hack. It’s still not something I would recommend as animated gifs can be pretty expensive in terms of resources but you can still use it to get a pretty decent animation.

One of the cool things is that this works as a plug in image and you should be able to use it in most places where image works. There are caveats though. E.g. you can’t use it as a native map marker as that image is passed to native. But other than such API’s it should work in labels and even in background image styles, although I would suggest avoiding the latter as it would be a memory/battery drain.

The library is in the extensions section and you can use it like this:

Form hi = new Form("Gif", new BorderLayout());

try {
    hi.add(CENTER, new ScaleImageLabel(GifImage.decode(getResourceAsStream("/giphy-downsized.gif"), 1177720)));
} catch(IOException err) {
    log(err);
}
hi.show();

Notice the following:

  • GifImage.decode can throw an IOException

  • It accepts an InputStream and the length of the input stream so you need to know the size in advance

  • It returns a GifImage which is a subclass of Image

Share this Post:

Posted by Shai Almog

Shai is the co-founder of Codename One. He's been a professional programmer for over 25 years. During that time he has worked with dozens of companies including Sun Microsystems.
For more follow Shai on Twitter & github.