Android自助餐之动画(二)补间动画
查看全套目录
下载完整源代码
明确被添加动画的控件
此处把动画添加到一个ImageView上
针对该控件写动画文件
文件都在res/anim
下
透明度动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="0.2"
android:toAlpha="0.8"
android:duration="1000"
android:fillAfter="true"
android:fillBefore="false"
android:interpolator="@android:interpolator/anticipate_overshoot"
android:repeatMode="reverse"
android:repeatCount="infinite"
/>
</set>
旋转动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="90"
android:toDegrees="-90"
android:pivotX="50%"
android:pivotY="100%"
android:duration="1000"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:interpolator="@android:interpolator/accelerate_decelerate"
/>
</set>
形变动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
>
<!--以下动画同步进行,可自行设置duration和startOffset进行排序-->
<scale
android:fromXScale="100%"
android:toXScale="50%"
android:fromYScale="100%"
android:toYScale="100%"
android:duration="1000"
android:pivotX="50%"
android:pivotY="50%"
android:repeatMode="reverse"
android:repeatCount="infinite"
/>
<scale
android:fromXScale="100%"
android:toXScale="100%"
android:fromYScale="100%"
android:toYScale="50%"
android:duration="1000"
android:pivotX="50%"
android:pivotY="50%"
android:repeatMode="reverse"
android:repeatCount="infinite"
android:startOffset="1000"
/>
</set>
位移动画
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!--以下动画同步进行,可自行设置duration和startOffset进行排序-->
<translate
android:fromXDelta="-100%"
android:toXDelta="150%"
android:fromYDelta="100%"
android:toYDelta="100%"
android:duration="1000"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:interpolator="@android:interpolator/accelerate_decelerate"
/>
<translate
android:fromXDelta="100%"
android:toXDelta="100%"
android:fromYDelta="150%"
android:toYDelta="-100%"
android:duration="1000"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:startOffset="1000"
/>
</set>
播放动画
在需要开始动画的地方添加如下代码:
AnimationSet animationSet = new AnimationSet(this, null);
Animation alphaAnimation = AnimationUtils.loadAnimation(this, R.anim.alpha_anim);
animationSet.addAnimation(alphaAnimation);
Animation scaleAnimation = AnimationUtils.loadAnimation(this, R.anim.scale_anim);
animationSet.addAnimation(scaleAnimation);
Animation translateAnimation = AnimationUtils.loadAnimation(this, R.anim.translate_anim);
animationSet.addAnimation(translateAnimation);
Animation rotateAnimation = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);
animationSet.addAnimation(rotateAnimation);
imageView.startAnimation(animationSet);