I am writing a BlackBerry application that uses a custom title bar. Rather than using the textual based title bar, my application uses an image.

I am having trouble redrawing this title bar once the orientation of the device, such as a BlackBerry Storm or Torch, changes from portrait to landscape. See my code for my titleBar Class below.

Any help would be appreciated! Thanks!!

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);
    }
}
有帮助吗?

解决方案

Solved it!

It was because I was getting the width in the constructor. When the device was reoriented, I would retrieve the saved value which was taken in the constructor.

Here is the fixed code:

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);
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top