开发者

Blackberry custom slideshow-style BitmapField manager

Right now, I'm trying to figure out how to implement the following:

Suppose I have a custom Manager that has about 10 or so BitmapFields layed out in a horizontal manner (similar to a slideshow contained in a HFM ) . What I want to achieve is to be able to move the image HFM via touchEvent horizontally, where a BitmapField would take focus on the left-hand side of the custom Manager. In other words, will I hav开发者_高级运维e to give a value to setHorizontalScroll and if so, is it a matter of just incrementing that value when the user makes a left or right touch event. Also, how can I get the focus of a Field within a given position on the screen (i.e. the left-most Field on the HFM) when the HFM is scrolling sideways via touchEvent?


1 - yes, setHorizontalScroll should work, don't forget to use HORIZONTAL_SCROLL in manager constructor

2 - try to test each Field getContentRect() for EventTouch getX(int) and getY(int)

UPDATE

To simplify global field position calculation use

    public XYPoint getGlobalXY(Field field) {
        XYPoint result = new XYPoint(field.getLeft(), field.getTop());
        if (field.getManager() != null) {
            result.translate(getGlobalXY(field.getManager()));
        }
        return result;
    }

Thread safe message dialog:

    public void showMessage(final String message) {
        UiApplication.getUiApplication().invokeLater(new Runnable() {
            public void run() {
                Dialog.inform(message);
            }
        });

    }

Sample code:

class Scr extends MainScreen {
    HorizontalFieldManager hfm;    
    public Scr() {
        add(new LabelField("asdfsad"));
        hfm = new HorizontalFieldManager(HORIZONTAL_SCROLL);
        for (int i = 0; i < 5; i++) {
            Bitmap bmp = new Bitmap(100, 100);
            Graphics g = Graphics.create(bmp);
            g.setFont(g.getFont().derive(100));
            String txt = String.valueOf(i);
            int x = g.getFont().getAdvance(txt);
            g.drawText(txt, x, 0);
            BitmapField bf = new BitmapField(bmp);
            hfm.add(bf);
        }
        add(hfm);
    }        

    protected boolean touchEvent(TouchEvent message) {
        if (message.getEvent() == TouchEvent.CLICK) {
            int x = message.getX(1);
            int y = message.getY(1);
            XYRect r = hfm.getExtent();
            r.setLocation(getGlobalXY(hfm));    
            if (r.contains(x, y)) {
                XYRect rf = hfm.getField(2).getExtent();
                rf.setLocation(getGlobalXY(hfm.getField(2)));    
                if (x < rf.x) {
                    showMessage("left side");
                } else if (x > rf.X2()) {
                    showMessage("right side");
                } else {
                    showMessage("field");
                }
            }
        }
        return super.touchEvent(message);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜