2012-04-27 25 views
-1

我使用這段代碼,如果有人熟悉的話,它來自黑莓知識庫。無論如何,我想知道如何使用這個類來操縱GIF。我可以在屏幕上獲得gif,但它不斷重複,不會消失。任何幫助是極大的讚賞!來自黑莓知識庫的AnimatedGIFField,不知道如何操縱gif

public class AnimatedGIFField extends BitmapField private GIFEncodedImage _image; //要繪製的圖像。 private int _currentFrame; // 當前幀的動畫序列。 private int _width; //圖像的寬度 (背景框架)。 private int _height; //圖像的高度 (背景框架)。 私人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; 
       } 
      } 
     } 
    } 
} 

}

+0

的可能重複http://stackoverflow.com/questions/10354219/how-do-i-make-a-gif-disappear-after-its-done-上黑莓的Java – 2012-04-27 21:02:25

回答

1

不要叫super.paint(graphics),而吸引你需要自己繪製的一切。重新寫你的paint(Graphics graphics)方法象下面這樣:

private boolean isPaint = true; 
protected void paint(Graphics graphics) { 
    if(!isPaint) return; 
// super.paint(graphics);  
    if (_currentFrame == _image.getFrameCount()-1) { 
     graphics.setGlobalAlpha(0); 
     isPaint = false; 
    } 
    graphics.drawImage(
       _image.getFrameLeft(_currentFrame), 
       _image.getFrameTop(_currentFrame), 
       _image.getFrameWidth(_currentFrame), 
       _image.getFrameHeight(_currentFrame), _image, 
       _currentFrame, 0, 0 
      ); 
}