Advertisement
warodri

BlackBerry - Animated Managers

Mar 3rd, 2012
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.61 KB | None | 0 0
  1. package mypackage;
  2.  
  3. import net.rim.device.api.system.Display;
  4. import net.rim.device.api.ui.Color;
  5. import net.rim.device.api.ui.Field;
  6. import net.rim.device.api.ui.Graphics;
  7. import net.rim.device.api.ui.Manager;
  8. import net.rim.device.api.ui.XYEdges;
  9. import net.rim.device.api.ui.component.ButtonField;
  10. import net.rim.device.api.ui.component.LabelField;
  11. import net.rim.device.api.ui.container.HorizontalFieldManager;
  12. import net.rim.device.api.ui.container.MainScreen;
  13. import net.rim.device.api.ui.container.VerticalFieldManager;
  14.  
  15. public final class MyScreen extends MainScreen
  16. {
  17. // flag which tells when to animate
  18. private boolean animating = false;
  19.  
  20. // dynamic index for the manager you want to animate
  21. private int animatePanelNumber = 0;
  22.  
  23. // change as you please... top value for moving managers
  24. private int topForInnerChilds = 10;
  25.  
  26. public MyScreen()
  27. {
  28. // Set the displayed title of the screen
  29. setTitle("Moving Managers");
  30.  
  31.  
  32. // get the master central manager. the big deal!
  33. HorizontalFieldManager masterManager = getMasterManager( 0, 150 );
  34. add( masterManager );
  35.  
  36. // this is a moving manager
  37. VerticalFieldManager one = getMovingManager( Color.GREEN, 280, 100 );
  38. masterManager.add( one );
  39.  
  40. // this is another moving manager
  41. VerticalFieldManager two = getMovingManager( Color.RED, 300, 100 );
  42. masterManager.add( two );
  43.  
  44. // add some components inside "one"
  45. one.add( getDummyChild("I'm a LabelField", Color.GREEN, 0 ) );
  46. one.add( getDummyChild("Focused LabelField", Color.GRAY, Field.FOCUSABLE ) );
  47.  
  48. // place some buttons for moving manager
  49. add( getButtonsForMovingManagers() );
  50.  
  51. }
  52.  
  53.  
  54.  
  55. /* manager for moving from side to side*/
  56. private VerticalFieldManager getMovingManager(
  57. final int backgroundColor, final int width, final int height )
  58. {
  59. VerticalFieldManager manager = new VerticalFieldManager()
  60. {
  61. protected void sublayout(int maxWidth, int maxHeight)
  62. {
  63. maxWidth = width;
  64. maxHeight = height;
  65. super.sublayout(maxWidth, maxHeight);
  66. setExtent(maxWidth, maxHeight);
  67. }
  68. protected void paintBackground(Graphics g)
  69. {
  70. g.setBackgroundColor( backgroundColor );
  71. g.clear();
  72. super.paintBackground(g);
  73. }
  74. };
  75.  
  76. return manager;
  77. }
  78.  
  79. /* change as you please...
  80. * builds a dummy LabelField fopr showing inside the moving manager */
  81. private LabelField getDummyChild(
  82. String text, final int backgroundColor, long style )
  83. {
  84. LabelField child = new LabelField( text, style )
  85. {
  86. protected void paintBackground(Graphics g)
  87. {
  88. g.setBackgroundColor( backgroundColor );
  89. g.clear();
  90. super.paintBackground(g);
  91. }
  92. };
  93. child.setMargin( new XYEdges( 3, 3, 3, 3 ) );
  94.  
  95. return child;
  96. }
  97.  
  98. /* change as you please...
  99. * these are button for selecting different managers */
  100. private HorizontalFieldManager getButtonsForMovingManagers()
  101. {
  102. HorizontalFieldManager managerButtons = new HorizontalFieldManager(
  103. USE_ALL_WIDTH | FIELD_HCENTER );
  104.  
  105. // activates manager 1
  106. ButtonField button = new ButtonField(" 1 ")
  107. {
  108. protected boolean navigationUnclick(int status, int time)
  109. {
  110. animatePanelNumber = 0;
  111. animating = true;
  112. return true;
  113. }
  114. };
  115. managerButtons.add( button );
  116.  
  117. // activates manager 2
  118. button = new ButtonField(" 2 ")
  119. {
  120. protected boolean navigationUnclick(int status, int time)
  121. {
  122. animatePanelNumber = 1;
  123. animating = true;
  124. return true;
  125. }
  126. };
  127. managerButtons.add( button );
  128.  
  129. return managerButtons;
  130. }
  131.  
  132.  
  133. /* this is the manager which will do all the work - add this to your screen */
  134. private HorizontalFieldManager getMasterManager( long style, final int height )
  135. {
  136. HorizontalFieldManager manager = new HorizontalFieldManager(
  137. Manager.USE_ALL_WIDTH | Manager.FOCUSABLE | Manager.FIELD_HCENTER | style )
  138. {
  139. protected void sublayout(int maxWidth, int maxHeight)
  140. {
  141. // padding between inner managers
  142. int padding = 5;
  143.  
  144. // define manager height
  145. maxHeight = height;
  146. super.sublayout(maxWidth, maxHeight);
  147. setExtent(maxWidth, maxHeight);
  148.  
  149. // move all the childs outside this window
  150. int startingHidingLeft = Display.getWidth();
  151. for ( int i=0; i < getFieldCount(); i++ )
  152. {
  153. Field field = getField( i );
  154. setPositionChild( field,
  155. startingHidingLeft, topForInnerChilds);
  156. startingHidingLeft += ( field.getWidth() + padding );
  157. }
  158.  
  159. // this is the moving animation thread
  160. Thread t = new Thread( new Runnable()
  161. {
  162. public void run()
  163. {
  164. while ( true )
  165. {
  166. if ( animating )
  167. {
  168. Field fieldDeseado = getField( animatePanelNumber );
  169.  
  170. for ( int i=0; i < getFieldCount(); i++ )
  171. {
  172. int leftDeseado = ( Display.getWidth() / 2 )
  173. - ( fieldDeseado.getWidth() / 2 );
  174.  
  175. Field field = getField( i );
  176.  
  177. int left = field.getLeft();
  178. int leftDelFieldDeseado = fieldDeseado.getLeft();
  179.  
  180. if ( leftDelFieldDeseado > leftDeseado )
  181. {
  182. leftDelFieldDeseado --;
  183. left --;
  184.  
  185. }
  186. else
  187. {
  188. leftDelFieldDeseado ++;
  189. left ++;
  190. }
  191.  
  192. // animate!
  193. setPositionChild( field,
  194. left, topForInnerChilds );
  195.  
  196. if ( leftDelFieldDeseado == leftDeseado )
  197. animating = false;
  198.  
  199. // wait...
  200. try { Thread.sleep( 5 ); } catch (
  201. Exception e ) {}
  202.  
  203. // update screen
  204. invalidate();
  205. }
  206.  
  207. }
  208. }
  209.  
  210. }
  211. });
  212. t.start();
  213. }
  214.  
  215. };
  216. return manager;
  217.  
  218. }
  219.  
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement