2013-05-02 60 views
0

我是黑莓開發的新手,並且希望將圖像添加到樣本黑莓應用程序中。我已經嘗試了多個教程,但圖像不顯示在後臺將背景圖像添加到黑莓應用程序不工作

可以任何一個好心告訴我是什麼問題?

/** 
* This class extends the UiApplication class, providing a graphical user 
* interface. 
*/ 
public class Diverse extends UiApplication { 

    private Bitmap backgroundBitmap; 
    private Bitmap fieldBitmap; 


    public static void main(String[] args) { 

     Diverse diverse = new Diverse(); 
     diverse.enterEventDispatcher(); 
    } 

    /** 
    * Creates a new MyApp object 
    */ 
    public Diverse() { 
     //The background image. 
     backgroundBitmap = Bitmap.getBitmapResource("background.png"); 

     MainScreen mainScreen = new MainScreen(); 

     HorizontalFieldManager horizontalFieldManager = new HorizontalFieldManager(HorizontalFieldManager.USE_ALL_WIDTH | HorizontalFieldManager.USE_ALL_HEIGHT){ 

      //Override the paint method to draw the background image. 
      public void paint(Graphics graphics) 
      { 
       //Draw the background image and then call paint. 
       graphics.drawBitmap(0, 0, 240, 240, backgroundBitmap, 0, 0); 
       super.paint(graphics); 
      }    

     }; 

     //The LabelField will show up through the transparent image. 
     LabelField labelField = new LabelField("This is a label"); 

     //A bitmap field with a transparent image. 
     //The background image will show up through the transparent BitMapField image. 
     BitmapField bitmapField = new BitmapField(Bitmap.getBitmapResource("field.png")); 

     //Add the manager to the screen. 
     mainScreen.add(horizontalFieldManager); 

     //Add the fields to the manager. 
     horizontalFieldManager.add(labelField); 
     horizontalFieldManager.add(bitmapField); 

     // Push a screen onto the UI stack for rendering. 
     pushScreen(new DiverseScreen()); 
    } 
} 

DiverseScreen類是

package diverse; 

import net.rim.device.api.ui.container.MainScreen; 

/** 
* A class extending the MainScreen class, which provides default standard 
* behavior for BlackBerry GUI applications. 
*/ 
public final class DiverseScreen extends MainScreen 
{ 
    /** 
    * Creates a new MyScreen object 
    */ 
    public DiverseScreen() 
    {   
     // Set the displayed title of the screen  
     setTitle("Diverse"); 
    } 
} 
+1

看到這個鏈接:http://stackoverflow.com/questions/8831430/splash-screen-image-size/8831780#8831780你會更好理解。 – alishaik786 2013-05-02 06:12:20

回答

1

的問題是,你已經設置背景圖片爲一個屏幕,但你永遠不顯示屏幕。然後,您顯示了不同的屏幕,但沒有而不是有一個背景圖像集。

首先,它有助於理解BlackBerry UI框架。它允許您在屏幕上創建對象的層次結構。在頂層,你有一個Screen(或Screen的子類),然後在Screen裏面,你有Managers,裏面是Fields。但是,每個級別都必須添加到某種容器中,最後,頂級Screen必須以類似pushScreen()的方式顯示。

See a little more description on this hierarchy in a recent answer here

在你的情況,你應該改變這一行

MainScreen mainScreen = new MainScreen(); 

DiverseScreen mainScreen = new DiverseScreen(); 

,然後改變這一行

pushScreen(new DiverseScreen()); 

pushScreen(mainScreen); 

因爲mainScreen是您添加水平字段管理器以繪製背景圖像的實例。

+0

謝謝,你幫了我很多。 – Shahzeb 2013-05-02 17:03:12