前面讲了三大动画的视图动画,现在就来简单介绍一下帧动画。
现在先来看一下效果:
一.使用xml布局的形式
1.在drawable目录下新建一个xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="@drawable/icon1"
android:duration="300"></item>
<item
android:drawable="@drawable/icon2"
android:duration="300"></item>
<item
android:drawable="@drawable/icon3"
android:duration="300"></item>
<item
android:drawable="@drawable/icon4"
android:duration="300"></item>
<item
android:drawable="@drawable/icon5"
android:duration="300"></item>
<item
android:drawable="@drawable/icon6"
android:duration="300"></item>
</animation-list>
2.布局xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/main3_image"
android:src="@drawable/icon1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
3.Java代码
public class Main3Activity extends AppCompatActivity{
private ImageView image;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
initView();
}
private void initView() {
image= (ImageView) findViewById(R.id.main3_image);
image.setImageResource(R.drawable.my_frame_animation);
AnimationDrawable animationDrawable= (AnimationDrawable) image.getDrawable();
animationDrawable.start();
}
}
二.使用代码的形式
public class Main3Activity extends AppCompatActivity{
private ImageView image;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
initView();
}
private void initView() {
AnimationDrawable animationDrawable=new AnimationDrawable();
Drawable drawable=getResources().getDrawable(R.drawable.icon1);
Drawable drawable2=getResources().getDrawable(R.drawable.icon2);
Drawable drawable3=getResources().getDrawable(R.drawable.icon3);
Drawable drawable4=getResources().getDrawable(R.drawable.icon4);
Drawable drawable5=getResources().getDrawable(R.drawable.icon5);
Drawable drawable6=getResources().getDrawable(R.drawable.icon6);
//添加图片
animationDrawable.addFrame(drawable,300);
animationDrawable.addFrame(drawable2,300);
animationDrawable.addFrame(drawable3,300);
animationDrawable.addFrame(drawable4,300);
animationDrawable.addFrame(drawable5,300);
animationDrawable.addFrame(drawable6,300);
animationDrawable.setOneShot(false);
image.setImageDrawable(animationDrawable);
animationDrawable.start();
}
}