逐帧动画是把动画过程的每张静态图片都收集起来,然后由Android来控制依次显示这些静态图片,然后利用人眼视觉暂留的原理,给用户造成动画的错觉。逐帧动画的动画原理与放电影的原理完全一样。
步骤:
——1.定义逐帧动画—/res/drawable目录中
只要在<animation-list>元素中使用<item>子元素定义动画的全部帧,并制定各帧的持续时间即可。
定义逐帧动画的语法格式:
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true|false">
<item android:drawable=""
android:duration="">
</animation-list>
其中android:oneshot控制该动画是否循环播放如果为true,动画将不会循环播放,否则该动画将会循环播放,每一个item子元素添加一帧。
注意:Android完成支持在Java代码中创建逐帧动画,先创建AnimationDrawable对象,然后调用addFrame(Drawable frame,int duration)向该动画中添加帧,每调用一次addFrame方法,就相当于添加一个item元素。
——2.在android代码中使用逐帧动画的步骤
a)将逐帧动画的文件设置给某个组件的背景图片
b)获取AnimationDrawable动画对象
AnimationDrawable anim=(AnimationDrawable)image.getBackground();
c)开始播放动画,逐帧动画默认是不播放的
anim.start()
d)停止动画
anim.stop();
代码:在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/p_1_01"
android:duration="100"/>
<item android:drawable="@drawable/p_1_02"
android:duration="100"/>
<item android:drawable="@drawable/p_1_03"
android:duration="100"/>
<item android:drawable="@drawable/p_1_04"
android:duration="100"/>
<item android:drawable="@drawable/p_1_05"
android:duration="100"/>
<item android:drawable="@drawable/p_1_06"
android:duration="100"/>
<item android:drawable="@drawable/p_1_07"
android:duration="100"/>
<item android:drawable="@drawable/p_1_08"
android:duration="100"/>
</animation-list>
在布局文件中:
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity" >
<ImageView android:id="@+id/animation_iv"
android:background="@drawable/animaion_list"
android:layout_gravity="center_horizontal"
android:layout_width="200sp"
android:layout_height="200sp"
/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start"
android:onClick="start"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="stop"
android:onClick="stop"/>
</LinearLayout>
在Activity中:
public class MainActivity extends Activity {
private ImageView animation_iv;
private AnimationDrawable ad;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
animation_iv = (ImageView) findViewById(R.id.animation_iv);
ad = (AnimationDrawable)animation_iv.getBackground();
}
/*
* 如何将资源图片转化为drawable对象
*/
private Drawable get_Drawable(int img_id){
Drawable drawable = null;
Resources res = getResources();
drawable = res.getDrawable(img_id);
return drawable;
}
public void start(View view){
ad.start();
}
public void stop(View view){
ad.stop();
}
}
其实安卓也完全支持在Java代码中去创建逐帧动画
具体代码如下:
public class MainActivity extends Activity {
private ImageView animation_iv;
private AnimationDrawable ad;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
animation_iv = (ImageView) findViewById(R.id.animation_iv);
// ad = (AnimationDrawable)animation_iv.getBackground();
ad= new AnimationDrawable();
ad.setOneShot(false);
ad.addFrame(get_Drawable(R.drawable.p_1_01), 50);
ad.addFrame(get_Drawable(R.drawable.p_1_02), 50);
ad.addFrame(get_Drawable(R.drawable.p_1_03), 50);
ad.addFrame(get_Drawable(R.drawable.p_1_04), 50);
ad.addFrame(get_Drawable(R.drawable.p_1_05), 50);
ad.addFrame(get_Drawable(R.drawable.p_1_06), 50);
ad.addFrame(get_Drawable(R.drawable.p_1_07), 50);
ad.addFrame(get_Drawable(R.drawable.p_1_08), 50);
animation_iv.setBackground(ad);
}
/*
* 如何将资源图片转化为drawable对象
*/
private Drawable get_Drawable(int img_id){
Drawable drawable = null;
Resources res = getResources();
drawable = res.getDrawable(img_id);
return drawable;
}
public void start(View view){
ad.start();
}
public void stop(View view){
ad.stop();
}
}