Android動畫之XML(二)

Animation也可以放在XML文件中,這樣程序的可維護性提高瞭。在XML中寫動畫的步驟如下

1.在res文件夾下面新建一個名為anim的文件夾

2.創建xml文件,並首先加入set標簽,改標簽如下

<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
 
</set>

3.在該標簽當中加入rotate,alpha,scale或者translate標簽

4.在代碼當中使用AnimationUtils加載xml文件,並生成Animation對象

Alpha動畫

[java]
<SPAN style="FONT-SIZE: 18px"><?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="https://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_interpolator"> 
    <alpha 
        android:fromAlpha="1.0" 
        android:toAlpha="0.0" 
        android:startOffset="500" 
        android:duration="2000" 
            /> 
 
</set></SPAN> 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:startOffset="500"
        android:duration="2000"
            />

</set>Animation a=AnimationUtils.loadAnimation(this, R.anim.alpha);
iv.startAnimation(a);

Scale動畫

[html]
<SPAN style="FONT-SIZE: 18px"><?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="https://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_interpolator"> 
    <scale 
        android:fromXScale="1.0" 
        android:toXScale="0.0" 
        android:fromYScale="1.0" 
        android:toYScale="0.0" 
        android:pivotX="50%" 
        android:pivotY="50%" 
        android:duration="2000" 
    /> 
 
</set></SPAN> 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <scale
        android:fromXScale="1.0"
        android:toXScale="0.0"
        android:fromYScale="1.0"
        android:toYScale="0.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="2000"
    />

</set>
Rotate動畫

[html]
<SPAN style="FONT-SIZE: 18px"><?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="https://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_interpolator"> 
    <rotate 
        android:fromDegrees="0" 
        android:toDegrees="400" 
        android:pivotX="50%" 
        android:pivotY="50%" 
        android:duration="3000" 
    /> 
 
</set></SPAN> 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <rotate
        android:fromDegrees="0"
        android:toDegrees="400"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="3000"
    />

</set>
Translate動畫

[html]
<SPAN style="FONT-SIZE: 18px"><?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="https://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_interpolator"> 
    <translate 
        android:fromXDelta="50%" 
        android:toXDelta="100%" 
        android:fromYDelta="50%" 
        android:toYDelta="100%" 
        android:duration="3000" 
    /> 
 
</set></SPAN> 

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <translate
        android:fromXDelta="50%"
        android:toXDelta="100%"
        android:fromYDelta="50%"
        android:toYDelta="100%"
        android:duration="3000"
    />

</set>
這裡重點提一下android:pivotX和android:pivotY和android:fromXDelta,android:toXDelta

android:pivotX="50"使用絕對坐標

android:pivotX="50%"相對自己

android:pivotX="50%p"相對父控件

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。