Android Open Source - android_game_engine Sprite Sheet 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   www.j a  v a  2  s .  com
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.RectF;

/**
 * A SpriteSheetAnimation is an Animation created from a sprite sheet and a rectangle array.<br />
 *
 * @author Cyril Leroux
 *         Created on 03/02/14.
 */
public class SpriteSheetAnimation extends Animation {

    private final Bitmap mSpriteSheet;
    private final Rect[] mFrames;

    /**
     * Constructor taking a sprite sheet.
     *
     * @param spriteSheet The Bitmap of the sprite sheet.
     * @param frames The rectangles to cut the sheet to create the animation.
     * @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 SpriteSheetAnimation(Bitmap spriteSheet, Rect[] frames, float frameDuration, boolean isLoop, AnimationCallback callback) {
        super(frameDuration, isLoop, callback);
        mSpriteSheet = spriteSheet;
        mFrames = frames;
    }

    @Override
    public void draw(Canvas canvas, RectF boundingRect) {
        final Rect src = mFrames[mCurrentFrameId];
        canvas.drawBitmap(mSpriteSheet, src, boundingRect, null);
    }

    @Override
    public Bitmap getBitmap(int frameId) {
        // Prevent out of bounds exceptions.
        if (frameId < 0 || frameId >= mFrames.length) {
            return null;
        }
        final Rect src = mFrames[frameId];
        return Bitmap.createBitmap(mSpriteSheet, src.left, src.top, src.width(), src.height());
    }

    protected int getFrameCount() { return mFrames.length; }

    @Override
    protected float getWidth() { return mFrames[mCurrentFrameId].width(); }

    @Override
    protected float getHeight() { return mFrames[mCurrentFrameId].height(); }
}




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