Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 26
Registered: ‎10-29-2009
My Device: Not Specified

Animated GIF?

Hello:  I would like to use an animated gif as the background for a custom button.  I am calling it as follows:

 

Bitmap _onPicture = Bitmap.getBitmapResource(filename);

 

where filename is an animated gif.  Doesn't work.  Are animated gifs supported and how to implement?

 

thanks

Developer
Posts: 343
Registered: ‎02-23-2009
My Device: 8700 | 8310 | BOLD | STORM

Re: Animated GIF?

Robotuner,

 

You will have to implement animation on your own as animated GIFs are not supported.  GIFs aren't really recommended either.  The preferred file format for blackberry (and many other mobile platforms including iPhone) is PNG.

Developer
Posts: 26
Registered: ‎10-29-2009
My Device: Not Specified

Re: Animated GIF?

Is there such a thing as an animated png?  Didnt know that.

Developer
Posts: 343
Registered: ‎02-23-2009
My Device: 8700 | 8310 | BOLD | STORM

Re: Animated GIF?

No. There is no animated PNG.  You just have to store multiple frames in one file..or different files (this is less efficient, though) and reference the location of the frame you want to display on the screen.

Developer
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified

Re: Animated GIF?

The following code helps you to generate the Animated gifs....

 

 

package test;

import net.rim.device.api.system.GIFEncodedImage;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
  
public class AnimatedGIFField extends BitmapField
{
private GIFEncodedImage _image; //The image to draw.
private int _currentFrame; //The current frame in the animation sequence.
private int _width; //The width of the image (background frame).
private int _height; //The height of the image (background frame).
private AnimatorThread _animatorThread;
  

public AnimatedGIFField(GIFEncodedImage image)
{
this(image, 0);
}
  

public AnimatedGIFField(GIFEncodedImage image, long style)
{
//Call super to setup the field with the specified style.
//The image is passed in as well for the field to
//configure its required size.
super(image.getBitmap(), style);
  

//Store the image and it's dimensions.
_image = image;
_width = image.getWidth();
_height = image.getHeight();
  

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

protected void paint(Graphics graphics)
{
//Call super.paint. This will draw the first background
//frame and handle any required focus drawing.
super.paint(graphics);
  

//Don't redraw the background if this is the first frame.
if (_currentFrame != 0)
{
//Draw the animation frame.
graphics.drawImage(_image.getFrameLeft(_currentFrame), _image.getFrameTop(_currentFrame),
_image.getFrameWidth(_currentFrame), _image.getFrameHeight(_currentFrame), _image, _currentFrame, 0, 0);
}
}
  

//Stop the animation thread when the screen the field is on is
//popped off of the display stack.
protected void onUndisplay()
{
_animatorThread.stop();
super.onUndisplay();
}
  

//A thread to handle the animation.
private class AnimatorThread extends Thread
{
private AnimatedGIFField _theField;
private boolean _keepGoing = true;
private int _totalFrames; //The total number of frames in the image.
private int _loopCount; //The number of times the animation has looped (completed).
private int _totalLoops; //The number of times the animation should loop (set in the image).
  

public AnimatorThread(AnimatedGIFField theField)
{
_theField = theField;
_totalFrames = _image.getFrameCount();
_totalLoops = _image.getIterations();
  

}
  

public synchronized void stop()
{
_keepGoing = false;
}
  

public void run()
{
while(_keepGoing)
{
//Invalidate the field so that it is redrawn.
UiApplication.getUiApplication().invokeAndWait(new Runnable()
{
public void run()
{
_theField.invalidate();
}
});
  

try
{
//Sleep for the current frame delay before
//the next frame is drawn.
sleep(_image.getFrameDelay(_currentFrame) * 10);
}
catch (InterruptedException iex)
{} //Couldn't sleep.
  

//Increment the frame.
++_currentFrame;
  

if (_currentFrame == _totalFrames)
{
//Reset back to frame 0 if we have reached the end.
_currentFrame = 0;
  

++_loopCount;
  

//Check if the animation should continue.
if (_loopCount == _totalLoops)
{
_keepGoing = false;
}
}
}
}
}
}

 

 

 

this is thw way to call this.....

in your screen class....

loadimg = new AnimatedGIFField(((GIFEncodedImage)GIFEncodedImage.getEncodedImageResource("img/loadingbar.gif"))); 
 			
this.add(loadimg);


where loadingbar.gif is an animated gif.

Note: you can develope your animated gifs using plazamic composer

 

 

 

 

 

Thanks and Regards,
PraveenGoparaju.
New Developer
Posts: 6
Registered: ‎06-17-2009
My Device: Not Specified

Re: Animated GIF?

Hi Blanc,

I'm using the AnimatedGIFField class but I have a problem when I add this field and then a new thread start.

In that case, the GIF doesn't "play".

Do you know why this happened?

Thanks in advance.

Developer
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified

Re: Animated GIF?

Let me what happen in detail. It is working fine for me. can you post a piece of code of yours???

Thanks and Regards,
PraveenGoparaju.
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Animated GIF?

The code you have been supplied with came from this KB article:

 

How To - Display an animated GIF
Article Number: DB-00652
http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800505/800345/...

I have had some problems with this code correctly displaying gifs - it seems that there are gifs this will work with and gifs that it won't.  I have posted a workaround and some additional information on the java Development forum.  I suggest that you search there for more details.  You will also find that more developers look at the Java Developer forum so you will get more response if you ask questions like this on that board.

http://supportforums.blackberry.com/t5/Java-Development/bd-p/java_dev

New Contributor
Posts: 3
Registered: ‎06-19-2010
My Device: 8320
My Carrier: Developer

Re: Animated GIF?

Thank you very much. 

New Member
Posts: 1
Registered: ‎10-12-2011
My Device: Bold 9700
My Carrier: Rogers

Re: Animated GIF?

Hi,

 

I realize this is an old thread but I was trying to use an animated gif myself and kept running into the long way of doing it, i.e, drawing frame by frame in a loop etc...

 

Granted I am using version 6.0 of the OS, so this may not apply to earlier versions, I have not checked, but there is a very simple way of displaying an animated image under this version. You can find it in the "BitmapDemo" sample app:

 

// Create an EncodedImage object to contain an animated gif resource.

EncodedImage encodedImage = EncodedImage.getEncodedImageResource("animation.gif");

 

// Create a BitmapField to contain the animation

BitmapField bitmapFieldAnimation = new BitmapField();

bitmapFieldAnimation.setImage(encodedImage);

 

add(bitmapFieldAnimation).

 

...works like a charm Man Happy