Android精靈動畫用法實(shí)例
本文實(shí)例講述了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è)計(jì)有所幫助。
- android Tween Animation屬性設(shè)置方法實(shí)例
- Android Tween動畫之RotateAnimation實(shí)現(xiàn)圖片不停旋轉(zhuǎn)效果實(shí)例介紹
- Android編程之簡單逐幀動畫Frame的實(shí)現(xiàn)方法
- Android編程實(shí)現(xiàn)仿心跳動畫效果的方法
- Android編程之界面跳動提示動畫效果實(shí)現(xiàn)方法
- Android編程根據(jù)系列圖片繪制動畫實(shí)例總結(jié)
- Android實(shí)現(xiàn)動畫效果詳解
- android實(shí)現(xiàn)字體閃爍動畫的方法
- Android編程中Tween動畫和Frame動畫實(shí)例分析
相關(guān)文章
Android實(shí)現(xiàn)退出時關(guān)閉所有Activity的方法
這篇文章主要介紹了Android實(shí)現(xiàn)退出時關(guān)閉所有Activity的方法,主要通過自定義類CloseActivityClass實(shí)現(xiàn)這一功能,需要的朋友可以參考下2014-09-09
詳解Android端與JavaWeb傳輸加密(DES+RSA)
這篇文章主要介紹了詳解Android端與JavaWeb傳輸加密(DES+RSA),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-11-11
Android利用Document實(shí)現(xiàn)xml讀取和寫入操作
這篇文章主要為大家詳細(xì)介紹了Android利用Document實(shí)現(xiàn)xml讀取和寫入操作,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-12-12
Android TextView漸變顏色和方向及動畫效果的設(shè)置詳解
TextView的在安卓中可以理解為一個文本視圖控件,Android的視圖控件的基類是View類,可以理解的TextView是View的子類。我們通常在.XML布局文件中會為文本視圖控件指定各種屬性來設(shè)置它的樣式,今天我們要講的當(dāng)然不是傳統(tǒng)常見的那種,將會帶有漸變顏色和方向及動畫效果2021-11-11
Androd 勇闖高階性能優(yōu)化之布局優(yōu)化篇
Android性能優(yōu)化方面也有很多文章了,這里就做一個總結(jié),從原理到方法,工具等做一個簡單的了解,從而可以慢慢地改變編碼風(fēng)格,從而提高性能2021-10-10
Android編程中Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)功能詳解
這篇文章主要介紹了Android編程中Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)功能,結(jié)合實(shí)例形式分析了Android Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)功能的具體步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-07-07
Android實(shí)現(xiàn)圖片點(diǎn)擊爆炸效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圖片點(diǎn)擊爆炸效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-08-08
android notification 的總結(jié)分析
notification是一種出現(xiàn)在任務(wù)欄的提示,特別是在4.0以后notification改進(jìn)了不少,本文內(nèi)容都是基于4.0及4.1以后總結(jié)來的2013-05-05

