Tuesday, January 11, 2011

How to programatically animate a gif in blackberry

Hello,

I have searched a lot and found many posts that helped me.I mixed them all to make it of my use.
So first of all thanks to those who helped me in achieving this task.
When i firstly tried to add a gif file i added it as a bitmap field to my screen but it appeared as an static image without any motion of frames.
The post mentioned below may be can help people who want to animate their GIF's

Here you go:



Animate gif image in blackberry

package com.Test;

import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.system.GIFEncodedImage;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.BitmapField;     

      public class AnimatedGIFField extends BitmapField
      {
          private GIFEncodedImage encoded_image;     //The image to draw.
          private int current_frame;          //The current frame
          private AnimatorThread animator_thread;      

          public AnimatedGIFField(GIFEncodedImage image)
          {
              this(image, 0);
          }
          public AnimatedGIFField(GIFEncodedImage image, long style)
          {      
             super(image.getBitmap(), style);
              encoded_image = image;         

              //Start the thread for animation.
              animator_thread = new AnimatorThread(this);
              animator_thread.start();
          }
          protected void paint(Graphics g)
          {
              super.paint(g);      

              //Don't redraw the background if this is the first frame of the GIF
              if (current_frame != 0)
              {
                  //Draw the animation frame.

                  g.drawImage(encoded_image.getFrameLeft(current_frame),
                  encoded_image.getFrameTop(current_frame),
                  encoded_image.getFrameWidth(current_frame),
                  encoded_image.getFrameHeight(current_frame),
                  encoded_image, current_frame, 0, 0);
              }
          }      

          //Stop the animation thread when the screen the field is on is
          //pop off of the display stack.

          protected void onUndisplay()
          {
              animator_thread.stop();
              super.onUndisplay();
          }

          //A thread to handle the animation.

          private class AnimatorThread extends Thread
          {
              private AnimatedGIFField _theField;
              private boolean continue_movement = true;
              private int total_frames;     //The total number of frames in the image.
              private int count;       //The number of times the animation has looped (completed).
              private int total_loops;      //The number of times the animation should loop (set in the image).      

              public AnimatorThread(AnimatedGIFField theField)
              {
                  _theField = theField;
                  total_frames = encoded_image.getFrameCount();
                  total_loops = encoded_image.getIterations();
              }
              public synchronized void stop()
              {
                  continue_movement= false;
              }
              public void run()
              {
                  while(continue_movement)
                  {
                      UiApplication.getUiApplication().invokeAndWait(new Runnable()
                      {
                          public void run()
                          {
                              _theField.invalidate(); // for repainting
                          }
                      });      

                      try
                      {
                          //put current frame in sleep mode before another frame is redrawn
                          sleep(encoded_image.getFrameDelay(current_frame) * 10);
                      }
                      catch (InterruptedException iex){}        

                      //Increment the frame.
                      ++current_frame;      

                      if (current_frame == total_frames)
                      {
                          //Reset back to frame 0 if we have reached the end.
                          current_frame = 0;
                          ++count;      

                          //Check if the animation should continue.

                          if (count == total_loops)
                          {
                              continue_movement= false;
                          }
                      }
                  }
              }
          }
}


How to call above class

GIFEncodedImage image;
EncodedImage encodedImage;
AnimatedGIFField animatedGIF;
 
encodedImage = EncodedImage.getEncodedImageResource("Share.gif");
byte data[] = new byte[4000];
data = encodedImage.getData();
image =(GIFEncodedImage) EncodedImage.createEncodedImage(data,0,data.length);
animatedGIF = new AnimatedGIFField(image);         
add(animatedGIF);          


Highly thankful to those posts that helped me in building this

5 comments:

  1. Hi Swati,

    very nice program.

    ReplyDelete
  2. Have you been able to use setdisplaypicture for bbm avatars and get this working so the avatar displays an animated gif?

    ReplyDelete
  3. Hello swati, please give me your e-mail id .because i am very confused.

    ReplyDelete
  4. Hello swati,
    can u tell me how to display map and my location in blackberry , i m using eclipse plugin for my app?

    ReplyDelete