<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@mipmap/feiben1" android:duration="100"/>
<item android:drawable="@mipmap/feiben2" android:duration="100"/>
<item android:drawable="@mipmap/feiben3" android:duration="100"/>
<item android:drawable="@mipmap/feiben4" android:duration="100"/>
<item android:drawable="@mipmap/feiben5" android:duration="100"/>
<item android:drawable="@mipmap/feiben6" android:duration="100"/>
<item android:drawable="@mipmap/feiben7" android:duration="100"/>
<item android:drawable="@mipmap/feiben8" android:duration="100"/>
</animation-list>
<ImageView android:id="@+id/image_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/run"/>
imageView.setBackgroundResource(R.drawable.run);
AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
animationDrawable.start();
补间动画分为四种
位移动画 TranslateAnimation
旋转动画 RotateAnimation
缩放动画 ScaleAnimation
淡入淡出动画 AlphaAnimation
补间动画在res文件夹下新建anim文件夹,下面放置动画的xml文件
setRepeatCount(int repeatCount)
设置动画重复执行的次数
Interpolator定义了动画变化的速率,在Animations框架当中定义了一下几种Interpolator
AccelerateDecelerateInterpolator:在动画开始与结束的地方速率改变比较慢,在中间的时候速率快。
AccelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始加速
CycleInterpolator:动画循环播放特定的次数,速率改变沿着正弦曲线
DecelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始减速
LinearInterpolator:动画以均匀的速率改变
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="5000">
<translate android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="500" android:toYDelta="0"></translate>
</set>
imageView2.clearAnimation();
Animation animationTranslate = AnimationUtils.loadAnimation(this, R.anim.translateanimation);
imageView2.setImageResource(R.mipmap.ic_launcher);
imageView2.setAnimation(animationTranslate);
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="5000">
<rotate android:repeatCount="-1" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:toDegrees="360"></rotate>
</set>
imageView2.clearAnimation();
Animation animationRotate = AnimationUtils.loadAnimation(this, R.anim.rotateanimation);
imageView2.setImageResource(R.mipmap.ic_launcher);
imageView2.setAnimation(animationRotate);
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="5000">
<scale android:fromXScale="1" android:fromYScale="1" android:pivotX="50%" android:pivotY="50%" android:toXScale="2" android:toYScale="2"></scale>
</set>
imageView2.clearAnimation();
Animation animationScale = AnimationUtils.loadAnimation(this, R.anim.scaleanimation);
imageView2.setImageResource(R.mipmap.ic_launcher);
imageView2.setAnimation(animationScale);
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="5000">
<alpha android:fromAlpha="1" android:toAlpha="0"></alpha>
</set>
imageView2.clearAnimation();
Animation animationAlpha = AnimationUtils.loadAnimation(this, R.anim.alphaanimation);
imageView2.setImageResource(R.mipmap.ic_launcher);
imageView2.setAnimation(animationAlpha);
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="5000">
<alpha android:fromAlpha="1" android:toAlpha="0"></alpha>
<rotate android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:toDegrees="360"></rotate>
<translate android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="500" android:toYDelta="0"></translate>
</set>
imageView2.clearAnimation();
Animation animationMany = AnimationUtils.loadAnimation(this, R.anim.manyanimation);
imageView2.setImageResource(R.mipmap.ic_launcher);
imageView2.setAnimation(animationMany);
imageView2.clearAnimation();
Animation animationMany = AnimationUtils.loadAnimation(this, R.anim.manyanimation);
imageView2.setImageResource(R.mipmap.ic_launcher);
imageView2.setAnimation(animationMany);
animationMany.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
Toast.makeText(MainActivity.this, "动画开始啦", Toast.LENGTH_SHORT).show();
}
@Override
public void onAnimationEnd(Animation animation) {
Toast.makeText(MainActivity.this, "动画结束啦", Toast.LENGTH_SHORT).show();
}
@Override
public void onAnimationRepeat(Animation animation) {
Toast.makeText(MainActivity.this, "动画重复啦", Toast.LENGTH_SHORT).show();
}
});