Android Open Source - android_game_engine Bitmap 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  a  v  a  2 s .  c o m
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.RectF;

/**
 * A BitmapAnimation is an Animation created from a bitmap array.
 *
 * @author Cyril Leroux
 *         Created on 03/02/14.
 */
public class BitmapAnimation extends Animation {

    private final Bitmap[] mFrames;

    /**
     * Constructor taking a bitmap array.
     *
     * @param bitmaps The animation bitmaps.
     * @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 BitmapAnimation(Bitmap[] bitmaps, float frameDuration, boolean isLoop, AnimationCallback callback) {
        super(frameDuration, isLoop, callback);
        mFrames = bitmaps;
    }

    /**
     * Constructor that will load a bitmap array from the resources.
     *
     * @param resources Context resources used to load animation bitmaps.
     * @param resourceIds The resource ids used to instantiate animation bitmaps.
     * @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 BitmapAnimation(Resources resources, int[] resourceIds, float frameDuration, boolean isLoop, AnimationCallback callback) {
        this(getBitmapsFromResources(resources, resourceIds), frameDuration, isLoop, callback);
    }

    /**
     * Loads an array of bitmaps from the Resources.
     *
     * @param resources The Resources from which to load the bitmaps.
     * @param resourceIds The ids of the resources to load.
     * @return The array of loaded bitmaps.
     */
    private static Bitmap[] getBitmapsFromResources(Resources resources, int[] resourceIds) {
        final int bitmapCount = resourceIds.length;
        Bitmap[] bitmaps = new Bitmap[bitmapCount];

        for (int i = 0; i < bitmapCount; i++) {
            bitmaps[i] = BitmapFactory.decodeResource(resources, resourceIds[i]);
        }
        return bitmaps;
    }

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

    @Override
    public Bitmap getBitmap(int frameId) {
        // Prevent out of bounds exceptions.
        if (frameId < 0 || frameId >= mFrames.length) {
            return null;
        }
        return mFrames[frameId];
    }

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

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

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




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