⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 animationdemo.java

📁 人民邮电出版社的《J2ME手机开发入门》全部源代码
💻 JAVA
字号:
/*
 * AnimationDemo.java
 *
 * Created on 2005年4月20日, 下午5:47
 */

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

import java.util.*;

/**
 *
 * @author  Liu Bin
 * @version
 */
public class AnimationDemo extends MIDlet implements CommandListener {
    private Display display;
    private Form form;
    
    //命令按钮
    private Command cmdThread;            //通过线程实现动画
    private Command cmdTimer;             //通过定时器实现动画
    private Command cmdCallSerially;      //通过callSerially实现动画
    private Command cmdExit;              //退出命令按钮
    private Command cmdBack;              //返回主界面命令按钮
    
    //构造方法
    public AnimationDemo() {
        try {
            nbInit();
        } catch(Exception e) {
            e.printStackTrace();
        }
        
    }
    
    //初始化代码
    private void nbInit() throws Exception {
        cmdExit = new Command("退出", Command.EXIT, 0);
        cmdTimer = new Command("通过定时器实现动画", Command.ITEM, 1);
        cmdCallSerially = new Command("通过callSerially实现动画", Command.ITEM, 1);
        cmdThread = new Command("通过线程实现动画", Command.ITEM, 1);
        cmdBack = new Command("返回主界面", Command.ITEM, 1);
        
        form = new Form("简单动画实现演示");
        form.addCommand(cmdTimer);
        form.addCommand(cmdThread);
        form.addCommand(cmdCallSerially);
        form.addCommand(cmdExit);
        form.setCommandListener(this);
    }
    
    public void startApp() {
        display = Display.getDisplay(this);
        display.setCurrent(form);
    }
    
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }
    
    /**
     * 处理命令按钮事件
     */
    public void commandAction(Command cmd, Displayable d) {
        if (cmd == cmdExit) {
            destroyApp(false);
        }
        
        //当按下“通过定时器实现动画”命令按钮,执行以下代码
        if (cmd == cmdTimer) {
            Canvas can = new TimerAnimationCanvas();
            can.addCommand(cmdBack);
            can.setCommandListener(this);
            display.setCurrent(can);
        }
        
        //当按下“通过线程实现动画”命令按钮,执行以下代码
        if (cmd == cmdThread) {
            Canvas can = new ThreadAnimationCanvas();
            can.addCommand(cmdBack);
            can.setCommandListener(this);
            display.setCurrent(can);
        }
        
        //当按下“通过callSerially实现动画”命令按钮,执行以下代码
        if (cmd == cmdCallSerially) {
            Canvas can = new CallSeriallyAnimationCanvas(display);
            can.addCommand(cmdBack);
            can.setCommandListener(this);
            display.setCurrent(can);
        }
        
        //返回主界面
        if (cmd == cmdBack) {
            Displayable dis = display.getCurrent();
            if (dis instanceof AnimationCanves) {
                ((AnimationCanves)dis).stop();
                display.setCurrent(this.form);
                dis = null;
            }
        }
    }
}

class AnimationCanves extends Canvas {
    //屏幕的宽度和高度
    private int canW, canH;
    
    //当前帧索引
    public int curIndex = 0;
    //数组的长度 - 动画的总帧数
    final static int LEN = 6;
    //帧数组
    private Image imgList[];
    
    public AnimationCanves() {
        try {
            imgList = new Image[LEN];
            for (int i=1;i<=LEN;i++) {
                imgList[i-1] = Image.createImage(
                        "/Res/" + i + ".png");
                out(i +".png has been loaded");
            }
        } catch (Exception e) {
            out(e.toString());
        }
        canW =  getWidth();
        canH = getHeight();
        out("canvas width is " + canW);
        out("canvas height is " + canH);
    }
    
    protected void paint(Graphics g) {
        if ((curIndex < 0) || (curIndex >= LEN)) {
            return;
        }
        
        //清除屏幕
        clearScreen(g);
        
        //在屏幕的中央绘制动画
        int x, y, w, h;
        if (imgList[curIndex] != null) {
            w = imgList[curIndex].getWidth();
            h = imgList[curIndex].getHeight();
            x = (canW - w) / 2;
            y = (canH - h) / 2;
            g.drawRegion(imgList[curIndex], 0, 0, w, h,
                    javax.microedition.lcdui.game.Sprite.TRANS_NONE,
                    x, y, Graphics.TOP | Graphics.LEFT);
        }
    }
    
    private void clearScreen(Graphics g) {
        //清除屏幕
        g.setColor(0xFFFFFF);
        g.fillRect(0, 0, canW, canH);
        g.setColor(0x000000);
    }
    
    void out(String msg) {
        System.out.println(msg);
    }
    
    public void stop() {}
}

class ThreadAnimationCanvas extends AnimationCanves implements Runnable {
    private boolean stopFlag = false;
    
    public ThreadAnimationCanvas() {
        new Thread(this).start();
    }
    
    public void run() {
        while (!stopFlag) {
            curIndex ++;
            if (curIndex == LEN) {
                //中间有一个明显的停顿
                curIndex = -5;
            }
            try {
                repaint();
                Thread.sleep(100);
            } catch (Exception e) {
            } 
        }
    }
    
    public void stop() {
        stopFlag = true;
    }
}

class TimerAnimationCanvas extends AnimationCanves {
    //定时器
    private Timer timer = new Timer();
    
    public TimerAnimationCanvas() {
        timer.schedule(new AnimationTimer(), 0, 100 );
    }
    
    public void stop() {
        timer.cancel();
    }
    
    /**
     * 处理动画的定时器任务
     */
    class AnimationTimer extends TimerTask {
        public void run() {
            curIndex ++;
            if (curIndex == AnimationCanves.LEN) {
                //中间有一个明显的停顿
                curIndex = -5;
            }
            repaint();
        }
    }
}

class CallSeriallyAnimationCanvas extends AnimationCanves implements Runnable {
    Display display;
    private boolean stopFlag = false;
    
    public CallSeriallyAnimationCanvas(Display display) {
        this.display = display;
        new Thread(this).start();
    }
    
    public void run() {
        curIndex ++;
        if (curIndex == LEN) {
            //中间有一个明显的停顿
            curIndex = -5;
        }
        try {
            repaint();
            Thread.sleep(100);
        } catch (Exception e) {
        }
        display.callSerially(this);
    }
    
    public void stop() {
        stopFlag = true;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -