Question

J'écris une application BlackBerry qui utilise une barre de titre personnalisée. Plutôt que d'utiliser la barre de titre textuelle, mon application utilise une image.

J'ai du mal à redessiner cette barre de titre une fois que l'orientation de l'appareil, comme une tempête de mûre ou une torche, passe du portrait au paysage. Voir mon code pour ma classe TitleBar ci-dessous.

Toute aide serait appréciée! Merci!!

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);
    }
}
Était-ce utile?

La solution

Résolu!

C'était parce que j'obtenais la largeur dans le constructeur. Lorsque l'appareil a été réorienté, je récupérais la valeur enregistrée qui a été prise dans le constructeur.

Voici le code fixe:

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);
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top