黑莓定制OVERLAY水平菜单

时间:2010-06-07 19:50:50

标签: user-interface blackberry menu custom-controls overlay

感谢Max this post,我做了一个横向菜单。但现在我正在尝试制作叠加菜单,我不知道怎么做...让我们看看我先得到了什么。

所以,我有一个类MapScreen来显示我的地图:

public class MapScreen extends MenuScreen

然后,我在同一个文件中有这样的MenuScreen类,当我按下MENU键时,它允许显示水平菜单:

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);
    }
}}

最后我的菜单经理:

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键时,菜单出现,我可以在按钮之间导航,当我再次按下相同的键时它会消失。唯一的问题是我的菜单没有覆盖我的地图,它推动了内容。我尝试使用菜单管理器,就像在第一个响应中一样,调整内容管理器的大小,但结果相同。

Max给了我链接Blackberry - fields layout animation这样做,但我真的不知道如何使用它来使它在我的项目中工作......

感谢您的帮助!

更新

这很好用,这就是我想要的。但是,我仍然有一个问题,因为我在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);

它也有效(现在没有翻转,后来)。太棒了! 我还覆盖了我的CyclicHFManager的Paint方法以获得背景颜色,因为我无法使用BorderFactory和BackgroundFactory ......我的菜单栏现在有颜色,所以没关系。
然后,由于缺少这两个类,在我的BitmapButtonField中,我不得不删除改变边框的2个setBorder函数。而现在我的按钮非常像带边框的普通按钮...... 如何在4.5下与setBorder函数产生相同的效果? (顺便说一句,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以下,删除了2个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() {

}

没有更多边框,看起来很棒!

我现在需要关注我的翻转,然后点击动作(当我在模拟器上点击它时,我没有对话框)。我也成功改变了我的菜单管理器的背景,我画了一张图片,因为我不能使用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;
}

1 个答案:

答案 0 :(得分:0)

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

嗨! Screen有自己的内容管理器,遗憾的是我们无法在屏幕级别上进行自定义子布局。您必须将菜单移动到单独的管理器中。

查看完整代码:

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);
    }
}