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 基礎(chǔ)入門教程——開發(fā)環(huán)境搭建
這篇文章主要介紹了Android 如何搭建開發(fā)環(huán)境,文中講解非常細(xì)致,幫助大家開始學(xué)習(xí)Android,想要學(xué)習(xí)Android的朋友可以了解下2020-06-06
使用Thumbnails實(shí)現(xiàn)圖片指定大小壓縮
這篇文章主要為大家詳細(xì)介紹了使用Thumbnails實(shí)現(xiàn)圖片指定大小壓縮,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08
Android編程實(shí)現(xiàn)QQ表情的發(fā)送和接收完整實(shí)例(附源碼)
這篇文章主要介紹了Android編程實(shí)現(xiàn)QQ表情的發(fā)送和接收的方法,涉及Android圖片資源、正則表達(dá)式及對話框的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
Android實(shí)現(xiàn)在屏幕上移動圖片的方法
這篇文章主要介紹了Android實(shí)現(xiàn)在屏幕上移動圖片的方法,實(shí)例分析了Android操作圖片的相關(guān)技巧,需要的朋友可以參考下2015-06-06
Android通過aapt命令獲取apk詳細(xì)信息(包括:文件包名,版本號,SDK等信息)
本文給大家分享android通過aapt命令獲取apk詳細(xì)信息(包括:文件包名,版本號,SDK等信息),非常不錯,簡單實(shí)用,對android sdk aapt知識感興趣的朋友一起通過本文學(xué)習(xí)吧2016-11-11
Android Webview添加網(wǎng)頁加載進(jìn)度條實(shí)例詳解
這篇文章主要介紹了Android Webview添加網(wǎng)頁加載進(jìn)度條實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2016-01-01
Android 使用 RxJava2 實(shí)現(xiàn)倒計(jì)時功能的示例代碼
本篇文章主要介紹了Android 使用 RxJava2 實(shí)現(xiàn)倒計(jì)時功能的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03

