TransitionEffect.java
上传用户:luxiaowei
上传日期:2022-06-06
资源大小:58k
文件大小:10k
源码类别:

J2ME

开发平台:

Java

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package com.framework;
  6. import java.util.Vector;
  7. import javax.microedition.lcdui.Graphics;
  8. import javax.microedition.lcdui.Image;
  9. /**
  10.  *
  11.  * @author Swaroop Kumar
  12.  */
  13. public class TransitionEffect  implements TimerInterface {
  14.     protected static final int STATE_RESET = 0;
  15.     protected static final int STATE_IN_PROGRESS = 1;
  16.     private int state;
  17.     public static final int TRANSITION_NO_EFFECT = 0;
  18.     public static final int TRANSITION_BOTTOM_TO_TOP = 1;
  19.     public static final int TRANSITION_LEFT_TO_RIGHT = 2;
  20.     public static final int TRANSITION_RIGHT_TO_LEFT = 3;
  21.     public static final int TRANSITION_LEFT_TO_RIGHT_STEPS = 4;
  22.     public static final int TRANSITION_PIXELS = 5;
  23.     public static final int TRANSITION_STEP_DOWN = 6;
  24.     private int transitionCounterOne;
  25.     private int transitionCounterTwo;
  26.     private static int BLOCK_HEIGHT ;
  27.     private static int PIXELIZATION_BLOCK_HEIGHT;
  28.     private int positions[];
  29.     BaseTimer timer;
  30.     private int width,height;
  31.     boolean waitParm = false;
  32.     private static int screenWidth, screenHeight;
  33.     protected void process(Graphics g,Image buffImage,UiDisplayble displayble)
  34.     {
  35.         switch(displayble.getTransitionEffect())
  36.         {
  37.             case TRANSITION_BOTTOM_TO_TOP:
  38.                 g.drawImage(buffImage, 0, 0, 0);
  39.                 g.translate(0, transitionCounterOne);
  40.                 displayble.paintUI(g);
  41.                 g.translate(0, -transitionCounterOne);
  42.                 transitionCounterOne-= 12;
  43.                 if(transitionCounterOne < -12)
  44.                 {
  45.                     transitionCounterOne = 0;
  46.                     reset();
  47.                 }
  48.             break;
  49.             
  50.             case TRANSITION_LEFT_TO_RIGHT:
  51.                 g.drawImage(buffImage, 0, 0, 0);
  52.                 g.translate(transitionCounterOne, 0);
  53.                 displayble.paintUI(g);
  54.                 g.translate(-transitionCounterOne, 0);
  55.                 transitionCounterOne-= 8;
  56.                 if(transitionCounterOne <= 0)
  57.                 {
  58.                     reset();
  59.                 }
  60.             break;
  61.             
  62.              case TRANSITION_RIGHT_TO_LEFT:
  63.                 g.drawImage(buffImage, 0, 0, 0);
  64.                 g.translate(transitionCounterOne, 0);
  65.                 displayble.paintUI(g);
  66.                 g.translate(-transitionCounterOne, 0);
  67.                 transitionCounterOne+= 8;
  68.                 if(transitionCounterOne >= 0)
  69.                 {
  70.                     reset();
  71.                 }
  72.             break;
  73.            
  74.             case TRANSITION_LEFT_TO_RIGHT_STEPS:
  75.             
  76.                 int startY = 0;
  77.                 for (int i = 0; i < transitionCounterOne; i++) {
  78.                     if(positions[i] == 0)
  79.                     {
  80.                         startY++;
  81.                     }else
  82.                         break;
  83.                 }
  84.                 if(startY > 0)
  85.                 {
  86.                     g.setClip(0, 0,screenWidth, startY * BLOCK_HEIGHT);
  87.                     displayble.paintUI(g);
  88.                 }
  89.                 for (int i = startY; i < transitionCounterOne; i++) {
  90.                     positions[i] -=screenWidth /7;
  91.                     if(positions[i] < 0)
  92.                         positions[i] = 0;
  93.                     g.translate(positions[i], 0);
  94.                     g.setClip(0, i * BLOCK_HEIGHT,screenWidth, BLOCK_HEIGHT);
  95.                     displayble.paintUI(g);
  96.                     g.translate(-positions[i], 0);
  97.                  }
  98.                 g.setClip(0, 0,screenWidth, screenHeight);
  99.                 if(transitionCounterOne < positions.length)
  100.                     transitionCounterOne++;
  101.                
  102.                 if(checkOverCondition(0))
  103.                     reset();
  104.             break;
  105.             
  106.             case TRANSITION_PIXELS:
  107.                 int increment = 4;
  108.                 for (int i = 0 ; i < increment; i++) {
  109.                     if(transitionCounterOne + i >= positions.length)
  110.                         break;
  111.                     int x = ((positions[transitionCounterOne + i]) % width) * PIXELIZATION_BLOCK_HEIGHT;
  112.                     int y = ((positions[transitionCounterOne + i]) / width) * PIXELIZATION_BLOCK_HEIGHT;
  113.                     g.setClip(x, y, PIXELIZATION_BLOCK_HEIGHT, PIXELIZATION_BLOCK_HEIGHT);
  114.                     displayble.paintUI(g);
  115.                     
  116.                 }
  117.                 g.setClip(0, 0,screenWidth, screenHeight);
  118.                 transitionCounterOne += increment;
  119.                 if(transitionCounterOne >= positions.length)
  120.                     reset();
  121.             break;
  122.             
  123.             case TRANSITION_STEP_DOWN:
  124.                 for (int i = 0; i < transitionCounterTwo; i++) {
  125.                     positions[positions.length - 1 - i] += BLOCK_HEIGHT;
  126.                 }
  127.                 if(transitionCounterTwo < positions.length && waitParm)
  128.                     transitionCounterTwo++;
  129.                 waitParm = !waitParm;
  130.                 startY  = 0;
  131.                 for (int i = 0; i < positions.length; i++) {
  132.                     if(positions[positions.length - 1 -i] >= screenHeight - (BLOCK_HEIGHT * (i + 1) ))
  133.                     {
  134.                         positions[positions.length - 1 -i] = screenHeight - (BLOCK_HEIGHT * (i + 1) );
  135.                         startY++;
  136.                     }else
  137.                         break;
  138.                 }
  139.                 if(startY == positions.length)
  140.                     reset();
  141.                 
  142.                 g.setClip(0, 0,screenWidth, screenHeight);
  143.                 g.drawImage(buffImage, 0, 0, 0);
  144.                 if(startY > 0)
  145.                 {
  146.                     g.setClip(0, screenHeight - (BLOCK_HEIGHT * startY),screenWidth, BLOCK_HEIGHT * startY);
  147.                     displayble.paintUI(g);
  148.                 }
  149.                 for (int i = startY; i < positions.length; i++) {
  150.                     if(positions[positions.length - i - 1] > 0)
  151.                     {
  152.                         int targetHeight = screenHeight - (BLOCK_HEIGHT * (i + 1));
  153.                         int translateY = (targetHeight - positions[positions.length - i - 1]);
  154.                         g.translate(0, -translateY);
  155.                         g.setClip(0, positions[positions.length - i - 1],screenWidth, BLOCK_HEIGHT );
  156.                         displayble.paintUI(g);
  157.                         g.translate(0, +translateY);
  158.                     }
  159.                 }
  160.             break;
  161.             
  162.         }
  163.     }
  164.    
  165.     private boolean checkOverCondition(int val)
  166.     {
  167.         boolean over = true;
  168.         for (int i = 0; i < positions.length; i++) {
  169.             if(positions[i] != val)
  170.             {
  171.                 over = false;
  172.                 break;
  173.             }
  174.         }
  175.        return over;
  176.     }
  177.     private void initParm(UiDisplayble displayble)
  178.     {
  179.          screenWidth  = DisplayManager.getInst().getWidth();
  180.         screenHeight = DisplayManager.getInst().getHeight();
  181.         BLOCK_HEIGHT = screenHeight / 15;
  182.         PIXELIZATION_BLOCK_HEIGHT = screenHeight / 9;
  183.        
  184.         switch(displayble.getTransitionEffect())
  185.         {
  186.             case TRANSITION_BOTTOM_TO_TOP:
  187.                 transitionCounterOne = displayble.getHeight();
  188.             break;
  189.             
  190.             case TRANSITION_LEFT_TO_RIGHT:
  191.                 transitionCounterOne =screenWidth;
  192.             break;
  193.             
  194.             case TRANSITION_RIGHT_TO_LEFT:
  195.                  transitionCounterOne = -DisplayManager.getInst().getWidth();
  196.             break;
  197.             
  198.             case TRANSITION_LEFT_TO_RIGHT_STEPS:
  199.                  transitionCounterOne = 1;
  200.                  int val = (screenHeight / BLOCK_HEIGHT) + 1;
  201.                  positions = new int[val];
  202.                  for (int i = 0; i < positions.length; i++) {
  203.                      positions[i] =screenWidth;
  204.                 }
  205.             break;
  206.             case TRANSITION_PIXELS:
  207.                 width = (DisplayManager.getInst().getWidth() / PIXELIZATION_BLOCK_HEIGHT)  + 1;
  208.                 height = (screenHeight / PIXELIZATION_BLOCK_HEIGHT)  + 1;
  209.                 transitionCounterOne = 0;
  210.                 positions = new int[width * height];
  211.                  
  212.                 for (int i = 0; i < positions.length; i++) {
  213.                      positions[i] = i;
  214.                 }
  215.                 for (int i = 0; i < positions.length; i++) {
  216.                     int loc1 = Util.getRandomNumber(0, positions.length - 1);
  217.                     int loc2 = Util.getRandomNumber(0, positions.length - 1);
  218.                     int tmp = positions[loc1];
  219.                     positions[loc1] = positions[loc2];
  220.                     positions[loc2]  = tmp;
  221.                 } 
  222.             break;
  223.             
  224.             case TRANSITION_STEP_DOWN:
  225.                 int size = (screenHeight / BLOCK_HEIGHT);
  226.                 positions = new int[size];
  227.                 transitionCounterOne  = 0;
  228.                 transitionCounterTwo = 1;
  229.                 for (int i = 0; i < positions.length; i++) {
  230.                      positions[i] = 0;
  231.                 }
  232.             break;
  233.             
  234.         }
  235.     }
  236.     protected void init(UiDisplayble displayble)
  237.     {
  238.         if(displayble != null && displayble.getTransitionEffect() != TRANSITION_NO_EFFECT)
  239.         {
  240.             DisplayManager.getInst().setBlockKeyPad(true);
  241.             timer = BaseTimer.schedule(100, 100, this, 0);
  242.             initParm(displayble);
  243.             setState(STATE_IN_PROGRESS);
  244.         }
  245.     }
  246.     private void reset()
  247.     {
  248.         DisplayManager.getInst().setBlockKeyPad(false);
  249.         setState(STATE_RESET);
  250.         if(timer != null)
  251.         {
  252.             timer.cancel();
  253.             timer = null;
  254.         }
  255.         try {
  256.             Thread.sleep(50);
  257.         } catch (Exception e) {
  258.         }
  259.         DisplayManager.getInst().forcePaint();
  260.     }
  261.     protected boolean isOver()
  262.     {
  263.         return getState() == STATE_RESET;
  264.     }
  265.     protected int getState() {
  266.         return state;
  267.     }
  268.     private void setState(int state) {
  269.         this.state = state;
  270.     }
  271.     public void task(int data) {
  272.        DisplayManager.getInst().invalidate();
  273.     }
  274.     
  275. }