参考:Android 逐帧动画:关于 逐帧动画 的使用都在这里了!
- 逐帧动画就是把动画的每一帧都表示出来,像一个gif一样
首先准备图片
在drawable下创建一个xml:frame_anim.xml
1
2
3
4
5
6
7
8
9
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item android:drawable="@drawable/signal1" android:duration="300"/>
<item android:drawable="@drawable/signal2" android:duration="300"/>
<item android:drawable="@drawable/signal3" android:duration="300"/>
<item android:drawable="@drawable/signal4" android:duration="300"/>
</animation-list>
每一个item表示一个图片,duration表示这个图片持续的时间,最外层的oneshot表示循环播放,false表示循环播放,true只播放一次
- 在java中加载此资源
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24private Button start;
private Button stop;
private ImageView image;
private AnimationDrawable animationDrawable;
...
start = (Button)findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
image = (ImageView)findViewById(R.id.image);
image.setImageResource(R.drawable.frame_anim);
animationDrawable = (AnimationDrawable) image.getDrawable();
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
animationDrawable.start();
}
});
stop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
animationDrawable.stop();
}
});
- 逐帧动画就像是一个gif,把每一帧的图片都指定了
- 避免图片过大造成内存泄漏