使用代碼編寫:好處:易調試bug, 壞處:可重用性差
使用xml編寫: 好處:n個控件使用同一動畫則只需一個xml文件
使用xml:
1,在res文件夾下面新建一個文件夾,名anim
2,新建xml文件,首先加入一個set標籤,改標籤
<set
xmlns:android="http://schemas.android.com/apk/res/android">
</set>
3,在該標籤中加入子標籤,alpha等。
4,在代碼中使用AnimationUtils加載xml文件,並生成Animation對象。
代碼:
xml文件:
<set
xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:startOffset="500"
android:duration="500"/>
</set>
主程序:
Animation animation=AnimationUtils.loadAnimation(TestActivity.this, R.anim.anim);
iv_icon.setAnimation(animation);