前言
逐帧动画最初我是在Flash上面接触到的。它是通过在很短的时间内,快速的播放多张连续的图片,从而给人一种动画的感觉,其实,使用摄像器材也是如此,摄像器材通过在每秒,甚至每毫秒的快速采样,实质上就是拍照片,在播放视频的时候,屏幕不停的绘制采样的照片,由于播放的速度超过了我们肉眼的反应时间。我们就感觉动了起来。
演示
代码实现
逐帧动画首先需要定义好XML资源文件,同时设置duration属性,控制每一帧的持续时间。
图案我是使用PhotoShop制作的,你也可以使用你喜欢的图片。
逐帧动画XML
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item
android:drawable="@drawable/ni"
android:duration="500"/>
<item
android:drawable="@drawable/hao"
android:duration="500"/>
<item
android:drawable="@drawable/huan"
android:duration="500"/>
<item
android:drawable="@drawable/yin"
android:duration="500"/>
<item
android:drawable="@drawable/lai"
android:duration="500"/>
<item
android:drawable="@drawable/dao"
android:duration="500"/>
<item
android:drawable="@drawable/wo"
android:duration="500"/>
<item
android:drawable="@drawable/de"
android:duration="500"/>
<item
android:drawable="@drawable/bo"
android:duration="500"/>
<item
android:drawable="@drawable/ke"
android:duration="500"/>
</animation-list>
主界面XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/anim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dip"
android:background="@anim/action" />
<ImageView
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/anim"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dip"
android:background="@drawable/stop"
android:clickable="true" />
</RelativeLayout>
JAVA代码
public class MainActivity extends Activity {
private ImageView btn;
private boolean isPlay = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView anim = (ImageView) findViewById(R.id.anim);
final AnimationDrawable animation = (AnimationDrawable) anim
.getBackground();
btn = (ImageView) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
if (isPlay) {
animation.start();
btn.setBackground(getResources().getDrawable(
R.drawable.play));
isPlay = false;
} else {
animation.stop();
btn.setBackground(getResources().getDrawable(
R.drawable.stop));
isPlay = true;
}
}
});
}
}
总结
如果动画稍长,逐帧动画要求的素材太多了,如果没有专门的美工,那简直是疯了的节奏啊,还好,Android提供了一种补间动画,几张图片就能构成一个动画效果。如果喜欢,请期待我的下一篇博文。
技术因分享饱含生机