Advertisement
Guest User

Untitled

a guest
Feb 4th, 2013
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.52 KB | None | 0 0
  1. package net.obviam.walking.model;
  2.  
  3. import android.graphics.Bitmap;
  4. import android.graphics.Canvas;
  5. import android.graphics.Paint;
  6. import android.graphics.Rect;
  7.  
  8.  
  9. /**
  10.  * @author impaler
  11.  *
  12.  */
  13. public class ElaineAnimated {
  14.    
  15.     private static final String TAG = ElaineAnimated.class.getSimpleName();
  16.  
  17.     private Bitmap bitmap;      // the animation sequence
  18.     private Rect sourceRect;    // the rectangle to be drawn from the animation bitmap
  19.     private int frameNr;        // number of frames in animation
  20.     private int currentFrame;   // the current frame
  21.     private long frameTicker;   // the time of the last frame update
  22.     private int framePeriod;    // milliseconds between each frame (1000/fps)
  23.    
  24.     private int spriteWidth;    // the width of the sprite to calculate the cut out rectangle
  25.     private int spriteHeight;   // the height of the sprite
  26.    
  27.     private int x;              // the X coordinate of the object (top left of the image)
  28.     private int y;              // the Y coordinate of the object (top left of the image)
  29.    
  30.     public ElaineAnimated(Bitmap bitmap, int x, int y, int width, int height, int fps, int frameCount) {
  31.         this.bitmap = bitmap;
  32.         this.x = x;
  33.         this.y = y;
  34.         currentFrame = 0;
  35.         frameNr = frameCount;
  36.         spriteWidth = bitmap.getWidth() / frameCount - 21;
  37.         spriteHeight = bitmap.getHeight() / 11;
  38.         sourceRect = new Rect(0, 0, spriteWidth, spriteHeight);
  39.         framePeriod = 1000 / fps;
  40.         frameTicker = 0l;
  41.     }
  42.    
  43.    
  44.     public Bitmap getBitmap() {
  45.         return bitmap;
  46.     }
  47.     public void setBitmap(Bitmap bitmap) {
  48.         this.bitmap = bitmap;
  49.     }
  50.     public Rect getSourceRect() {
  51.         return sourceRect;
  52.     }
  53.     public void setSourceRect(Rect sourceRect) {
  54.         this.sourceRect = sourceRect;
  55.     }
  56.     public int getFrameNr() {
  57.         return frameNr;
  58.     }
  59.     public void setFrameNr(int frameNr) {
  60.         this.frameNr = frameNr;
  61.     }
  62.     public int getCurrentFrame() {
  63.         return currentFrame;
  64.     }
  65.     public void setCurrentFrame(int currentFrame) {
  66.         this.currentFrame = currentFrame;
  67.     }
  68.     public int getFramePeriod() {
  69.         return framePeriod;
  70.     }
  71.     public void setFramePeriod(int framePeriod) {
  72.         this.framePeriod = framePeriod;
  73.     }
  74.     public int getSpriteWidth() {
  75.         return spriteWidth;
  76.     }
  77.     public void setSpriteWidth(int spriteWidth) {
  78.         this.spriteWidth = spriteWidth;
  79.     }
  80.     public int getSpriteHeight() {
  81.         return spriteHeight;
  82.     }
  83.     public void setSpriteHeight(int spriteHeight) {
  84.         this.spriteHeight = spriteHeight;
  85.     }
  86.     public int getX() {
  87.         return x;
  88.     }
  89.     public void setX(int x) {
  90.         this.x = x;
  91.     }
  92.     public int getY() {
  93.         return y;
  94.     }
  95.     public void setY(int y) {
  96.         this.y = y;
  97.     }
  98.    
  99.     // the update method for Elaine
  100.     public void update(long gameTime) {
  101.         if (gameTime > frameTicker + framePeriod) {
  102.             frameTicker = gameTime;
  103.             // increment the frame
  104.             currentFrame++;
  105.             if (currentFrame >= frameNr) {
  106.                 currentFrame = 0;
  107.             }
  108.         }
  109.         // define the rectangle to cut out sprite
  110.         this.sourceRect.left = currentFrame * spriteWidth;
  111.         this.sourceRect.right = this.sourceRect.left + spriteWidth;
  112.     }
  113.    
  114.     // the draw method which draws the corresponding frame
  115.     public void draw(Canvas canvas) {
  116.         // where to draw the sprite
  117.             Rect destRect = new Rect(getX(), getY(), getX() + spriteWidth, spriteHeight);
  118.             canvas.drawBitmap(bitmap, sourceRect, destRect, null);
  119.        
  120.        
  121.        
  122.         //Disegna ogni frame di elaine
  123.        
  124.         /*canvas.drawBitmap(bitmap, 20, 150, null);
  125.         Paint paint = new Paint();
  126.         paint.setARGB(50, 0, 255, 0);
  127.         canvas.drawRect(20 + (currentFrame * destRect.width()), 150, 20 + (currentFrame * destRect.width()) + destRect.width(), 150 + destRect.height(),  paint);
  128.         */}
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement