教育行業(yè)A股IPO第一股(股票代碼 003032)

全國(guó)咨詢/投訴熱線:400-618-4000

Android培訓(xùn)之補(bǔ)間動(dòng)畫xml法(二)

更新時(shí)間:2016年03月25日17時(shí)24分 來源:傳智播客Android培訓(xùn)學(xué)院 瀏覽次數(shù):

平移編寫
在anim文件夾下新建一個(gè)trans.xml文件,代碼如例1-5所示:
例1-5
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromXDelta="-50%"
        android:toXDelta="50%"
        android:fromYDelta="-50%"
        android:toYDelta="50%"
        android:duration="2000"
        android:repeatCount="2"
        android:repeatMode="reverse">
</translate>
復(fù)制代碼
在Activity中編寫按鈕點(diǎn)擊事件,代碼如例1-6所示
例1-6
public void trans(View view) {
        Animation aa = AnimationUtils.loadAnimation(this, R.anim.trans);
        iv.setAnimation(aa);
}
復(fù)制代碼
運(yùn)行程序,效果如圖1-4所示:
縮放編寫
在anim文件夾下新建一個(gè)scale.xml文件,代碼如例1-7所示:
例1-7
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromXScale="0.1"
        android:toXScale="2.0"
        android:fromYScale="0.1"
        android:toYScale="2.0"
        android:duration="2000"
        android:repeatCount="2"
        android:repeatMode="reverse">
</scale>
復(fù)制代碼
在Activity中加載該動(dòng)畫資源,代碼如例1-8所示:
例1-8
public void scale(View view) {
        Animation aa = AnimationUtils.loadAnimation(this, R.anim.scale);
        iv.setAnimation(aa);
}
復(fù)制代碼
運(yùn)行程序,效果如圖1-5所示:
復(fù)雜動(dòng)畫
在anim文件夾下新建一個(gè)set.xml文件,在該文件中將上述單個(gè)動(dòng)畫添加到<set>節(jié)點(diǎn)下,代碼如例1-9所示:
例1-9
<?xml version="1.0" encoding="utf-8"?>
<set>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromXDelta="-50%"
        android:toXDelta="50%"
        android:fromYDelta="-50%"
        android:toYDelta="50%"
        android:duration="2000"
        android:repeatCount="2"
        android:repeatMode="reverse">
</translate>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromAlpha="0"
        android:toAlpha="1.0"
        android:duration="2000"
        android:repeatCount="2"
        android:repeatMode="reverse">
</alpha>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromXScale="0.1"
        android:toXScale="2.0"
        android:fromYScale="0.1"
        android:toYScale="2.0"
        android:duration="2000"
        android:repeatCount="2"
        android:repeatMode="reverse">
</scale>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="2000"
        android:repeatCount="2"
        android:repeatMode="reverse">
</rotate>
</set>
復(fù)制代碼
在Activity中編寫按鈕的點(diǎn)擊事件,加載該動(dòng)畫,代碼如例1-10所示:
例1-10
public void set(View view) {
        Animation aa = AnimationUtils.loadAnimation(this, R.anim.set);
        iv.setAnimation(aa);
}
復(fù)制代碼
運(yùn)行程序,效果如圖1-6所示:

本文版權(quán)歸傳智播客Android培訓(xùn)學(xué)院所有,歡迎轉(zhuǎn)載,轉(zhuǎn)載請(qǐng)注明作者出處。謝謝!
作者:傳智播客Android培訓(xùn)學(xué)院
首發(fā):http://metathetuscanyresort.com/android/
0 分享到:
和我們?cè)诰€交談!