TransitionEffect.java
资源名称:src.zip [点击查看]
上传用户:luxiaowei
上传日期:2022-06-06
资源大小:58k
文件大小:10k
源码类别:
J2ME
开发平台:
Java
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package com.framework;
- import java.util.Vector;
- import javax.microedition.lcdui.Graphics;
- import javax.microedition.lcdui.Image;
- /**
- *
- * @author Swaroop Kumar
- */
- public class TransitionEffect implements TimerInterface {
- protected static final int STATE_RESET = 0;
- protected static final int STATE_IN_PROGRESS = 1;
- private int state;
- public static final int TRANSITION_NO_EFFECT = 0;
- public static final int TRANSITION_BOTTOM_TO_TOP = 1;
- public static final int TRANSITION_LEFT_TO_RIGHT = 2;
- public static final int TRANSITION_RIGHT_TO_LEFT = 3;
- public static final int TRANSITION_LEFT_TO_RIGHT_STEPS = 4;
- public static final int TRANSITION_PIXELS = 5;
- public static final int TRANSITION_STEP_DOWN = 6;
- private int transitionCounterOne;
- private int transitionCounterTwo;
- private static int BLOCK_HEIGHT ;
- private static int PIXELIZATION_BLOCK_HEIGHT;
- private int positions[];
- BaseTimer timer;
- private int width,height;
- boolean waitParm = false;
- private static int screenWidth, screenHeight;
- protected void process(Graphics g,Image buffImage,UiDisplayble displayble)
- {
- switch(displayble.getTransitionEffect())
- {
- case TRANSITION_BOTTOM_TO_TOP:
- g.drawImage(buffImage, 0, 0, 0);
- g.translate(0, transitionCounterOne);
- displayble.paintUI(g);
- g.translate(0, -transitionCounterOne);
- transitionCounterOne-= 12;
- if(transitionCounterOne < -12)
- {
- transitionCounterOne = 0;
- reset();
- }
- break;
- case TRANSITION_LEFT_TO_RIGHT:
- g.drawImage(buffImage, 0, 0, 0);
- g.translate(transitionCounterOne, 0);
- displayble.paintUI(g);
- g.translate(-transitionCounterOne, 0);
- transitionCounterOne-= 8;
- if(transitionCounterOne <= 0)
- {
- reset();
- }
- break;
- case TRANSITION_RIGHT_TO_LEFT:
- g.drawImage(buffImage, 0, 0, 0);
- g.translate(transitionCounterOne, 0);
- displayble.paintUI(g);
- g.translate(-transitionCounterOne, 0);
- transitionCounterOne+= 8;
- if(transitionCounterOne >= 0)
- {
- reset();
- }
- break;
- case TRANSITION_LEFT_TO_RIGHT_STEPS:
- int startY = 0;
- for (int i = 0; i < transitionCounterOne; i++) {
- if(positions[i] == 0)
- {
- startY++;
- }else
- break;
- }
- if(startY > 0)
- {
- g.setClip(0, 0,screenWidth, startY * BLOCK_HEIGHT);
- displayble.paintUI(g);
- }
- for (int i = startY; i < transitionCounterOne; i++) {
- positions[i] -=screenWidth /7;
- if(positions[i] < 0)
- positions[i] = 0;
- g.translate(positions[i], 0);
- g.setClip(0, i * BLOCK_HEIGHT,screenWidth, BLOCK_HEIGHT);
- displayble.paintUI(g);
- g.translate(-positions[i], 0);
- }
- g.setClip(0, 0,screenWidth, screenHeight);
- if(transitionCounterOne < positions.length)
- transitionCounterOne++;
- if(checkOverCondition(0))
- reset();
- break;
- case TRANSITION_PIXELS:
- int increment = 4;
- for (int i = 0 ; i < increment; i++) {
- if(transitionCounterOne + i >= positions.length)
- break;
- int x = ((positions[transitionCounterOne + i]) % width) * PIXELIZATION_BLOCK_HEIGHT;
- int y = ((positions[transitionCounterOne + i]) / width) * PIXELIZATION_BLOCK_HEIGHT;
- g.setClip(x, y, PIXELIZATION_BLOCK_HEIGHT, PIXELIZATION_BLOCK_HEIGHT);
- displayble.paintUI(g);
- }
- g.setClip(0, 0,screenWidth, screenHeight);
- transitionCounterOne += increment;
- if(transitionCounterOne >= positions.length)
- reset();
- break;
- case TRANSITION_STEP_DOWN:
- for (int i = 0; i < transitionCounterTwo; i++) {
- positions[positions.length - 1 - i] += BLOCK_HEIGHT;
- }
- if(transitionCounterTwo < positions.length && waitParm)
- transitionCounterTwo++;
- waitParm = !waitParm;
- startY = 0;
- for (int i = 0; i < positions.length; i++) {
- if(positions[positions.length - 1 -i] >= screenHeight - (BLOCK_HEIGHT * (i + 1) ))
- {
- positions[positions.length - 1 -i] = screenHeight - (BLOCK_HEIGHT * (i + 1) );
- startY++;
- }else
- break;
- }
- if(startY == positions.length)
- reset();
- g.setClip(0, 0,screenWidth, screenHeight);
- g.drawImage(buffImage, 0, 0, 0);
- if(startY > 0)
- {
- g.setClip(0, screenHeight - (BLOCK_HEIGHT * startY),screenWidth, BLOCK_HEIGHT * startY);
- displayble.paintUI(g);
- }
- for (int i = startY; i < positions.length; i++) {
- if(positions[positions.length - i - 1] > 0)
- {
- int targetHeight = screenHeight - (BLOCK_HEIGHT * (i + 1));
- int translateY = (targetHeight - positions[positions.length - i - 1]);
- g.translate(0, -translateY);
- g.setClip(0, positions[positions.length - i - 1],screenWidth, BLOCK_HEIGHT );
- displayble.paintUI(g);
- g.translate(0, +translateY);
- }
- }
- break;
- }
- }
- private boolean checkOverCondition(int val)
- {
- boolean over = true;
- for (int i = 0; i < positions.length; i++) {
- if(positions[i] != val)
- {
- over = false;
- break;
- }
- }
- return over;
- }
- private void initParm(UiDisplayble displayble)
- {
- screenWidth = DisplayManager.getInst().getWidth();
- screenHeight = DisplayManager.getInst().getHeight();
- BLOCK_HEIGHT = screenHeight / 15;
- PIXELIZATION_BLOCK_HEIGHT = screenHeight / 9;
- switch(displayble.getTransitionEffect())
- {
- case TRANSITION_BOTTOM_TO_TOP:
- transitionCounterOne = displayble.getHeight();
- break;
- case TRANSITION_LEFT_TO_RIGHT:
- transitionCounterOne =screenWidth;
- break;
- case TRANSITION_RIGHT_TO_LEFT:
- transitionCounterOne = -DisplayManager.getInst().getWidth();
- break;
- case TRANSITION_LEFT_TO_RIGHT_STEPS:
- transitionCounterOne = 1;
- int val = (screenHeight / BLOCK_HEIGHT) + 1;
- positions = new int[val];
- for (int i = 0; i < positions.length; i++) {
- positions[i] =screenWidth;
- }
- break;
- case TRANSITION_PIXELS:
- width = (DisplayManager.getInst().getWidth() / PIXELIZATION_BLOCK_HEIGHT) + 1;
- height = (screenHeight / PIXELIZATION_BLOCK_HEIGHT) + 1;
- transitionCounterOne = 0;
- positions = new int[width * height];
- for (int i = 0; i < positions.length; i++) {
- positions[i] = i;
- }
- for (int i = 0; i < positions.length; i++) {
- int loc1 = Util.getRandomNumber(0, positions.length - 1);
- int loc2 = Util.getRandomNumber(0, positions.length - 1);
- int tmp = positions[loc1];
- positions[loc1] = positions[loc2];
- positions[loc2] = tmp;
- }
- break;
- case TRANSITION_STEP_DOWN:
- int size = (screenHeight / BLOCK_HEIGHT);
- positions = new int[size];
- transitionCounterOne = 0;
- transitionCounterTwo = 1;
- for (int i = 0; i < positions.length; i++) {
- positions[i] = 0;
- }
- break;
- }
- }
- protected void init(UiDisplayble displayble)
- {
- if(displayble != null && displayble.getTransitionEffect() != TRANSITION_NO_EFFECT)
- {
- DisplayManager.getInst().setBlockKeyPad(true);
- timer = BaseTimer.schedule(100, 100, this, 0);
- initParm(displayble);
- setState(STATE_IN_PROGRESS);
- }
- }
- private void reset()
- {
- DisplayManager.getInst().setBlockKeyPad(false);
- setState(STATE_RESET);
- if(timer != null)
- {
- timer.cancel();
- timer = null;
- }
- try {
- Thread.sleep(50);
- } catch (Exception e) {
- }
- DisplayManager.getInst().forcePaint();
- }
- protected boolean isOver()
- {
- return getState() == STATE_RESET;
- }
- protected int getState() {
- return state;
- }
- private void setState(int state) {
- this.state = state;
- }
- public void task(int data) {
- DisplayManager.getInst().invalidate();
- }
- }