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

来源:互联网 发布:三国志13全武将数据 编辑:程序博客网 时间:2024/05/24 23:14
本文来自:http://blog.csdn.net/hellogv/ ,转载必须注明出处!


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

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


原创粉丝点击