補(bǔ)間動(dòng)畫分類:
TranslateAnimation(位移動(dòng)畫)
RotateAnimation(旋轉(zhuǎn)動(dòng)畫)
ScaleAnimation(縮放動(dòng)畫)
AlphaAnimation(透明度漸變)
AnimationSet(組合漸變)
1.位移動(dòng)畫
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)動(dòng)畫
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.縮放動(dòng)畫
ValueAnimator animator = ValueAnimator.ofFloat(1.0f, 0.6f, 1.2f, 1.0f, 0.6f, 1.2f, 1.0f);
animator.setDuration(6000L);//設(shè)置縮放時(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.透明動(dòng)畫
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:縮放的中軸點(diǎn)X/Y坐標(biāo)台妆,即距離自身左邊緣的位置仲义,比如50%就是以圖像的 中心為中軸點(diǎn)-->
<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ù)阳距,默認(rèn)值為0葵姥,代表一次遗座,假如是其他值舞丛,
比如3撒踪,則旋轉(zhuǎn)4次 另外偷俭,值為-1或者infinite時(shí)走贪,表示動(dòng)畫永不停止-->
<!--repeatMode:設(shè)置重復(fù)模式佛猛,默認(rèn)restart,但只有當(dāng)repeatCount大于0或者infinite或-1時(shí)才有效坠狡。
還可以設(shè)置成reverse继找,表示偶數(shù)次顯示動(dòng)畫時(shí)會(huì)做方向相反的運(yùn)動(dòng)!-->
<translate
android:duration="5000"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="320"
android:toYDelta="0" />
<!--fromXDelta/fromYDelta:動(dòng)畫起始位置的X/Y坐標(biāo)-->
<!--toXDelta/toYDelta:動(dòng)畫結(jié)束位置的X/Y坐標(biāo)-->
<alpha
android:duration="5000"
android:fromAlpha="1.0"
android:toAlpha="0.1" />
<!--duration :時(shí)間-->
<!--fromAlpha :起始透明度-->
<!--toAlpha:結(jié)束透明度-->
</set>
//在MainActivity中的代碼
Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim);
img.startAnimation(animation);
參考文檔:http://www.runoob.com/w3cnote/android-tutorial-alphaanimation.html