Using Frame Animation in Live wallpaper

June 7, 2012 | Rishabh Agrawal | General

I want to animate fishes….Following are the steps to be followed:

fish class…

public class Jellyfish {

    public float x;             // x co-ordinate
    public float y;             // y co-ordinate
    public float off;
    ValentineEngine engine;
    Bitmap[] bitmap;
    int currentFrame;          // current frame display..
    long frameTicker;
    int frameNum;           //  no of frame
    // public float mX;
    // public float mY;

    double speedX;        // x co-ordinate speed of animation
    double speedY;        // y co-ordinate speed of animation

    int direction;        //   Direction of animaton

    int[] FPS;       
    int[] images = new int[] { R.drawable.jelly_fish1, R.drawable.jelly_fish2,
            R.drawable.jelly_fish3, R.drawable.jelly_fish4,
            R.drawable.jelly_fish5, R.drawable.jelly_fish6,
            R.drawable.jelly_fish7, R.drawable.jelly_fish8,       

                  //  array of fishes….
            R.drawable.jelly_fish9, R.drawable.jelly_fish10,
            R.drawable.jelly_fish11, R.drawable.jelly_fish12 };

    void init() {
        frameNum = images.length;        // array size
        bitmap = new Bitmap[frameNum];
        FPS = new int[] { 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185, 185 };
    }

    public Jellyfish(Resources res, float x, float y,                                        // constructor calling from your services class
            ValentineEngine valentineEngine, double speedX, double speedY,                   // and getting value of all perameter…
            int direction) {
        this.direction = direction;

        this.speedY = speedY;

        this.speedX = speedX;
        this.engine = valentineEngine;
        init();

        for (int i = 0; i < frameNum; i++) {
            bitmap[i] = BitmapFactory.decodeResource(res, images[i]);
        }

        this.x = x;
        this.y = y;
    }

                /* method to draw fishes on canvas */

    public void doDraw(Canvas canvas) {
        //off = -(engine.width * engine.xOffset) + x;
        canvas.drawBitmap(bitmap[currentFrame],off, y, null);       
    }

            /* method to animate fishes */

        public void animate(long elapsedTime) {
        if (System.currentTimeMillis() > frameTicker + FPS[0]) {
            currentFrame++;
            if (currentFrame >= frameNum) {
                currentFrame = 0;
            }
            frameTicker = System.currentTimeMillis();
            x += speedX * (elapsedTime / 20f);                      // updating X & Y co-ordinate
            y += speedY * (elapsedTime / 20f) * direction;

        }
    }

         /* method to check border when they cross the border */

   public boolean checkBorder() {
        if (direction == -1)
            if (y < 0)

                return true;
            else if (y > engine.height)
                return true;

        return false;
    }
}

/***** in your wallpaper services class*******/

create arraylist

ArrayList<Jellyfish> jellyfish;
jellyfish = new ArrayList<Jellyfish>();

               /*****in animate method  in your wallpaper services class********/

synchronized (jellyfish) {
                if (jellyfish.size() < 3) {
                    /* calling jellyfish constructor */
                    addjellyFish();
                }

                for (int i = 0; i < jellyfish.size(); i++) {
                    if (!jellyfish.get(i).checkBorder()) {
                        /* animate jellyfish */
                        jellyfish.get(i).animate(elapsed);
                    } else {
                        /* fishes is removed when they cross the border */
                        jellyfish.remove(i);
                    }
                }
            }

                  /***** in doDraw method of wallpaper services class********/

  synchronized (jellyfish) {
                for (int i = 0; i < jellyfish.size(); i++) {
                    /* drawing fish on to the canvas */
                    jellyfish.get(i).doDraw(canvas);
                }

            }

/*****  addjelliFish() metod call in animate metod of wallpaper services class********/

 /* jellyfish constructor calling*/

        void addjellyFish() {
            synchronized (jellyfish) {
                jellyfish.add(new Jellyfish(getResources(), (float) getRandom(
                        0, width – 150),                                                // constructor calling of jelly fish class..and pass all perameter
                        (float) getRandom(height – 10, height), this,
                        getRandom(2.23, 2.35), getRandom(2.25, 2.45), -1));
            }
        }

 

-By Sonu Odesi

Android developer

 

 


THE AUTHORRishabh Agrawal

Rishabh Agrawal is the founder of Creatiosoft, a company focused on creating high-quality software for the iGaming industry, specialising in poker and card games. With years of experience, Rishabh is dedicated to delivering engaging and user-friendly gaming experiences. Through this blog, he shares his passion and insights to help readers understand the latest trends and advancements in iGaming.

Comments are closed.

Recent Posts

The Future of Mobile Gaming: What to Expect in the Next 5 Years

Over the years, mobile gaming has seen amazing development and innovation utilized by the game development company in India. It…
06 Jun 2023 Rishabh Agrawal

How Do I Get Started With NFT Gaming?

The popularity of the NFT game has created a different fan base. There was a time when games were a…
18 Jul 2022 Rishabh Agrawal

What Are The Top Trends in NFT Marketplace 2022?

The NFTs are the new engaging and revolutionary technology across the globe. Though, these non-fungible tokens appeared for the first…
09 Jun 2022 Rishabh Agrawal