loading gif image on above bitmap field in blackberry
I've add splash screen image on bitmapfield when open my app. in thread method i get logo from httpconnection.
After complete the thread execution , i delete the bitmapfield. and load the logos on that screen.
I want show loa开发者_如何学编程ding gif images on above the bitmapfield when executing the thread.
Pls help.
Hi, already i display image on fullscreen using bitmap field. in above bitmap field how to show loading gif image. thats my question.
Go to this link:
http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800505/800345/How_To_-_Display_an_animated_GIF.html?nodeid=1405903&vernum=0
You will get the AnimatedGIFField.java file. Save it with proper name. and write like this....
GIFEncodedImage bitmapImage=(GIFEncodedImage)GIFEncodedImage.getEncodedImageResource("loading.gif");
AnimatedGIFField image_field=new AnimatedGIFField(bitmapImage);
add(image_field);
NOTE: If you are Using this "loading.gif" in >6.0 versions it gives exceptoin. For this you have to rename the file as "loading.agif". Means for 7.0 version you have to use "loading.agif" not "loading.gif". Rename that file and put in the res folder and change the file name as:
GIFEncodedImage.getEncodedImageResource("loading.agif")
If you have any doubts come on stackOverFlow chat room name "Life for Blackberry" to clarify your and our doubts.
Use the following class to load a GIF
file into a BitmapField
,
To call it and display it use the code below:
GIFEncodedImage yourImage =(GIFEncodedImage)GIFEncodedImage.getEncodedImageResource("picture.gif");
AnimatedGIFField yourImageField =new AnimatedGIFField(yourImage);
add(yourImageField);
**
- Class:
**
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;
import net.rim.device.api.ui.Color;
//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;
}
}
}
}
}
}
精彩评论