逐帧动画控件的方法:
package com.example.scr;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.View;
public class ArrowAnimationView extends View{
Context mContext;
private int scr_w = 0, scr_h = 0;
private ArrayList<Bitmap> ani = new ArrayList<Bitmap>(5);
private int g_anim_index = 0;
private boolean isStart = true;
private boolean isShow = true;
private Timer timer = new Timer();
public ArrowAnimationView(Context context) {
super(context);
mContext = context;
int[] imgIds = {R.drawable.ani01, R.drawable.ani02, R.drawable.ani03,
R.drawable.ani04, R.drawable.ani05 };
for (int i = 0; i < imgIds.length; i++) {
Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(),
imgIds[i]);
ani.add(bm);
}
timer.scheduleAtFixedRate(new MyTask(), 100, 1000);
}
public ArrowAnimationView(Context context, int w, int h) {
super(context);
scr_w = w;
scr_h = h;
mContext = context;
int[] imgIds = {R.drawable.ani01, R.drawable.ani02, R.drawable.ani03,
R.drawable.ani04, R.drawable.ani05 };
for (int i = 0; i < imgIds.length; i++) {
Bitmap bm = BitmapFactory.decodeResource(mContext.getResources(),
imgIds[i]);
ani.add(bm);
}
timer.scheduleAtFixedRate(new MyTask(), 1, 200);
}
@Override
public void layout(int l, int t, int r, int b) {
// TODO Auto-generated method stub
super.layout(l, t, r, b);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void drawArrowAnim(Canvas canvas)
{
int img_w = ani.get(0)。getWidth();
int img_h = ani.get(0)。getHeight();
int left = (scr_w / 2) - (img_w / 2);
int top = scr_h - img_h - 120;
canvas.drawBitmap(ani.get(g_anim_index), left, top, null);
}
private class MyTask extends TimerTask {
@Override
public void run() {
if (isStart) {
g_anim_index++;
if (g_anim_index > ani.size()-1) {
g_anim_index = 0;
}
}
}
}
void startAnim()
{
isStart = true;
}
void stopAnim()
{
isStart = false;
}
void hideAnim()
{
isShow = true;
}
void showAnim()
{
isShow = false;
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
if (isShow)
{
drawArrowAnim(canvas);
}
invalidate();
}
public void onViewDestroy() {
// TODO Auto-generated method stub
if (timer != null) {
timer.cancel();
}
}
}
更多精彩教程请关注:小橙子下载