Android精灵动画用法实例
作者:红薯 发布时间:2023-08-22 22:20:57
标签:Android,动画
本文实例讲述了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程序设计有所帮助。
0
投稿
猜你喜欢
- 1,现在因为遇到一个读取pdf文件文本信息遇到乱么问题,才找到这个文本字符串的编码转换的实现方式来判断是否存在乱码(0>乱码>2
- 话不多说,请看下面//C# 代码int year = DateTime.Now.Year;int month = DateTime.Now.
- Console.WriteLine("This is a Client, host name is {0}", Dns.
- 前言:在工作中一次排查慢接口时,查到了一个函数耗时较长,最终定位到是通过 List 去重导致的。由于测试环境还有线上早期数据较少,这个接口的
- 执行如下的jni调用:package jni;public class JNITransObject { public nativ
- 1.Java Io流的概念,分类,类图。1.1 Java Io流的概念java的io是实现输入和输出的基础,可以方便的实现数据的输入和输出操
- 现在有一张订单表t_stockorder,其拥有id、code、client_id、merchandise_id、merchandise_n
- 知乎是一个真实的网络问答社区,社区氛围友好、理性、认真,连接各行各业的精英。他们分享着彼此的专业知识、经验和见解,为中文互联网源源不断地提供
- 本文实例讲述了Android使用onCreateOptionsMenu()创建菜单Menu的方法。分享给大家供大家参考,具体如下:Andro
- try { // 方
- 1安装eclipse插件步骤,点击help,选择Eclipse Marketplace2.输入Scala,点击go3.选择搜索到的Scala
- 上代码喽~package ncu.com.app.chatpter_5;import java.util.Random;//结点类class
- 本文实例为大家分享了C#仿微信红包功能的具体代码,供大家参考,具体内容如下Program.cs代码:class Program { &nbs
- 基本用法不说了,网上例子很多,这里主要介绍下比较特殊情况下使用的方法。1. 分组有的时候,我们对一个实体类需要有多中验证方式,在不同的情况下
- 下面Demo中我使用了2种排序方式1.让Employee继承IComparable 接口,实现CompareTo方法排序2.定义一个方法使用
- 本文实例讲述了C#生成随机数的方法。分享给大家供大家参考。具体如下:/// <summary> /// 生成num位
- 当数据量比较大的时候,我们就需要考虑读写分离了,也就是动态切换数据库连接,对指定的数据库进行操作。在spring中实现动态的切换无非就是利用
- 创建类模式主要关注对象的创建过程,将对象的创建过程进行封装,使客户端可以直接得到对象,而不用去关心如何创建对象。创建类模式有5种,分别是:
- 本文实例为大家分享了C#泛型类创建与使用的具体代码,供大家参考,具体内容如下using System;using System.Collec
- C#动态创建lambda表达式近日在使用了一下EF框架,在做多条件where查询的时候不知道怎么做,网上找了找,一开始用context.Da