Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package mypackage;
- import net.rim.device.api.system.Display;
- import net.rim.device.api.ui.Color;
- import net.rim.device.api.ui.Field;
- import net.rim.device.api.ui.Graphics;
- import net.rim.device.api.ui.Manager;
- import net.rim.device.api.ui.XYEdges;
- import net.rim.device.api.ui.component.ButtonField;
- import net.rim.device.api.ui.component.LabelField;
- import net.rim.device.api.ui.container.HorizontalFieldManager;
- import net.rim.device.api.ui.container.MainScreen;
- import net.rim.device.api.ui.container.VerticalFieldManager;
- public final class MyScreen extends MainScreen
- {
- // flag which tells when to animate
- private boolean animating = false;
- // dynamic index for the manager you want to animate
- private int animatePanelNumber = 0;
- // change as you please... top value for moving managers
- private int topForInnerChilds = 10;
- public MyScreen()
- {
- // Set the displayed title of the screen
- setTitle("Moving Managers");
- // get the master central manager. the big deal!
- HorizontalFieldManager masterManager = getMasterManager( 0, 150 );
- add( masterManager );
- // this is a moving manager
- VerticalFieldManager one = getMovingManager( Color.GREEN, 280, 100 );
- masterManager.add( one );
- // this is another moving manager
- VerticalFieldManager two = getMovingManager( Color.RED, 300, 100 );
- masterManager.add( two );
- // add some components inside "one"
- one.add( getDummyChild("I'm a LabelField", Color.GREEN, 0 ) );
- one.add( getDummyChild("Focused LabelField", Color.GRAY, Field.FOCUSABLE ) );
- // place some buttons for moving manager
- add( getButtonsForMovingManagers() );
- }
- /* manager for moving from side to side*/
- private VerticalFieldManager getMovingManager(
- final int backgroundColor, final int width, final int height )
- {
- VerticalFieldManager manager = new VerticalFieldManager()
- {
- protected void sublayout(int maxWidth, int maxHeight)
- {
- maxWidth = width;
- maxHeight = height;
- super.sublayout(maxWidth, maxHeight);
- setExtent(maxWidth, maxHeight);
- }
- protected void paintBackground(Graphics g)
- {
- g.setBackgroundColor( backgroundColor );
- g.clear();
- super.paintBackground(g);
- }
- };
- return manager;
- }
- /* change as you please...
- * builds a dummy LabelField fopr showing inside the moving manager */
- private LabelField getDummyChild(
- String text, final int backgroundColor, long style )
- {
- LabelField child = new LabelField( text, style )
- {
- protected void paintBackground(Graphics g)
- {
- g.setBackgroundColor( backgroundColor );
- g.clear();
- super.paintBackground(g);
- }
- };
- child.setMargin( new XYEdges( 3, 3, 3, 3 ) );
- return child;
- }
- /* change as you please...
- * these are button for selecting different managers */
- private HorizontalFieldManager getButtonsForMovingManagers()
- {
- HorizontalFieldManager managerButtons = new HorizontalFieldManager(
- USE_ALL_WIDTH | FIELD_HCENTER );
- // activates manager 1
- ButtonField button = new ButtonField(" 1 ")
- {
- protected boolean navigationUnclick(int status, int time)
- {
- animatePanelNumber = 0;
- animating = true;
- return true;
- }
- };
- managerButtons.add( button );
- // activates manager 2
- button = new ButtonField(" 2 ")
- {
- protected boolean navigationUnclick(int status, int time)
- {
- animatePanelNumber = 1;
- animating = true;
- return true;
- }
- };
- managerButtons.add( button );
- return managerButtons;
- }
- /* this is the manager which will do all the work - add this to your screen */
- private HorizontalFieldManager getMasterManager( long style, final int height )
- {
- HorizontalFieldManager manager = new HorizontalFieldManager(
- Manager.USE_ALL_WIDTH | Manager.FOCUSABLE | Manager.FIELD_HCENTER | style )
- {
- protected void sublayout(int maxWidth, int maxHeight)
- {
- // padding between inner managers
- int padding = 5;
- // define manager height
- maxHeight = height;
- super.sublayout(maxWidth, maxHeight);
- setExtent(maxWidth, maxHeight);
- // move all the childs outside this window
- int startingHidingLeft = Display.getWidth();
- for ( int i=0; i < getFieldCount(); i++ )
- {
- Field field = getField( i );
- setPositionChild( field,
- startingHidingLeft, topForInnerChilds);
- startingHidingLeft += ( field.getWidth() + padding );
- }
- // this is the moving animation thread
- Thread t = new Thread( new Runnable()
- {
- public void run()
- {
- while ( true )
- {
- if ( animating )
- {
- Field fieldDeseado = getField( animatePanelNumber );
- for ( int i=0; i < getFieldCount(); i++ )
- {
- int leftDeseado = ( Display.getWidth() / 2 )
- - ( fieldDeseado.getWidth() / 2 );
- Field field = getField( i );
- int left = field.getLeft();
- int leftDelFieldDeseado = fieldDeseado.getLeft();
- if ( leftDelFieldDeseado > leftDeseado )
- {
- leftDelFieldDeseado --;
- left --;
- }
- else
- {
- leftDelFieldDeseado ++;
- left ++;
- }
- // animate!
- setPositionChild( field,
- left, topForInnerChilds );
- if ( leftDelFieldDeseado == leftDeseado )
- animating = false;
- // wait...
- try { Thread.sleep( 5 ); } catch (
- Exception e ) {}
- // update screen
- invalidate();
- }
- }
- }
- }
- });
- t.start();
- }
- };
- return manager;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement