staticanimation.java.svn-base

来自「j2me设计的界面包」· SVN-BASE 代码 · 共 552 行 · 第 1/2 页

SVN-BASE
552
字号
/* * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Sun designates this * particular file as subject to the "Classpath" exception as provided * by Sun in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, * CA 95054 USA or visit www.sun.com if you need additional information or * have any questions. */package com.sun.lwuit;import com.sun.lwuit.animations.Animation;import com.sun.lwuit.geom.Dimension;import com.sun.lwuit.geom.Rectangle;import java.io.DataInputStream;import java.io.IOException;import java.util.Vector;/** * An animation with pre-existing  * * @author Shai Almog */public class StaticAnimation extends IndexedImage implements Animation {    private Frame[] frames;        /**     * The current frame doesn't point directly at the frames array since the first frame     *  is the image itself     */    private int currentFrame;        private long animationStartTime;    private int totalAnimationTime;        private boolean loop;        /**     * Invoked with the first frame value     */    StaticAnimation(int width, int height, int[] palette, byte[] data) {        super(width, height, palette, data);    }    /**     * Returns the number of frames in the animation including the initial frame     */    public int getFrameCount() {        // +1 for this image        return frames.length + 1;    }        /**     * The time in which the given frame should appear     *      * @param frame must be a number bigger than -1 and smaller than getFrameCount()     * @return the time in milliseconds for the frame to appear     */    public int getFrameTime(int frame) {        if(frame == 0) {            return 0;        }        return frames[frame - 1].time;    }    /**     * Returns the duration for the entire animation used to determin when to loop     * @return total animation time in milliseconds     */    public int getTotalAnimationTime() {        return totalAnimationTime;    }        /**     * Returns the RGB for the given frame, this method is relatively slow and      * it is recommended that you do not use it for realtime animations.     *      * @param frame must be a number bigger than -1 and smaller than getFrameCount()     * @return ARGB pixels within the given frame     */    public int[] getFrameRGB(int frame) {        if(frame == 0) {            return getRGB();        }        Frame f = frames[frame - 1];        int height = getHeight();        int width = getWidth();        int[] array = new int[width * height];        byte[] imageDataByte = f.getKeyFrame();        int[] palette = getPalette();                for(int line = 0 ; line < height ; line++) {            byte[] lineArray = f.getModifiedRow(line);            if(lineArray != null) {                for(int position = 0 ; position < width ; position++) {                    int i = lineArray[position] & 0xff;                                    array[position + (width * line)] = palette[i];                }            } else {                int currentPos = line * width;                for(int position = 0 ; position < width ; position++) {                    int i = imageDataByte[position + currentPos] & 0xff;                                    array[position + (width * line)] = palette[i];                }            }        }        return array;    }        /**     * Creates an animation from the given stream, this method is used internally     * by Resources normally you should not create static animations manually.     *      * @param data input stream from which the animation is loaded     * @return An instance of a static animation     */    public static StaticAnimation createAnimation(DataInputStream data) throws IOException {        // read the length of the palette;        byte pSize = data.readByte();        int[] palette = new int[pSize & 0xff];        for(int iter = 0 ; iter < palette.length ; iter++) {            palette[iter] = data.readInt();        }        int width = data.readShort();        int height = data.readShort();        int numberOfFrames = data.readByte() & 0xff;        int totalAnimationTime = Math.max(1, data.readInt());        boolean loop = data.readBoolean();        byte[] array = new byte[width * height];        data.readFully(array);                // create the first frame of the animation        StaticAnimation animation = new StaticAnimation(width, height, palette, array);        animation.frames = new Frame[numberOfFrames -1];        animation.totalAnimationTime = totalAnimationTime;        animation.loop = loop;        int currentTime = 0;        byte[] lastKeyframe = array;        Vector rowNumbers = new Vector();        Vector rowValues = new Vector();                // read the rest of the frames in the animation        for(int iter = 1 ; iter < numberOfFrames ; iter++) {            currentTime += data.readInt();                        // if this is a keyframe then just read the data else read the specific            // modified row offsets            if(data.readBoolean()) {                array = new byte[width * height];                data.readFully(array);                animation.frames[iter - 1] = new Frame(currentTime, array);                lastKeyframe = array;                rowValues.removeAllElements();                rowNumbers.removeAllElements();            } else {                boolean drawPrevious = data.readBoolean();                int nextRow = data.readShort();                while(nextRow != -1) {                    byte[] rowData = new byte[width];                    data.readFully(rowData);                    // if this row was already modified in a previous frame we can remove                    // the old row                    Integer rowInteger = new Integer(nextRow);                    int index = rowNumbers.indexOf(rowInteger);                    if(index > -1) {                        rowNumbers.removeElementAt(index);                        rowValues.removeElementAt(index);                    }                                        rowNumbers.addElement(rowInteger);                    rowValues.addElement(rowData);                    nextRow = data.readShort();                }                animation.frames[iter - 1] = new Frame(currentTime, lastKeyframe, rowNumbers, rowValues, drawPrevious);            }        }                return animation;    }        /**     * @inheritDoc     */    public boolean animate() {        if(animationStartTime == 0) {            animationStartTime = System.currentTimeMillis();            return false;        }        long currentTime = System.currentTimeMillis();        int position = (int)(currentTime - animationStartTime);        if(loop) {            position %= totalAnimationTime;        } else {            if(position > totalAnimationTime) {                return false;            }        }                // special case for last frame        if(currentFrame == frames.length) {            if(position >= totalAnimationTime || position < frames[frames.length - 1].getTime()) {                currentFrame = 0;                return true;            }        } else {            if(position >= frames[currentFrame].getTime()) {                currentFrame++;                if(currentFrame > frames.length + 1) {                    currentFrame = 0;                }                return true;            }        }                return false;    }        /**     * Restarts the animation     */    public void restart() {        animationStartTime = System.currentTimeMillis();    }    /**     * @inheritDoc     */    public void paint(Graphics g) {        drawImage(g, 0, 0);    }    /**     * Indicates whether the animation will run in a loop or run only once     */    public boolean isLoop() {        return loop;    }    /**     * Indicates whether the animation will run in a loop or run only once     */    public void setLoop(boolean loop) {        this.loop = loop;    }        /**     * Overriden to allow animation frames     */    void drawImage(Graphics g, int x, int y) {        if(currentFrame == 0) {            super.drawImage(g, x, y);            return;        }        int width = getWidth();                if(lineCache == null || lineCache.length < width) {            lineCache = new int[width];        }

⌨️ 快捷键说明

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