Bitmap[] bitmap = new Bitmap[cols*rows]; int width = bitSource.getWidth() / cols; int height = bitSource.getHeight() / rows; int x = 0, y = 0; for (int i = 0; i < cols * rows; i++) { bitmap[i] = Bitmap.createBitmap(bitSource, x, y, width, height); if ((i + 1) % cols == 0 && i != 0) { y += height; x = 0; } else { x += width; } }
然后利用当前系统时间-程序ondraw后的时间,如果大于你设置的动画时间,就继续下一张图.package com.linjian;import java.io.InputStream;import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;public class Animation { /** 上一帧播放时间 **/
private long mLastPlayTime = 0;
/** 播放当前帧的ID **/
private int mPlayID = 0;
/** 动画frame数量 **/
private int mFrameCount = 0;
/** 用于储存动画资源图片 **/
private Bitmap[] mframeBitmap = null;
/** 是否循环播放 **/
private boolean mIsLoop = false;
/** 播放结束 **/
private boolean mIsend = false;
/** 动画播放间隙时间 **/
private int ANIM_TIME = 100;
public Animation(){
}
/**
* 构造函数
*
* @param context
* @param frameBitmapID
* @param isloop
*/
public Animation(Context context, int[] frameBitmapID, boolean isloop) {
mFrameCount = frameBitmapID.length;
mframeBitmap = new Bitmap[mFrameCount];
for (int i = 0; i < mFrameCount; i++) {
mframeBitmap[i] = ReadBitMap(context, frameBitmapID[i]);
}
mIsLoop = isloop;
} /**
* 构造函数
*
* @param context
* @param frameBitmap
* @param isloop
*/
public Animation(Context context, Bitmap[] frameBitmap, boolean isloop) {
mFrameCount = frameBitmap.length;
mframeBitmap = frameBitmap;
mIsLoop = isloop;
} /**
* 绘制动画中的其中一帧
*
* @param Canvas
* @param paint
* @param x
* @param y
* @param frameID
*/
public void DrawFrame(Canvas Canvas, Paint paint, int x, int y, int frameID) {
Canvas.drawBitmap(mframeBitmap[frameID], x, y, paint);
} /**
* 绘制动画
*
* @param Canvas
* @param paint
* @param x
* @param y
*/
public void DrawAnimation(Canvas Canvas, Paint paint, int x, int y) {
// 如果没有播放结束则继续播放
if (!mIsend) {
Canvas.drawBitmap(mframeBitmap[mPlayID], x, y, paint);
long time = System.currentTimeMillis();
if (time - mLastPlayTime > ANIM_TIME) {
mPlayID++;
mLastPlayTime = time;
if (mPlayID >= mFrameCount) {
// 标志动画播放结束
mIsend = true;
if (mIsLoop) {
// 设置循环播放
mIsend = false;
mPlayID = 0;
}
}
}
}
}
public long getmLastPlayTime() {
return mLastPlayTime;
}
public void setmLastPlayTime(long mLastPlayTime) {
this.mLastPlayTime = mLastPlayTime;
}
public int getmPlayID() {
return mPlayID;
}
public void setmPlayID(int mPlayID) {
this.mPlayID = mPlayID;
}
public int getmFrameCount() {
return mFrameCount;
}
public void setmFrameCount(int mFrameCount) {
this.mFrameCount = mFrameCount;
}
public Bitmap[] getMframeBitmap() {
return mframeBitmap;
}
public void setMframeBitmap(Bitmap[] mframeBitmap) {
this.mframeBitmap = mframeBitmap;
}
public boolean ismIsLoop() {
return mIsLoop;
}
public void setmIsLoop(boolean mIsLoop) {
this.mIsLoop = mIsLoop;
}
public boolean ismIsend() {
return mIsend;
}
public void setmIsend(boolean mIsend) {
this.mIsend = mIsend;
}
public int getANIM_TIME() {
return ANIM_TIME;
}
public void setANIM_TIME(int ANIM_TIME) {
this.ANIM_TIME = ANIM_TIME;
}
/**
* 切割图片资源.
* @param bitSource
* @param cols
* @param rows
* @return
*/
public Bitmap[] clipBitmapResource(Bitmap bitSource,int cols,int rows){
Bitmap[] bitmap = new Bitmap[cols*rows];
int width = bitSource.getWidth() / cols;
int height = bitSource.getHeight() / rows;
int x = 0, y = 0;
for (int i = 0; i < cols * rows; i++) {
bitmap[i] = Bitmap.createBitmap(bitSource, x, y, width, height); if ((i + 1) % cols == 0 && i != 0) { y += height;
x = 0; } else { x += width;
}
}
return bitmap;
} /**
* 读取图片资源
*
* @param context
* @param resId
* @return
*/
public Bitmap ReadBitMap(Context context, int resId) {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
// 获取资源图片
InputStream is = context.getResources().openRawResource(resId);
return BitmapFactory.decodeStream(is, null, opt);
}
}
2. 需要刷新时,调用requestRender()函数即可。系统调用onDrawFrame的时间怎么控制:
http://www.eoeandroid.com/thread-93767-1-1.html