Saturday, July 11, 2009

In order to show GIF images in Blackberry applications you have to disable exporting all images to png format. This can be done from JDE by right clicking the project and going to properties. In resources tab select “Don’t convert image files to png”.

dontpng

Now add the gif image in the project. And add this class with your project.



/*

* AnimatedGIFField.java
*
* © <your company here>, 2003-2008
* Confidential and proprietary.
*/

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;

//A field that displays an animated GIF.

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 class will create a custom field type object. You can add it with a Screen type objects.

The following code will add a GIF image with a Screen type object



AnimatedGIFField testanimated= new AnimatedGIFField((GIFEncodedImage)(GIFEncodedImage.getEncodedImageResource( "loading2.gif" )),AnimatedGIFField.FIELD_LEFT);
//add(answer);
add(testanimated);


You are done. Let me know if you found any difficulties adding it.

9530

13 Comments:

  1. PraveenGoparaju said...
    Hi, i have a problem in adding a image to the project. I am creating a folder at the root dir, Images and in that am giving the required images. the program am giving path as /Images/image.jpg, but it is not retriving that image and is throwing a runtime exception. I changed cheked the option dont convert images to png files also. but am not getting it. so please help me...
    PraveenGoparaju said...
    Hi, i have a problem in adding a image to the project. I am creating a folder at the root dir, Images and in that am giving the required images. the program am giving path as /Images/image.jpg, but it is not retriving that image and is throwing a runtime exception. I changed cheked the option dont convert images to png files also. but am not getting it. so please help me...
    Mohd Arshad said...
    Image is added successfully but the problem is gif image is running very fast.
    PraveenGoparaju said...
    Hi, i found the way to add the animated gif to app screen, but i failed to add other controls to it. when i add controls like buttons, labels they are not adding to my manager. suggest me the way to fix this problem.

    Thanks and Regards.
    nash.bansal said...
    Thank you so very much for this! :D
    Shimul said...
    @nash
    you are most welcome
    hira.sirojudin said...
    useful tutorial....
    thanks a lot
    Rahul said...
    Hello, I can add the image to the screen but still its stationary and not playing the image frames. I did select that check box from project properties but no effect.. :(
    Shimul said...
    @Rahul
    Are you using JDE or Eclipse with JDE, I found that we dont need to put the tick mark no more in my eclipse. Those GIFs are working automatically for me. Just check your codes.
    Zakuska said...
    Thanks for the Class. Works great! However... how do you stop the animation. I add the animated gif called (Progress) to my screen but when I deleted the field it throws an exception saying the code could not be found in the simulator? Help Please
    Ger said...
    thanks!!
    Kimi said...
    Sir my picture is still . Its animation are not working . please guide me ...
    Rishi said...
    Its very interesting post, good job

    Videocon Telecom has slashed the National Roaming rates effective 1st May 2015 as per the TRAI’s guidelines.The drop in tariff is to the extent of 75% on Local and National SMS; 40% on Incoming calls; and 23% & 20% on STD & Local calls resp while roaming.

Post a Comment