2012-08-05 89 views
2

我是BlackBerry Development(5.0)的新手。我在Android應用程序開發方面有一點經驗。 我想要做的是在整個屏幕上填充一個圖像(水平) 類似於你可以在Android中使用fill_parent在佈局文件中完成的操作。 我看了一些論壇找到解決方案,但沒有得到滿意的。BlackBerry Image字段水平填充屏幕

這是我應得我的形象

Bitmap headerLogo = Bitmap.getBitmapResource("uperlogo.png"); 
BitmapField headerLogoField = 
    new BitmapField(headerLogo, BitmapField.USE_ALL_WIDTH | Field.FIELD_HCENTER); 
setTitle(headerLogoField); 

此代碼是給了我在上面標題(如需要),並在該中心。我只是想讓它水平延伸以覆蓋所有空間。

回答

3

在創建BitmapField之前,可以水平拉伸Bitmap,這樣可以解決問題。但是如果延伸到Bitmap並將其作爲標題使用,將會爲支持屏幕旋轉的設備(例如Storm,Torch系列)帶來問題。在這種情況下,您必須保持兩個拉伸的Bitmap實例,一個用於縱向模式,另一個用於橫向模式。而且您還需要編寫一些額外的代碼,以根據方向設置適當的Bitmap。如果你不想這樣做,那麼請檢查下面的方法2:


使用CustomBitmapField例如

一個CustomBitmapField可拉長Bitmap水平都可以使用。檢查實施。

class MyScreen extends MainScreen { 

    public MyScreen() { 
     Bitmap bm = Bitmap.getBitmapResource("uperlogo.png"); 
     setTitle(new CustomBitmapField(bm)); 
    } 

    class CustomBitmapField extends Field { 
     private Bitmap bmOriginal; 
     private Bitmap bm; 

     private int bmHeight; 

     public CustomBitmapField(Bitmap bm) { 
      this.bmOriginal = bm; 
      this.bmHeight = bm.getHeight(); 
     } 

     protected void layout(int width, int height) { 
      bm = new Bitmap(width, bmHeight); 
      bmOriginal.scaleInto(bm, Bitmap.FILTER_BILINEAR); 
      setExtent(width, bmHeight); 
     } 

     protected void paint(Graphics graphics) { 
      graphics.drawBitmap(0, 0, bm.getWidth(), bmHeight, bm, 0, 0); 
     } 
    } 
} 


使用背景實例

Background對象可以很容易地解決這個問題。如果一個Background實例可以設置爲HorizontalFieldManager,它將使用其可用的所有寬度,那麼在屏幕旋轉的情況下,它將處理其大小和背景繪畫。並且Background實例本身會照顧所提供的Bitmap的擴展。檢查下面的代碼。

class MyScreen extends MainScreen { 

    public MyScreen() { 
     setTitle(getMyTitle());   
    } 

    private Field getMyTitle() { 
     // Logo. 
     Bitmap bm = Bitmap.getBitmapResource("uperlogo.png"); 

     // Create a manager that contains only a dummy field that doesn't 
     // paint anything and has same height as the logo. Background of the 
     // manager will serve as the title. 

     HorizontalFieldManager hfm = new HorizontalFieldManager(USE_ALL_WIDTH); 
     Background bg = BackgroundFactory.createBitmapBackground(bm, Background.POSITION_X_LEFT, Background.POSITION_Y_TOP, Background.REPEAT_SCALE_TO_FIT); 
     hfm.setBackground(bg); 
     hfm.add(new DummyField(bm.getHeight())); 

     return hfm; 
    } 

    // Implementation of a dummy field 
    class DummyField extends Field { 
     private int logoHeight; 

     public DummyField(int height) { 
      logoHeight = height; 
     } 

     protected void layout(int width, int height) { 
      setExtent(1, logoHeight); 
     } 

     protected void paint(Graphics graphics) { 
     } 
    } 
} 
+0

「使用後臺實例」實現解決了我的問題。感謝一堆:) – Jazib 2012-08-07 09:20:20