Android 学习笔记(八):播放帧动画
帧动画,即逐帧动画,就是一张一张的显示连续图片,实现动画效果,
关键是控制图片切换频率的方式,开始试了试System.currentTimeMillis(),
API中的解释是:
Returns the current system time in milliseconds since January 1, 1970 00:00:00 UTC.
即返回从 UTC 1970 年 1 月 1 日午夜开始经过的毫秒数,
通过
long nowTime = System.currentTimeMillis();
if (nowTime - startTime >= 100) {
startTime=nowTime;
//do something
}
可实现指定间隔时间,
但是关于System.currentTimeMillis(),由于系统的不同,访问计时器的开销,等等,不能保证每次返回值的精度,实际测试中也出现了画面停顿和闪烁现象,还是用线程吧,
先准备好动画图片,
关于命名,不能用纯数字,这样在R.java中,生成的变量会出现错误:
public static final int23=0x7f020001;
下面是代码:
FrameAnimationActivity.java
[java] public class FrameAnimationActivity extends Activity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new Drawing(this));
}
}
class Drawing extends View implements Runnable{
Bitmap[] bitmap = new Bitmap[6];
//创建一个画笔
Paint paint=new Paint();
//保存图片切换的变量
int temp=0;
//创建线程
Thread t=new Thread(this);
//构造方法
public Drawing(Context context) {
super(context);
bitmap[0] = BitmapFactory.decodeResource(this.getResources(),R.drawable.r23);
bitmap[1] = BitmapFactory.decodeResource(this.getResources(),R.drawable.r24);
bitmap[2] = BitmapFactory.decodeResource(this.getResources(),R.drawable.r25);
bitmap[3] = BitmapFactory.decodeResource(this.getResources(),R.drawable.r26);
bitmap[4] = BitmapFactory.decodeResource(this.getResources(),R.drawable.r27);
bitmap[5] = BitmapFactory.decodeResource(this.getResources(),R.drawable.r28);
//开始线程
t.start();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//设置画布颜色
canvas.drawColor(Color.GRAY);
//绘制图片
canvas.drawBitmap(bitmap[temp], 50, 20, paint);
}
public void run() {
while(true){
temp++;
if(temp==5){
temp=0;
}
//刷新屏幕,间隔70毫秒
this.postInvalidate();
try {
Thread.sleep(70);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class FrameAnimationActivity extends Activity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new Drawing(this));
}
}
class Drawing extends View implements Runnable{
Bitmap[] bitmap = new Bitmap[6];
//创建一个画笔 www.zzzyk.com
Paint paint=new Paint();
//保存图片切换的变量
int temp=0;
//创建线程
Thread t=new Thread(this);
//构造方法
public Drawing(Context context) {
super(context);
bitmap[0] = BitmapFactory.decodeResource(this.getResources(),R.drawable.r23);
bitmap[1] = BitmapFactory.decodeResource(this.getResources(),R.drawable.r24);
bitmap[2] = BitmapFactory.decodeResource(this.getResources(),R.drawable.r25);
bitmap[3] = BitmapFactory.decodeResource(this.getResources(),R.drawable.r26);
bitmap[4] = BitmapFactory.decodeResource(this.getResources(),R.drawable.r27);
bitmap[5] = BitmapFactory.decodeResource(this.getResources(),R.drawable.r28);
//开始线程
t.start();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//设置画布颜色
canvas.drawColor(Color.GRAY);
//绘制图片
canvas.drawBitmap(bitmap[temp], 50, 20, paint);
}
public void run() {
while(true){
temp++;
if(temp==5){
temp=0;
}
//刷新屏幕,间隔70毫秒
this.postInvalidate();
try {
Thread.sleep(70);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
线程的使用和java一样,都是实现Rannable接口,重写run()方法,通过Thread类创建线程,
其中,刷新屏幕有两种方法,
invalidate():实例化一个Handler对象,并重写handleMessage方法调用invalidate()实现界面刷新,
postInvalidate():直接调用即可,
补充:移动开发 , Android ,