动画分为逐帧动画、补间动画和属性动画
逐帧动画是依次显示一系列的静态图片,利用人眼的视觉暂留达到动画的效果,
实现方式一:
在资源Drawable中添加animation-list的xml动画资源在设置控件背景
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true" ><!-- oneshot="true"设置不循环播放,若为fals则循环播放 -->
<item android:drawable="@drawable/ic_launcher" android:duration="1000" />//显示的图片及显示时间。
<item android:drawable="@drawable/ic_launcher" android:duration="500" />
</animation-list>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:background="@drawable/lis"/>
</LinearLayout>
创建AnimationDrawable对象,获取对象后还可以继续调用addFrame(Drawable frame,int duration);添加图图片,然后必须调用AnimationDrawable的start()方法开始动画或stop()方法停止播放动画。
package com.example.donghua.test;
public class ShowDonghua extends Activity{
ImageView showD;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_donghua);
showD=(ImageView) findViewById(R.id.showD);
AnimationDrawable d=(AnimationDrawable) showD.getBackground();
d.start();
}
}
实现方式二:直接new AnimationDrawable对象,添加图片,设置背景,开始动画。
package com.example.donghua.test;
import com.example.sharecontent.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.widget.ImageView;
public class ShowDonghua extends Activity{
ImageView showD;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_donghua);
showD=(ImageView) findViewById(R.id.showD);
//AnimationDrawable d=(AnimationDrawable) showD.getBackground();
//d.start();
AnimationDrawable d=new AnimationDrawable();
d.addFrame(getResources().getDrawable(R.drawable.time_0), 2000);
d.addFrame(getResources().getDrawable(R.drawable.time_1), 2000);
d.addFrame(getResources().getDrawable(R.drawable.time_2), 2000);
showD.setImageDrawable(d);
d.start();
}
}