2

我正在寫一個使用自定義標題欄的BlackBerry應用程序。我的應用程序使用圖像,而不是使用基於文本的標題欄。BlackBerry - 重新定位後更改我的標題欄

一旦設備的方向(如BlackBerry Storm或Torch)從縱向變爲橫向,我很難重繪此標題欄。看到我的代碼爲我的titleBar類下面。

任何幫助,將不勝感激!謝謝!!

import net.rim.device.api.ui.container.MainScreen; 
import net.rim.device.api.ui.component.BitmapField; 
import net.rim.device.api.system.Bitmap; 
import net.rim.device.api.ui.Color; 
import net.rim.device.api.ui.Graphics; 
import net.rim.device.api.ui.*; 
import net.rim.device.api.ui.component.*; 
import net.rim.device.api.ui.container.*; 
import net.rim.device.api.system.*; 

/** 
* Title Bar 
*/ 

public class TitleBar extends Field implements DrawStyle 
{ 
    private int fieldWidth; 
    private int fieldHeight; 
    private int fontColour; 
    private int backgroundColor; 

    private Bitmap bgImage = Bitmap.getBitmapResource("bgtitle.png"); 
    private Bitmap titleImage = Bitmap.getBitmapResource("logotitle.png"); 
    private static final int BACKGROUND_COLOR = 0x00000000; 


    public TitleBar() 
    { 
     super(Field.NON_FOCUSABLE); 
     fieldHeight = titleImage.getHeight(); 
     fieldWidth = Display.getWidth(); 

     //background color is black 
     backgroundColor = BACKGROUND_COLOR;   
    } 

    public void setBackgroundColour(int _backgroundColour) 
    { 
     backgroundColor = _backgroundColour; 
     invalidate(); 
    } 

    protected void layout(int width, int height) 
    { 
     setExtent(getPreferredWidth(), getPreferredHeight()); 
    } 

    public int getPreferredWidth() 
    { 
     return fieldWidth; 
    } 

    public int getPreferredHeight() 
    { 
     return fieldHeight; 
    } 

    protected void paint(Graphics graphics) 
    { 

     int w = this.getPreferredWidth(); 
     int h = this.getPreferredHeight(); 

     int width_of_bg = 10; 
     int paint_position = 0; 

     int screen_width = Display.getWidth(); 
     while(paint_position<screen_width){ 
      graphics.drawBitmap(paint_position, 0, w, h, bgImage, 0, 0); 
      paint_position += width_of_bg; 
     } 

     int marginX = (w- titleImage.getWidth())/2 ; 
     graphics.drawBitmap(marginX, 0, w, h, titleImage, 0, 0); 
    } 
} 
+0

什麼是你遇到的問題?你可以提供一個截圖,或者更詳細地瞭解繪圖問題是什麼? – 2011-02-14 08:12:51

回答

1

解決了!

這是因爲我在構造函數中獲取寬度。當設備重新定向時,我會檢索構造函數中保存的值。

這裏是固定的代碼:

import net.rim.device.api.ui.container.MainScreen; 
import net.rim.device.api.ui.component.BitmapField; 
import net.rim.device.api.system.Bitmap; 
import net.rim.device.api.ui.Color; 
import net.rim.device.api.ui.Graphics; 
import net.rim.device.api.ui.*; 
import net.rim.device.api.ui.component.*; 
import net.rim.device.api.ui.container.*; 
import net.rim.device.api.system.*; 

/** 
* Title Bar 
*/ 

public class TitleBar extends Field implements DrawStyle 
{ 
    private int fieldWidth; 
    private int fieldHeight; 
    private int fontColour; 
    private int backgroundColor; 

    private Bitmap bgImage = Bitmap.getBitmapResource("bgtitle.png"); 
    private Bitmap titleImage = Bitmap.getBitmapResource("logotitle.png"); 
    private static final int BACKGROUND_COLOR = 0x00000000; 


    public TitleBar() 
    { 
     super(Field.NON_FOCUSABLE); 
     fieldHeight = titleImage.getHeight(); 
     fieldWidth = Display.getWidth(); 

     //background color is black 
     backgroundColor = BACKGROUND_COLOR;   
    } 

    public void setBackgroundColour(int _backgroundColour) 
    { 
     backgroundColor = _backgroundColour; 
     invalidate(); 
    } 

    protected void layout(int width, int height) 
    { 
     setExtent(getPreferredWidth(), getPreferredHeight()); 
    } 

    public int getPreferredWidth() 
    { 
     return Display.getWidth(); 
    } 

    public int getPreferredHeight() 
    { 
     return fieldHeight; 
    } 

    protected void paint(Graphics graphics) 
    { 

     int w = this.getPreferredWidth(); 
     int h = this.getPreferredHeight(); 

     int width_of_bg = 10; 
     int paint_position = 0; 

     while(paint_position<w){ 
      graphics.drawBitmap(paint_position, 0, w, h, bgImage, 0, 0); 
      paint_position += width_of_bg; 
     } 

     int marginX = (w- titleImage.getWidth())/2 ; 
     graphics.drawBitmap(marginX, 0, w, h, titleImage, 0, 0); 
    } 
}