Android Open Source - android_game_engine Animation






From Project

Back to project page android_game_engine.

License

The source code is released under:

GNU General Public License

If you think the Android project android_game_engine listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.es.engine.graphics.animation;
//from  w  w w .  j av a 2  s  .co m
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.RectF;

/**
 * Animation class.<br />
 * An animation is an autonomous object that will display a list of images at a predefine rate.
 *
 * @author Cyril Leroux
 *         Created on 26/09/13.
 */
public abstract class Animation {

    protected static enum State {
        STATE_RUNNING,
        STATE_STOPPING,
        STATE_STOPPED
    }

    private final AnimationCallback mCallback;
    /** True if the animation is supposed to play loop. */
    private final boolean mIsLoop;
    /** The time during which a bitmap is on the screen before proceeding to the next one. */
    private final float mFrameDuration;

    protected int mCurrentFrameId;
    /** The current state of the animation. It can be either one of RUNNING, STOPPING or STOPPED. */
    private State mState;
    /** Time of the last bitmap update. */
    private long mLastUpdate;
    private long mStartTime;

    /**
     * @param frameDuration Frame duration in milliseconds.
     * @param isLoop True if the animation is supposed to play loop.
     * @param callback The object that will be called when the animation ends.
     */
    public Animation(float frameDuration, boolean isLoop, AnimationCallback callback) {

        mCallback = callback;
        mIsLoop = isLoop;
        mFrameDuration = frameDuration;

        mState = State.STATE_RUNNING;
        mStartTime = -1;
    }

    public abstract void draw(Canvas canvas, RectF boundingRect);

    public abstract Bitmap getBitmap(int frameId);

    protected abstract float getWidth();

    protected abstract float getHeight();

    /** @return The number of frames in the animation. */
    protected abstract int getFrameCount();

    public void start() {

        if (mState != State.STATE_RUNNING) {
            mCurrentFrameId = 0;
            mStartTime = System.currentTimeMillis();
            mLastUpdate = mStartTime;
            mState = State.STATE_RUNNING;
        }
    }

    public void stop() {

        if (mState == State.STATE_RUNNING) {
            mState = State.STATE_STOPPING;
        }
    }

    /** Update the current frame id if necessary. */
    public boolean updateFrame() {

        if (mState == State.STATE_STOPPED) {
            return false;
        }

        final float elapsedTime = System.currentTimeMillis() - mLastUpdate;
        final int step = (int) (elapsedTime / mFrameDuration);
        if (step >= 1) {
            incrementImageId(step);
            return true;
        }
        return false;
    }

    private void incrementImageId(int step) {

        if (mState == State.STATE_STOPPED) {
            return;
        }

        // update bitmap id for the next draw
        mCurrentFrameId += step;

        if (mIsLoop && mState != State.STATE_STOPPING) {
            // if current id is greater than max value then wrap
            mCurrentFrameId %= getFrameCount();

        } else if (mCurrentFrameId >= getFrameCount() - 1) {
            // Animation stops when it reaches the last bitmap
            mCurrentFrameId = getFrameCount() - 1;
            mState = State.STATE_STOPPED;
            mStartTime = -1;
            if (mCallback != null) {
                mCallback.onAnimationStopped();
            }
        }

        mLastUpdate = System.currentTimeMillis();
    }

    public boolean isRunning() {
        return (mState == State.STATE_RUNNING) || (mState == State.STATE_STOPPING);
    }

    public long getStartTime() { return mStartTime; }
}




Java Source Code List

com.google.example.games.basegameutils.BaseGameActivity.java
com.google.example.games.basegameutils.GameHelperUtils.java
com.google.example.games.basegameutils.GameHelper.java
com.google.example.games.basegameutils.ScreenUtils.java
org.es.engine.audio.Sound.java
org.es.engine.game_mechanic.DrawingThread.java
org.es.engine.game_mechanic.DrawingView.java
org.es.engine.graphics.animation.AnimationCallback.java
org.es.engine.graphics.animation.Animation.java
org.es.engine.graphics.animation.BitmapAnimation.java
org.es.engine.graphics.animation.SpriteSheetAnimation.java
org.es.engine.graphics.drawable.DrawableElement.java
org.es.engine.graphics.sprite.GenericSprite.java
org.es.engine.graphics.sprite.SpriteSheet.java
org.es.engine.graphics.sprite.Sprite.java
org.es.engine.graphics.utils.DrawTextUtils.java
org.es.engine.graphics.utils.DrawingParam.java
org.es.engine.hud.Button.java
org.es.engine.hud.Control.java
org.es.engine.hud.HUD.java
org.es.engine.hud.Text.java
org.es.engine.hud.ToggleButton.java
org.es.engine.toolbox.pathfinding.Node.java
org.es.engine.toolbox.pathfinding.ShortestPath.java