views:

302

answers:

1

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 have 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?

A: 

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);
    }
}
Max Gontar
So what you're saying is that for every given BitmapField, I should use that Field's content rect (i.e. Extent) and then when getX and getY falls under that Field's rect on the left-hand side of it's HFM, I should increment setHorizontalScroll? Is there any chance you can provide a touchEvent code sample, where you have a HFM containing BitmapFields and how I could use getX and getY when a BitmapField's Extent falls under the left-hand side of it's parent HFM?
Diego Tori
sure, see update
Max Gontar
Hey Max, thanks for the code. I'm starting to understand how to get the finger to detect that HFM and it's BitmapField, since from the looks of it, it'll get whatever field from the left and top side of the HFM. However, Suppose I have 10 of them, and I have an index value set to the total number of BitmapFields that goes up and down depending on horizontal scroll, and I wanted to get them from the horizontal center of the HFM via a defined focus area that has a width to contain my BMF in. Would creating an XYPoint using the difference of the field's width and my focus area's width suffice?
Diego Tori
In other words, out of all of the BitmapFields displayed in that strip on the screen, how would I zero in on the middle-most currently displayed bitmapfield?
Diego Tori
You can use http://www.blackberry.com/developers/docs/4.5.0api/net/rim/device/api/ui/Manager.html#getHorizontalScroll(), Field.getExtend() and Manager.getVisibleWidth() to find out which field is in the middle.
Max Gontar
Maybe it would be better to put some arrows before and after HorizontalFieldManager for scrolling image-by-image. And If you click some BitmapField, just center it, whatever fields it take to scroll through.
Max Gontar
I see. So by creating an XYPoint with the HFM's current visible width, I can essentially hone into the Fields currently visible? That would be more ideal instead of me trying to get more sophisticated by trying to get the center most field. Also, the code detecting whether or not I have clicked on a Field, can it also be adapted to where it does something if I swipe my finger to scroll and a field's extend passes through a fixed point on the screen?
Diego Tori
Sure, see http://www.devx.com/wireless/Article/42432/1954
Max Gontar