1

I want to show a .gif banner in the top of my app. I'm using this code to show the animated field BB Animated GIF Field. The issue is that I want to refresh this banner every minute. I've tried so many things:

  • create a new object, but this doesn't change the banner.

  • create a new animated field and try to replace it....IllegalArgumentException. (I'm trying to change that from inside a Thread, using invokeLater()...I've used invokeAndWait() too)

  • remove this animated field and put a new one (from the invokeLater() or invokeAndWait() -> IllegalException)

  • setting a bitmap to this field. The first animate doesn't show and I can see an image from the other banner but it isn't animated.

Any ideas?

If you need to see some code, I will try to post it tomorrow.

Nate
  • 31,017
  • 13
  • 83
  • 207
Mun0n
  • 4,438
  • 4
  • 28
  • 46

1 Answers1

1

If you're using a minimum BlackBerry OS of 6.0 (or above), the BitmapField class directly supports animated gifs.

If you need to support a lower OS version, then you just need to add a method to your AnimatedGIFField class to swap out the old image, and use a new one:

public void setImage(EncodedImage image) {
    // Call BitmapField#setImage()
    super.setImage(image);

    // Store the image and its dimensions.
    _image = image;
    _width = image.getWidth();
    _height = image.getHeight();
    _currentFrame = 0;

    // Stop the previous thread.
    _animatorThread.stop();

    // Start a new animation thread.
    _animatorThread = new AnimatorThread(this);
    _animatorThread.start();
}

Note that this is a UI operation. So, if you want to update the image from a background thread, make sure to wrap it with a call that puts it on the UI thread. For example:

 final EncodedImage eImage = EncodedImage.getEncodedImageResource("img/banner.gif");
 UiApplication.getUiApplication().invokeLater(new Runnable() {
     public void run() { 
         _animatedBanner.setImage(eImage);
     }
 });

Update: I also notice that the way the AnimatedGIFField class works is not really good multi-threading practice (for stopping the thread). If you really want to make the code better, you could implement this solution, or use the technique I show in this answer.

Read more about this here.

Community
  • 1
  • 1
Nate
  • 31,017
  • 13
  • 83
  • 207