在黑莓上面的位图字段上加载gif图像

时间:2011-09-30 04:22:26

标签: blackberry

打开我的应用时,我在位图字段上添加了启动画面图像。在线程方法我从httpconnection获取徽标。

完成线程执行后,我删除了bitmapfield。并在该屏幕上加载徽标。

我想在执行线程时在位图字段上方显示加载gif图像。

请帮助。

嗨,我已经使用位图字段在全屏显示图像了。在上面的位图字段中如何显示加载gif图像。这就是我的问题。

2 个答案:

答案 0 :(得分:4)

转到此链接:

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800505/800345/How_To_-_Display_an_animated_GIF.html?nodeid=1405903&vernum=0

您将获得 AnimatedGIFField.java 文件。用正确的名称保存。并写这样....

GIFEncodedImage bitmapImage=(GIFEncodedImage)GIFEncodedImage.getEncodedImageResource("loading.gif");
AnimatedGIFField image_field=new AnimatedGIFField(bitmapImage);
add(image_field);

注意:如果您在> 6.0版本中使用此“ loading.gif ”,则会提供exceptoin。为此,您必须将文件重命名为“ loading.agif ”。 7.0版本的手段你必须使用“loading.agif”而不是“loading.gif”。重命名该文件并放入res文件夹并将文件名更改为:

  

GIFEncodedImage.getEncodedImageResource( “loading.agif”)

如果您对stackOverFlow聊天室名称“黑莓生活”有任何疑问,请澄清您和我们的疑虑。

答案 1 :(得分:1)

使用以下类将GIF文件加载到BitmapField, 要调用它并显示它,请使用以下代码:

GIFEncodedImage yourImage =(GIFEncodedImage)GIFEncodedImage.getEncodedImageResource("picture.gif");
AnimatedGIFField yourImageField =new AnimatedGIFField(yourImage);
add(yourImageField);

**

  • 类别:

**

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;
                    }
                }
            }
        }
    }
}