補間動畫
包含漸變Alpha、旋轉Rotate、縮放Scale、平移Translate
佈局代碼
<?xml version="1.0" encoding="utf-8"?>
<!-- duration 執行動畫的時間 fillafter 執行完動畫後,保持最後的效果-->
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="5555"
android:fillAfter="true">
<!-- 透明度 從0 到1 -->
<alpha
android:fromAlpha="0"
android:toAlpha="1" />
<!-- 旋轉 從0度旋轉720 pivot 以基點(中心點) -->
<rotate
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="720"
/>
<!-- 縮放 從1到0.5 從原始狀態,縮放一半,以父佈局中心爲基點 -->
<scale
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0.5"
android:toYScale="0.5" />
<!-- 平移 從屏幕外 平移到屏幕內,注意以佈局的左上角(座標0,0)爲基點 -->
<translate
android:fromXDelta="-100%"
android:fromYDelta="-100%"
android:toXDelta="0"
android:toYDelta="0"></translate>
</set>
rotate、scale動畫的android:pivotX和android:pivotY屬性、translate動畫的android:toXDelta和android:toYDelta屬性的取值都可以是都可以數值、百分數、百分數p,比如:50、50%、50%p,他們取值的代表的意義各不相同:
50表示以View左上角爲原點沿座標軸正方向(x軸向右,y軸向下)偏移50px的位置;
50%表示以View左上角爲原點沿座標軸正方向(x軸向右,y軸向下)偏移View寬度或高度的50%處的位置;
50%p表示以View左上角爲原點沿座標軸正方向(x軸向右,y軸向下)偏移父控件寬度或高度的50%處的位置(p表示相對於ParentView的位置)。
Java代碼
public void clickToSet(View view) {
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
alphaAnimation.setDuration(2000);
RotateAnimation rotateAnimation = new RotateAnimation(
0, 360,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(2000);
ScaleAnimation scaleAnimation = new ScaleAnimation(
1, 0.5f,
1, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(2000);
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 1,
Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 1);
translateAnimation.setDuration(2000);
AnimationSet animationSet = new AnimationSet(true);
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(scaleAnimation);
animationSet.addAnimation(translateAnimation);
mTargetView.startAnimation(animationSet);
}