WEB开发网
开发学院软件开发Java J2ME GUI实战之三 ----------LWUIT实现... 阅读

J2ME GUI实战之三 ----------LWUIT实现切换特效

 2009-09-12 00:00:00 来源:WEB开发网   
核心提示:以上第一幅图是窗体切换特效之一;第二幅图是控件切换特效之一以下给出设置特效的代码,这些代码同样来自Sample例子中: 1. /* 2. * Copyright ?2008 Sun Microsystems, Inc. All rights reserved. 3. * Use is subject to licens

J2ME GUI实战之三 ----------LWUIT实现切换特效

J2ME GUI实战之三 ----------LWUIT实现切换特效

以上第一幅图是窗体切换特效之一;第二幅图是控件切换特效之一

以下给出设置特效的代码,这些代码同样来自Sample例子中:

   1. /*
   2.  * Copyright ?2008 Sun Microsystems, Inc. All rights reserved.
   3.  * Use is subject to license terms.
   4.  *
   5.  */
   6. package com.sun.lwuit.uidemo;
   7.
   8. import com.sun.lwuit.Button;
   9. import com.sun.lwuit.ButtonGroup;
  10. import com.sun.lwuit.CheckBox;
  11. import com.sun.lwuit.Command;
  12. import com.sun.lwuit.Component;
  13. import com.sun.lwuit.Container;
  14. import com.sun.lwuit.Dialog;
  15. import com.sun.lwuit.Display;
  16. import com.sun.lwuit.Form;
  17. import com.sun.lwuit.Label;
  18. import com.sun.lwuit.M3G;
  19. import com.sun.lwuit.RadioButton;
  20. import com.sun.lwuit.TextArea;
  21. import com.sun.lwuit.TextField;
  22. import com.sun.lwuit.animations.CommonTransitions;
  23. import com.sun.lwuit.animations.Transition;
  24. import com.sun.lwuit.animations.Transition3D;
  25. import com.sun.lwuit.events.ActionEvent;
  26. import com.sun.lwuit.events.ActionListener;
  27. import com.sun.lwuit.layouts.BoxLayout;
  28. import com.sun.lwuit.layouts.FlowLayout;
  29. import com.sun.lwuit.plaf.Style;
  30.
  31.
  32. /**
  33.  * Transitons between screens
  34.  *
  35.  * @author Shai Almog
  36.  */
  37. public class TransitionDemo extends Demo {
  38.     /**
  39.      * The selected radio button index
  40.      */
  41.     private static int selectedIndex = 0;
  42.
  43.     public String getName() {
  44.         return "Transitions";
  45.     }
  46.
  47.     protected String getHelp() {
  48.         return "Transitions appear when switching from one form to the next, a transition can be bound " +
  49.             "to the operation of exiting or entering the screen. There are default transitions in the toolkit " +
  50.             "and custom transitions are easy to write.";
  51.     }
  52.
  53.     private RadioButton createRB(String label, ButtonGroup g, Form f) {
  54.         RadioButton b = new RadioButton(label);
  55.         Style s = b.getStyle();
  56.         s.setMargin(0, 0, 0, 0);
  57.         s.setBgTransparency(70);
  58.         g.add(b);
  59.         f.addComponent(b);
  60.         return b;
  61.     }
  62.    
  63.     protected void execute(final Form f) {
  64.         f.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
  65.         Label title = new Label("Please select a transition type:");
  66.         title.getStyle().setMargin(0, 0, 0, 0);
  67.         title.getStyle().setBgTransparency(70);
  68.         f.addComponent(title);
  69.
  70.         final ButtonGroup radioButtonGroup = new ButtonGroup();
  71.         createRB("Slide Horizontal", radioButtonGroup, f);
  72.         createRB("Slide Vertical", radioButtonGroup, f);
  73.         createRB("Fade", radioButtonGroup, f);
  74.         if(M3G.isM3GSupported()) {
  75.             createRB("Rotate", radioButtonGroup, f);
  76.             createRB("Fly In", radioButtonGroup, f);
  77.             createRB("Cube", radioButtonGroup, f);
  78.             createRB("Static Rotation", radioButtonGroup, f);
  79.             createRB("Swing Top", radioButtonGroup, f);
  80.             createRB("Swing Bottom", radioButtonGroup, f);
  81.         }
  82.
  83.         radioButtonGroup.setSelected(selectedIndex);
  84.
  85.         final TextField speed = new TextField("500");
  86.         speed.setConstraint(TextArea.NUMERIC);
  87.         speed.setInputModeOrder(new String[] {"123"});
  88.         f.addComponent(createPair("Speed", speed));
  89.
  90.         final Form destination = new Form("Destination");
  91.         destination.addComponent(new Label("This is the transition destination..."));
  92.         destination.addCommand(new Command("Back") {
  93.             public void actionPerformed(ActionEvent evt) {
  94.                 f.show();
  95.             }
  96.         });
  97.        
  98.         final CheckBox highQuality = new CheckBox("High Quality");
  99.         highQuality.setSelected(!Display.getInstance().isLightMode());
 100.         highQuality.getStyle().setBgTransparency(0);
 101.         f.addComponent(highQuality);
 102.         highQuality.addActionListener(new ActionListener() {
 103.             public void actionPerformed(ActionEvent evt) {
 104.                 if(Display.getInstance().isLightMode()) {
 105.                     Dialog.show("Warning", "The device seems a bit weak for high quality rendering, " +
 106.                         "using this mode might crash your device.", "OK", null);
 107.                 }
 108.             }
 109.         });
 110.
 111.         final Button updateButton = new Button("Preview Transition");
 112.         final Button applyButton = new Button("Apply Transition");
 113.         final Button applyMenu = new Button("Apply To Menu");
 114.         final Button applyComponents = new Button("Apply To Components");
 115.         updateButton.setAlignment(Button.CENTER);
 116.         updateButton.getStyle().setPadding(5, 5, 7, 7);
 117.         applyButton.setAlignment(Button.CENTER);
 118.         applyButton.getStyle().setPadding(5, 5, 7, 7);
 119.         ActionListener listener = new ActionListener() {
 120.             public void actionPerformed(ActionEvent ev) {
 121.                 int runSpeed = Integer.parseInt(speed.getText());
 122.                 Transition in, out;//in 表示窗体切入时使用的特效,out表示窗体切出时使用的特性
 123.                 switch (radioButtonGroup.getSelectedIndex()) {
 124.                     case 0: {
 125.                         out = CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, false, runSpeed);
 126.                         in = CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, true, runSpeed);
 127.                         break;
 128.                     }
 129.                     case 1: {
 130.                         out = CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, false, runSpeed);
 131.                         in = CommonTransitions.createSlide(CommonTransitions.SLIDE_VERTICAL, true, runSpeed);
 132.                         break;
 133.                     }
 134.                     case 2: {
 135.                         out = CommonTransitions.createFade(runSpeed);
 136.                         in = CommonTransitions.createFade(runSpeed);
 137.                         break;
 138.                     }
 139.                     case 3:  {
 140.                         out = Transition3D.createRotation(runSpeed, true);
 141.                         in = Transition3D.createRotation(runSpeed, false);
 142.                         break;
 143.                     }
 144.                     case 4:  {
 145.                         out = Transition3D.createFlyIn(runSpeed);
 146.                         in = Transition3D.createFlyIn(runSpeed);
 147.                         break;
 148.                     }
 149.                     case 5:  {
 150.                         out = Transition3D.createCube(runSpeed, true);
 151.                         in = Transition3D.createCube(runSpeed, false);
 152.                         break;
 153.                     }
 154.                     case 6:  {
 155.                         out = Transition3D.createStaticRotation(runSpeed, true);
 156.                         in = Transition3D.createStaticRotation(runSpeed, false);
 157.                         break;
 158.                     }
 159.                     case 7:  {
 160.                         out = Transition3D.createSwingIn(runSpeed);
 161.                         in = out;
 162.                         break;
 163.                     }
 164.                     default:  {
 165.                         out = Transition3D.createSwingIn(runSpeed, false);
 166.                         in = out;
 167.                         break;
 168.                     }
 169.                 }
 170.                 selectedIndex = radioButtonGroup.getSelectedIndex();
 171.                 if(out instanceof Transition3D) {//这里需要设置特效效果,仅对部分特效有效
 172.                     ((Transition3D)out).setHighQualityMode(highQuality.isSelected());
 173.                     ((Transition3D)in).setHighQualityMode(highQuality.isSelected());
 174.                 }
 175.                 if(updateButton == ev.getSource()) {
 176.                     //演示窗体切换特效
 177.                     f.setTransitionOutAnimator(out);
 178.                     f.setTransitionInAnimator(in);
 179.                     destination.show();
 180.                 }
 181.                 else if(applyButton == ev.getSource()) {
 182.                         //设置全部窗体在切换时都是同一个特效,注意是“窗体切换时”
 183.                     f.setTransitionOutAnimator(null);
 184.                     f.setTransitionInAnimator(null);
 185.                     UIDemoMIDlet.setTransition(in, out);
 186.                     }
 187. /*这个函数是UIDemoMIDlet的成员函数
 188.  *  public static void setTransition(Transition in, Transition out) {
 189.         form.setTransitionInAnimator(in);
 190.         form.setTransitionOutAnimator(out);
 191.     }
 192. */
 193.                 else  if(applyMenu == ev.getSource()) {
 194.                         //设置全部菜单都是同一个特效,菜单Menu就是一般在右下角弹出的
 195.                         UIDemoMIDlet.setMenuTransition(in, out);
 196.                 }
 197. /*这个函数是UIDemoMIDlet的成员函数
 198.  *     public static void setMenuTransition(Transition in, Transition out) {
 199.         form.setMenuTransitions(in, out);
 200.         UIManager.getInstance().getLookAndFeel().setDefaultMenuTransitionIn(in);
 201.         UIManager.getInstance().getLookAndFeel().setDefaultMenuTransitionOut(out);
 202.     }
 203.  */
 204.                 else if(applyComponents == ev.getSource()) {
 205.                         //设置控件都是同一个特效,这里的控件可以是Button
 206.                         //当Button被选中时就会显示特效
 207.                         UIDemoMIDlet.setComponentTransition(in);
 208.                 }
 209. /* 这个函数是UIDemoMIDlet的成员函数
 210.  *     public static void setComponentTransition(Transition t) {
 211.         componentTransitions = t;
 212.         form.setSmoothScrolling(false);
 213.     }
 214.  */
 215.             }
 216.         };
 217.         updateButton.addActionListener(listener);
 218.         applyButton.addActionListener(listener);
 219.         applyMenu.addActionListener(listener);
 220.         applyComponents.addActionListener(listener);
 221.
 222.         Container buttonPanel = new Container(new FlowLayout(Component.CENTER));
 223.         buttonPanel.addComponent(updateButton);
 224.         f.addComponent(buttonPanel);
 225.
 226.         buttonPanel = new Container(new FlowLayout(Component.CENTER));
 227.         buttonPanel.addComponent(applyButton);
 228.         f.addComponent(buttonPanel);
 229.
 230.         buttonPanel = new Container(new FlowLayout(Component.CENTER));
 231.         buttonPanel.addComponent(applyMenu);
 232.         f.addComponent(buttonPanel);
 233.
 234.         buttonPanel = new Container(new FlowLayout(Component.CENTER));
 235.         buttonPanel.addComponent(applyComponents);
 236.         f.addComponent(buttonPanel);
 237.     }   
 238. }

Tags:JME GUI 实战

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接