首先在res目录下建立anim目录,添加3个xml文件:load_animation_1, load_animation_2, load_animation_3,文件内容分别如下:
load_animation_1:
<?xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
android:duration="150">
android:duration="150">
android:duration="150">
android:duration="150">
android:duration="150">
android:duration="150">
android:duration="150">
android:duration="150">
android:duration="150">
android:duration="150">
android:duration="150">
android:duration="150">
load_animation_2:
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
android:duration="150">
android:duration="150">
android:duration="150">
android:duration="150">
android:duration="150">
android:duration="150">
android:duration="150">
android:duration="150">
android:duration="150">
android:duration="150">
android:duration="150">
android:duration="150">
wifi_animation_1.xml:
android:duration="150">
android:duration="150">
android:duration="150">
android:duration="150">
android:duration="150">
android:duration="150">
然后在MainActivity中通过setBackgroundResource(R.anim.load_animation_1)来为ImageView设置背景资源,并通过getBackground()方法来获得背景动态图片,返回的是一个AnimationDrawable对象,通过该对象的start()方法和stop()方法可以启动或者停止动态图片的效果,具体代码如下:
public class MainActivity extends AppCompatActivity {
private Button button1, button2, button3;
private ImageView animationIV;
private ImageView animationIV2;
private ImageView animationIV3;
private AnimationDrawable AniDraw, AniDraw2, AniDraw3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(android.R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(AniDraw.isRunning()) {
AniDraw.stop();
} else {
AniDraw.start();
}
}
});
button2 = (Button) findViewById(android.R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(AniDraw2.isRunning()) {
AniDraw2.stop();
} else {
AniDraw2.start();
}
}
});
button3 = (Button) findViewById(android.R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(AniDraw3.isRunning()) {
AniDraw3.stop();
} else {
AniDraw3.start();
}
}
});
animationIV = (ImageView) findViewById(R.id.animationIV);
animationIV.setBackgroundResource(R.anim.load_animation_1);
AniDraw = (AnimationDrawable) animationIV.getBackground();
animationIV2 = (ImageView)findViewById(R.id.animationIV2);
AniDraw2 = (AnimationDrawable)animationIV2.getDrawable();
animationIV3 = (ImageView)findViewById(R.id.animationIV3);
animationIV3.setImageResource(R.anim.wifi_animation_1);
AniDraw3 = (AnimationDrawable)animationIV3.getDrawable();
}
}
总结:
在anim文件夹下定义动画xml文件
通过标签来定义帧动画,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画
通过设置ImageView的setImageResource()来设置ImageView的动画效果,并且通过getDrawable()来获得ImageView的动画图片
通过设置ImageView的setBackgroundResource()方法来设置
ImageView的动画效果,并且通过getBackground()来获得
ImageView的动画图片
通过设置AnimationDrawable的start()和stop()方法可以启动或停止动画效果