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

📄 threadanimation.java

📁 在eclipse环境下
💻 JAVA
字号:
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;


public class ThreadAnimation extends MIDlet {
    private Display display;
    
    private ThreadAnimationCanvas tac;
    

    
    public ThreadAnimation() {
        super();
        tac = new ThreadAnimationCanvas();
        tac.setFullScreenMode(true);
    }

    protected void startApp() throws MIDletStateChangeException {
        //获得当前MIDlet的Display对象
        display = Display.getDisplay(this); 
        //设置ThreadAnimationCanvas对象为当前显示对象
        display.setCurrent(tac);
    }

    protected void pauseApp() {
        // TODO Auto-generated method stub

    }

    protected void destroyApp(boolean arg0) 
        throws MIDletStateChangeException {
        // TODO Auto-generated method stub

    }

    class ThreadAnimationCanvas extends Canvas  implements Runnable {
        boolean stopFlag = false;
        
        //屏幕的宽度和高度
        private int canW, canH;
        
        //当前帧索引
        public int curIndex = 0;
        //数组的长度 - 动画的总帧数
        final static int LEN = 8;
        //帧数组
        private Image imgList[];
        
        public ThreadAnimationCanvas() {
            //装载动画帧图像
            try {
                imgList = new Image[LEN];
                for (int i=0;i<LEN;i++) {
                    imgList[i] = Image.createImage(
                            "/image" + i + ".jpg");
                    System.out.println("image" + i +".png has been loaded");
                }
            } catch (Exception e) {
                System.out.println("Can not load images: " + e);
            }
            
            canW =  getWidth();
            canH = getHeight();
            System.out.println("canvas width is " + canW);
            System.out.println("canvas height is " + canH);
            
            new Thread(this).start();
        }
        
        public void run() {
            while(!stopFlag) {
                curIndex ++;
                if (curIndex == LEN-1) {
                    //中间有一个明显的停顿
                    curIndex = -2;
                }
                try {
                    repaint();
                    Thread.sleep(400);
                } catch (Exception e) {
                }
            }
            
            //通过callSerially的实现方法
//            curIndex ++;
//            if (curIndex == LEN-1) {
//                //中间有一个明显的停顿
//                curIndex = -2;
//            }
//            try {
//                repaint();
//                Thread.sleep(400);
//            } catch (Exception e) {
//            }
//            display.callSerially(this);
        }
        
        /**
         * 在这个方法中绘制屏幕
         */
        protected void paint(Graphics g) {
            //清除屏幕 - 背景黑色
            g.setColor(0x000000);
            g.fillRect(0, 0, this.getWidth(), this.getHeight());
            
            //在屏幕的中央绘制动画
            int x, y, w, h;
            if (curIndex>=0 && imgList[curIndex] != null) {
                w = imgList[curIndex].getWidth();
                h = imgList[curIndex].getHeight();
                x = (canW - w) / 2;
                y = (canH - h) / 2;
                g.drawImage(imgList[curIndex], x, y, 
                        Graphics.TOP | Graphics.LEFT);
            }
        }
    }
}

⌨️ 快捷键说明

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