OVERLAY Blackberry Custom Horizontal Menu

Thanks to Max in this post , I made a horizontal menu. But now I’m trying to create an overlay menu, I don’t find how to do it ... Let's see what I got first.

So, I have a MapScreen class that displays my map:

public class MapScreen extends MenuScreen

Then I have the MenuScreen class in the same file, which allows me to display a horizontal menu when I press the MENU button:

abstract class MenuScreen extends MainScreen {
boolean mMenuEnabled = false;
CyclicHFManager mMenuManager = null;

public MenuScreen() {
    mMenuManager = new CyclicHFManager();
    mMenuManager.setBorder(BorderFactory.createBevelBorder(new XYEdges(4,
            0, 0, 0), new XYEdges(Color.DARKBLUE, 0, 0, 0), new XYEdges(
            Color.WHITE, 0, 0, 0)));
    mMenuManager.setBackground(BackgroundFactory
            .createLinearGradientBackground(Color.DARKBLUE, Color.DARKBLUE,
                    Color.LIGHTBLUE, Color.LIGHTBLUE));


    for (int i = 0; i < 10; i++) {
        Bitmap nBitmap = new Bitmap(60, 60);
        Graphics g = new Graphics(nBitmap);
        g.setColor(Color.DARKBLUE);
        g.fillRect(0, 0, 60, 60);
        g.setColor(Color.WHITE);
        g.drawRect(0, 0, 60, 60);
        Font f = g.getFont().derive(Font.BOLD, 40);
        g.setFont(f);
        String text = String.valueOf(i);
        g.drawText(text, (60 - f.getAdvance(text)) >> 1, (60 - f
                .getHeight()) >> 1);

        Bitmap fBitmap = new Bitmap(60, 60);
        g = new Graphics(fBitmap);
        g.setColor(Color.DARKBLUE);
        g.fillRect(0, 0, 60, 60);
        g.setColor(Color.GOLD);
        g.drawRect(0, 0, 60, 60);
        g.setFont(f);
        g.drawText(text, (60 - f.getAdvance(text)) >> 1, (60 - f
                .getHeight()) >> 1);

        BitmapButtonField button = new BitmapButtonField(nBitmap, fBitmap);
        button.setCookie(String.valueOf(i));
        button.setPadding(new XYEdges(0, 18, 0, 18));

        button.setChangeListener(new FieldChangeListener() {
            public void fieldChanged(Field field, int context) {
                Dialog.inform("Button # " + (String) field.getCookie());
            }
        });

        mMenuManager.add(button);
    }
}

protected boolean keyDown(int keycode, int time) {
    if (Keypad.KEY_MENU == Keypad.key(keycode)) {
        if (mMenuManager.getManager() != null) {
            delete(mMenuManager);
            mMenuManager.mCyclicTurnedOn = false;
        } else {
            add(mMenuManager);
            mMenuManager.getField(2).setFocus();
            mMenuManager.mCyclicTurnedOn = true;
        }
        return true;
    } else {
        return super.keyDown(keycode, time);
    }
}}

And finally, my menu manager:

public class CyclicHFManager extends HorizontalFieldManager {
int mFocusedFieldIndex = 0;
public boolean mCyclicTurnedOn = false;

public void focusChangeNotify(int arg0) {
    super.focusChangeNotify(arg0);
    if (mCyclicTurnedOn) {
        int focusedFieldIndexNew = getFieldWithFocusIndex();
        if (focusedFieldIndexNew != mFocusedFieldIndex) {
            if (focusedFieldIndexNew - mFocusedFieldIndex > 0)
                switchField(0, getFieldCount() - 1);
            else
                switchField(getFieldCount() - 1, 0);
        }
    } else {
        mFocusedFieldIndex = getFieldWithFocusIndex();
    }
}

private void switchField(int prevIndex, int newIndex) {
    Field field = getField(prevIndex);
    delete(field);
    insert(field, newIndex);
}}

, : MENU, , , , . , , . , , , .

Blackberry - , , , , ...

!

UPDATE

, , . , 4,5. , MenuHostManager

USE_ALL_HEIGHT  

setPositionChild(mMenuManager, 0, 
    Display.getHeight() - mMenuManager.getPreferredHeight());  

. .

:

Bitmap nBitmap = EncodedImage.getEncodedImageResource("menu" + 
    i + ".png").getBitmap();
BitmapButtonField button = new BitmapButtonField(nBitmap, nBitmap);

( , ). !
Paint CyclicHFManager, , BorderFactory BackgroundFactory... , .
, - , BitmapButtonField setBorder, . , ...
, setBorder 4.5? (BTW, setBorder 4.5).
!

2

. 5.0 :

public BitmapButtonField(Bitmap normal, Bitmap focused) {
    super(CONSUME_CLICK);
    mNormal = normal;
    mFocused = focused;
    mWidth = mNormal.getWidth();
    mHeight = mNormal.getHeight();
    setMargin(0, 0, 0, 0);
    setPadding(0, 0, 0, 0);
    setBorder(BorderFactory
            .createSimpleBorder(new XYEdges(0, 0, 0, 0)));
    setBorder(VISUAL_STATE_ACTIVE, BorderFactory
            .createSimpleBorder(new XYEdges(0, 0, 0, 0)));
}

: i45.tinypic.com/4sz85u.jpg

4.5 setBorder ( ):

public BitmapButtonField(Bitmap normal, Bitmap focused) {
    super(CONSUME_CLICK);
    mNormal = normal;
    mFocused = focused;
    mWidth = mNormal.getWidth();
    mHeight = mNormal.getHeight();
    setMargin(0, 0, 0, 0);
    setPadding(0, 0, 0, 0);
}

: i49.tinypic.com/rartz5.jpg

, 5.0, 4.5!

3

,

protected void applyTheme() {

}

, !

, click ( , ). , , BackgroundFactory.createLinearGradientBackground.

, getVisualState()?

4

BitmapButtonField, getVisualState(). , , ( mCurrent):

public class BitmapButtonField extends ButtonField {
Bitmap mNormal;
Bitmap mFocused;
Bitmap mCurrent;

int mWidth;
int mHeight;

public BitmapButtonField(Bitmap normal, Bitmap focused) {
    super(CONSUME_CLICK);
    mNormal = normal;
    mFocused = focused;
    mCurrent = normal;
    mWidth = mNormal.getWidth();
    mHeight = mNormal.getHeight();
    setMargin(0, 0, 0, 0);
    setPadding(0, 0, 0, 0);
}

protected void onFocus(int direction) {
    mCurrent = mFocused;  
    invalidate();
}

protected void onUnfocus() {
    mCurrent = mNormal;
    invalidate();
}

protected void paint(Graphics graphics) {
    Bitmap bitmap = mCurrent;   
    graphics.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(),
            bitmap, 0, 0);
}

protected void applyTheme() {

}

public int getPreferredWidth() {
    return mWidth;
}

public int getPreferredHeight() {
    return mHeight;
}

protected void layout(int width, int height) {
    setExtent(mWidth, mHeight);
}
}

5

. ! ! , ( - , .) . MapScreen.java

protected boolean navigationMovement(int dx, int dy, int status, int time) {
    if (mMenuHostManager.isActive()) {
        return super.navigationMovement(dx, dy, status, time);
    }
    return mapField.navigationMovement(dx, dy, status, time);
}

protected boolean navigationClick(int status, int time) {
    if (mMenuHostManager.isActive()) {
        return super.navigationClick(status, time);
    }
    return true;
}

protected boolean navigationUnclick(int status, int time) {
    if (mMenuHostManager.isActive()) {
        return super.navigationUnclick(status, time);
    }
    return true;
}
+3
1

alt text http://img534.imageshack.us/img534/9679/overlay1o.jpg alt text http://img243.imageshack.us/img243/4176/overlay2.jpg

! , , , . .

:

class Scr extends MenuScreen {
    public Scr() {
        mMenuHostManager.add(new LabelField(
                "Lorem ipsum dolor sit amet, consectetur "
                        + "adipiscing elit. Vestibulum egestas eleifend "
                        + "urna, in dictum erat auctor ac. Integer "
                        + "volutpat ultricies mauris eu accumsan. "
                        + "Quisque at euismod dui. Phasellus "
                        + "faucibus orci id libero interdum a "
                        + "feugiat lacus sodales. Nullam "
                        + "pulvinar vestibulum dolor "
                        + "rhoncus tristique."));
    }
}

class MenuScreen extends MainScreen {
    MenuHostManager mMenuHostManager;

    public MenuScreen() {
        mMenuHostManager = new MenuHostManager();
        add(mMenuHostManager);
    }

    protected boolean keyDown(int keycode, int time) {
        if (Keypad.KEY_MENU == Keypad.key(keycode)) {
            mMenuHostManager.toggleMenu();
            return true;
        } else {
            return super.keyDown(keycode, time);
        }
    }
}

class MenuHostManager extends HorizontalFieldManager {
    CyclicHFManager mMenuManager = null;

    public MenuHostManager() {
        super(USE_ALL_WIDTH | USE_ALL_HEIGHT);
        mMenuManager = new CyclicHFManager();
        mMenuManager.setBorder(BorderFactory.createBevelBorder(
                new XYEdges(4, 0, 0, 0), new XYEdges(Color.DARKBLUE, 0, 0,
                        0), new XYEdges(Color.WHITE, 0, 0, 0)));
        mMenuManager.setBackground(BackgroundFactory
                .createSolidTransparentBackground(Color.DARKBLUE, 100));

        for (int i = 0; i < 10; i++) {
            Bitmap nBitmap = new Bitmap(60, 60);
            Graphics g = new Graphics(nBitmap);
            g.setColor(Color.DARKBLUE);
            g.fillRect(0, 0, 60, 60);
            g.setColor(Color.WHITE);
            g.drawRect(0, 0, 60, 60);
            Font f = g.getFont().derive(Font.BOLD, 40);
            g.setFont(f);
            String text = String.valueOf(i);
            g.drawText(text, (60 - f.getAdvance(text)) >> 1, (60 - f
                    .getHeight()) >> 1);

            Bitmap fBitmap = new Bitmap(60, 60);
            g = new Graphics(fBitmap);
            g.setColor(Color.DARKBLUE);
            g.fillRect(0, 0, 60, 60);
            g.setColor(Color.GOLD);
            g.drawRect(0, 0, 60, 60);
            g.setFont(f);
            g.drawText(text, (60 - f.getAdvance(text)) >> 1, (60 - f
                    .getHeight()) >> 1);

            BitmapButtonField button = new BitmapButtonField(nBitmap,
                    fBitmap);
            button.setCookie(String.valueOf(i));
            button.setPadding(new XYEdges(0, 18, 0, 18));

            button.setChangeListener(new FieldChangeListener() {
                public void fieldChanged(Field field, int context) {
                    Dialog
                            .inform("Button # "
                                    + (String) field.getCookie());
                }
            });

            mMenuManager.add(button);
        }
    }

    protected void sublayout(int width, int height) {
        super.sublayout(width, height);
        if (mMenuManager.getManager() != null) {
            layoutChild(mMenuManager, mMenuManager.getPreferredWidth(),
                    mMenuManager.getPreferredHeight());
            setPositionChild(mMenuManager, 0, 0);
        }

    }

    public void toggleMenu() {
        if (mMenuManager.getManager() != null) {
            delete(mMenuManager);
            mMenuManager.mCyclicTurnedOn = false;
        } else {
            add(mMenuManager);
            updateLayout();
            mMenuManager.getField(2).setFocus();
            mMenuManager.mCyclicTurnedOn = true;
        }
    }
}

class CyclicHFManager extends HorizontalFieldManager {
    int mFocusedFieldIndex = 0;
    public boolean mCyclicTurnedOn = false;

    public void focusChangeNotify(int arg0) {
        super.focusChangeNotify(arg0);
        if (mCyclicTurnedOn) {
            int focusedFieldIndexNew = getFieldWithFocusIndex();
            if (focusedFieldIndexNew != mFocusedFieldIndex) {
                if (focusedFieldIndexNew - mFocusedFieldIndex > 0)
                    switchField(0, getFieldCount() - 1);
                else
                    switchField(getFieldCount() - 1, 0);
            }
        } else {
            mFocusedFieldIndex = getFieldWithFocusIndex();
        }
    }

    private void switchField(int prevIndex, int newIndex) {
        Field field = getField(prevIndex);
        delete(field);
        insert(field, newIndex);
    }
}

class BitmapButtonField extends ButtonField {
    Bitmap mNormal;
    Bitmap mFocused;
    Bitmap mActive;

    int mWidth;
    int mHeight;

    public BitmapButtonField(Bitmap normal, Bitmap focused) {
        super(CONSUME_CLICK);
        mNormal = normal;
        mFocused = focused;
        mWidth = mNormal.getWidth();
        mHeight = mNormal.getHeight();
        setMargin(0, 0, 0, 0);
        setPadding(0, 0, 0, 0);
        setBorder(BorderFactory
                .createSimpleBorder(new XYEdges(0, 0, 0, 0)));
        setBorder(VISUAL_STATE_ACTIVE, BorderFactory
                .createSimpleBorder(new XYEdges(0, 0, 0, 0)));
    }

    protected void paint(Graphics graphics) {
        Bitmap bitmap = null;
        switch (getVisualState()) {
        case VISUAL_STATE_NORMAL:
            bitmap = mNormal;
            break;
        case VISUAL_STATE_FOCUS:
            bitmap = mFocused;
            break;
        case VISUAL_STATE_ACTIVE:
            bitmap = mFocused;
            break;
        default:
            bitmap = mNormal;
        }
        graphics.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(),
                bitmap, 0, 0);
    }

    public int getPreferredWidth() {
        return mWidth;
    }

    public int getPreferredHeight() {
        return mHeight;
    }

    protected void layout(int width, int height) {
        setExtent(mWidth, mHeight);
    }
}
0

Source: https://habr.com/ru/post/1748909/


All Articles