Android Sprite Animation Usage Example _android

Source: Internet
Author: User
Tags gety

This example describes the use of Android sprite animations. Share to everyone for your reference. Specifically as follows:

Elaineanimated.java files are as follows:

Package Net.obviam.walking.model;
Import Android.graphics.Bitmap;
Import Android.graphics.Canvas;
Import Android.graphics.Paint;
Import Android.graphics.Rect;
  public class Elaineanimated {private static final String TAG = ElaineAnimated.class.getSimpleName ();
  Private Bitmap Bitmap;
  The animation sequence private Rect sourcerect;
  The rectangle to is drawn from the animation bitmap private int framenr;
  Number of frames in animation private int currentframe;
  The current frame private long frameticker;
  The time of the last frame update private int frameperiod;
  Milliseconds between each frame (1000/fps) private int spritewidth;
  The width of the sprite to calculate the cut out rectangle private int spriteheight;
  The height of the sprite private int x;
  The X coordinate of the object (top left of the image) private int y; The Y coordinate of the object (top left of the image) is public elaineanimated (Bitmap Bitmap, int x, int y, int width, int height, int fps, int framecount) {this.bitmap = bitmap;
    this.x = x;
    This.y = y;
    currentframe = 0;
    Framenr = Framecount;
    Spritewidth = Bitmap.getwidth ()/framecount;
    Spriteheight = Bitmap.getheight ();
    Sourcerect = new Rect (0, 0, spritewidth, spriteheight);
    Frameperiod = 1000/fps;
  Frameticker = 0l;
  Public Bitmap Getbitmap () {return Bitmap;
  public void SetBitmap (Bitmap Bitmap) {this.bitmap = Bitmap;
  Public Rect Getsourcerect () {return sourcerect;
  public void Setsourcerect (Rect sourcerect) {this.sourcerect = Sourcerect;
  public int Getframenr () {return framenr;
  The public void setframenr (int framenr) {this.framenr = Framenr;
  public int Getcurrentframe () {return currentframe;
  The public void setcurrentframe (int currentframe) {this.currentframe = Currentframe;
  public int Getframeperiod () {return frameperiod; } public void SetframeperIOD (int frameperiod) {this.frameperiod = Frameperiod;
  public int getspritewidth () {return spritewidth;
  The public void setspritewidth (int spritewidth) {this.spritewidth = Spritewidth;
  public int getspriteheight () {return spriteheight;
  The public void setspriteheight (int spriteheight) {this.spriteheight = Spriteheight;
  public int GetX () {return x;
  public void SetX (int x) {this.x = x;
  public int GetY () {return y;
  The public void sety (int y) {this.y = y;  }//The Update method to Elaine public void update (long gametime) {if (Gametime > Frameticker + frameperiod)
      {frameticker = gametime;
      Increment the frame currentframe++;
      if (currentframe >= framenr) {currentframe = 0;
    }//define the rectangle to cut out sprite This.sourceRect.left = currentframe * spritewidth;
  This.sourceRect.right = This.sourceRect.left + spritewidth; }//THe draw method which draws the corresponding frame public void draw (Canvas Canvas) {//Where to draw the sprite
    Rect destrect = new Rect (GetX (), GetY (), GetX () + Spritewidth, GetY () + spriteheight);
    Canvas.drawbitmap (Bitmap, sourcerect, destrect, NULL);
    Canvas.drawbitmap (bitmap, M, null);
    Paint Paint = new Paint ();
    Paint.setargb (50, 0, 255, 0); Canvas.drawrect (Currentframe * destrect.width ()), + (Currentframe * destrect.width ()) + destrect.width (), 15
  0 + destrect.height (), paint);

 }
}

I hope this article will help you with your Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.