補間動畫分類:
TranslateAnimation(位移動畫)
RotateAnimation(旋轉(zhuǎn)動畫)
ScaleAnimation(縮放動畫)
AlphaAnimation(透明度漸變)
AnimationSet(組合漸變)
1.位移動畫
TranslateAnimation animation =new TranslateAnimation(Animation.RELATIVE_TO_SELF,0f, Animation.RELATIVE_TO_SELF,1f,Animation.RELATIVE_TO_SELF,0f, Animation.RELATIVE_TO_SELF,1f);
animation.setDuration(2000);
animation.setInterpolator(this, android.R.anim.linear_interpolator);
img.startAnimation(animation);
animation.start();
2.旋轉(zhuǎn)動畫
RotateAnimation animation = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 0f);
animation.setDuration(5000);
animation.setInterpolator(this, android.R.anim.accelerate_interpolator);
img.startAnimation(animation);
animation.start();
3.縮放動畫
ValueAnimator animator = ValueAnimator.ofFloat(1.0f, 0.6f, 1.2f, 1.0f, 0.6f, 1.2f, 1.0f);
animator.setDuration(6000L);//設(shè)置縮放時間
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float scale = (Float) animation.getAnimatedValue();
img.setScaleX(scale);
img.setScaleY(scale);
}
});
animator.setInterpolator(new LinearInterpolator());
animator.start();
}
4.透明動畫
AlphaAnimation animation = new AlphaAnimation(1, 0);
animation.setDuration(2000);
animation.setRepeatCount(-1);
img.startAnimation(animation);
animation.start();
5.組合漸變
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="5000"
android:fromXScale="0.2"
android:fromYScale="0.2"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.5"
android:toYScale="1.5" />
<!--fromXScale/fromYScale:沿著X軸/Y軸縮放的起始比例-->
<!--toXScale/toYScale:沿著X軸/Y軸縮放的結(jié)束比例-->
<!--pivotX/pivotY:縮放的中軸點X/Y坐標唯鸭,即距離自身左邊緣的位置,比如50%就是以圖像的 中心為中軸點-->
<rotate
android:duration="5000"
android:fromDegrees="0"
android:repeatCount="1"
android:repeatMode="reverse"
android:toDegrees="360" />
<!--fromDegrees/toDegrees:旋轉(zhuǎn)的起始/結(jié)束角度-->
<!--repeatCount:旋轉(zhuǎn)的次數(shù)明肮,默認值為0,代表一次柿估,假如是其他值,
比如3的妖,則旋轉(zhuǎn)4次 另外,值為-1或者infinite時嫂粟,表示動畫永不停止-->
<!--repeatMode:設(shè)置重復模式墨缘,默認restart星虹,但只有當repeatCount大于0或者infinite或-1時才有效宽涌。
還可以設(shè)置成reverse蝶棋,表示偶數(shù)次顯示動畫時會做方向相反的運動!-->
<translate
android:duration="5000"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="320"
android:toYDelta="0" />
<!--fromXDelta/fromYDelta:動畫起始位置的X/Y坐標-->
<!--toXDelta/toYDelta:動畫結(jié)束位置的X/Y坐標-->
<alpha
android:duration="5000"
android:fromAlpha="1.0"
android:toAlpha="0.1" />
<!--duration :時間-->
<!--fromAlpha :起始透明度-->
<!--toAlpha:結(jié)束透明度-->
</set>
//在MainActivity中的代碼
Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim);
img.startAnimation(animation);