2013-04-29 22 views
1

我想在我的應用程序頂部顯示一個.gif橫幅。我正在使用此代碼顯示動畫字段BB Animated GIF Field。問題是我想每分鐘刷新一次這個橫幅。我試過這麼多東西:黑莓java,令人耳目一新的自定義動畫橫幅

  • 創建一個新的對象,但這並不改變橫幅。

  • 創建一個新的動畫字段,並嘗試將其替換.... IllegalArgumentException。 (我試圖改變這種狀況從Thread內,使用invokeLater() ...我用invokeAndWait()太)

  • 刪除該動畫領域並提出一個新的(從invokeLater()invokeAndWait() - >IllegalException

  • 將位圖設置爲該字段。第一個動畫不顯示,我可以看到另一個橫幅的圖像,但它不是動畫。

任何想法?

如果你需要看一些代碼,我會盡量明天發佈。

回答

1

如果您使用的是6.0(或更高版本)的最低BlackBerry操作系統,則BitmapFielddirectly supports animated gifs

如果您需要支持更低的OS版本,那麼你只需要一個方法添加到您的AnimatedGIFField類淘汰掉舊圖像,並使用一個新問題:

public void setImage(EncodedImage image) { 
    // Call BitmapField#setImage() 
    super.setImage(image); 

    // Store the image and its dimensions. 
    _image = image; 
    _width = image.getWidth(); 
    _height = image.getHeight(); 
    _currentFrame = 0; 

    // Stop the previous thread. 
    _animatorThread.stop(); 

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

注意,這是一個UI操作。所以,如果你想從後臺線程更新圖像,請確保使用將其放在UI線程上的調用來包裝它。例如:

final EncodedImage eImage = EncodedImage.getEncodedImageResource("img/banner.gif"); 
UiApplication.getUiApplication().invokeLater(new Runnable() { 
    public void run() { 
     _animatedBanner.setImage(eImage); 
    } 
}); 

更新:我也注意到AnimatedGIFField類的工作方式實在不行多線程實踐(停止線程)。如果你真的想讓代碼更好,你可以使用implement this solutionuse the technique I show in this answer

閱讀more about this here

+0

是啊!我正在使用OS 5,並且setImage方法非常完美! 謝謝! – Mun0n 2013-04-30 07:52:03

+0

不客氣! – Nate 2013-04-30 07:52:21