Android精靈動畫用法實例
更新時間:2015年06月16日 16:46:22 作者:紅薯
這篇文章主要介紹了Android精靈動畫用法,實例分析了Android動畫的相關(guān)使用技巧,需要的朋友可以參考下
本文實例講述了Android精靈動畫用法。分享給大家供大家參考。具體如下:
ElaineAnimated.java文件如下:
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 be 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)
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;
}
public void setFrameNr(int frameNr) {
this.frameNr = frameNr;
}
public int getCurrentFrame() {
return currentFrame;
}
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;
}
public void setSpriteWidth(int spriteWidth) {
this.spriteWidth = spriteWidth;
}
public int getSpriteHeight() {
return spriteHeight;
}
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;
}
public void setY(int y) {
this.y = y;
}
// the update method for 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, 20, 150, null);
Paint paint = new Paint();
paint.setARGB(50, 0, 255, 0);
canvas.drawRect(20 + (currentFrame * destRect.width()), 150, 20 + (currentFrame * destRect.width()) + destRect.width(), 150 + destRect.height(), paint);
}
}
希望本文所述對大家的Android程序設(shè)計有所幫助。
相關(guān)文章
Android 基礎(chǔ)入門教程——開發(fā)環(huán)境搭建
這篇文章主要介紹了Android 如何搭建開發(fā)環(huán)境,文中講解非常細(xì)致,幫助大家開始學(xué)習(xí)Android,想要學(xué)習(xí)Android的朋友可以了解下2020-06-06
Android編程實現(xiàn)QQ表情的發(fā)送和接收完整實例(附源碼)
這篇文章主要介紹了Android編程實現(xiàn)QQ表情的發(fā)送和接收的方法,涉及Android圖片資源、正則表達(dá)式及對話框的相關(guān)操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11
Android通過aapt命令獲取apk詳細(xì)信息(包括:文件包名,版本號,SDK等信息)
本文給大家分享android通過aapt命令獲取apk詳細(xì)信息(包括:文件包名,版本號,SDK等信息),非常不錯,簡單實用,對android sdk aapt知識感興趣的朋友一起通過本文學(xué)習(xí)吧2016-11-11
Android Webview添加網(wǎng)頁加載進度條實例詳解
這篇文章主要介紹了Android Webview添加網(wǎng)頁加載進度條實例詳解的相關(guān)資料,需要的朋友可以參考下2016-01-01
Android 使用 RxJava2 實現(xiàn)倒計時功能的示例代碼
本篇文章主要介紹了Android 使用 RxJava2 實現(xiàn)倒計時功能的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03

