Tween Animation XML實現效果
阿新 • • 發佈:2018-12-20
1,XML實現動畫效果
p是基於父佈局,%必須新增
【1】建立anim檔案
【2】透明動畫(AlphaAnimation)
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fromAlpha="1.0" android:repeatCount="1" android:repeatMode="reverse" android:toAlpha="0.0" > </alpha>
【3】旋轉動畫(RotateAnimation)
<?xml version="1.0" encoding="utf-8"?> <rotate android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:duration="2000" android:repeatCount="1" android:repeatMode="reverse" xmlns:android="http://schemas.android.com/apk/res/android"> </rotate>
【4】縮放動畫(ScaleAnimation)
<?xml version="1.0" encoding="utf-8"?> <scale android:fromXScale="1.0" android:toXScale="3.0" android:fromYScale="1.0" android:toYScale="3.0" android:duration="2000" android:repeatCount="1" android:repeatMode="reverse" android:pivotX="50%" android:pivotY="50%" xmlns:android="http://schemas.android.com/apk/res/android"> </scale>
【5】位移動畫(TranslateAnimation)
<?xml version="1.0" encoding="utf-8"?>
<translate
android:fromXDelta="0%p"
android:toXDelta="0%p"
android:fromYDelta="0%p"
android:toYDelta="20%p"
android:duration="2000"
android:fillAfter="true"
xmlns:android="http://schemas.android.com/apk/res/android">
</translate>
【6】動畫集合(AnimationSet)
<?xml version="1.0" encoding="utf-8"?>
<set>
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromAlpha="1.0"
android:repeatCount="1"
android:repeatMode="reverse"
android:toAlpha="0.0" >
</alpha>
<scale
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="1"
android:repeatMode="reverse"
android:toXScale="3.0"
android:toYScale="3.0" >
</scale>
<translate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fillAfter="true"
android:fromXDelta="0%p"
android:fromYDelta="0%p"
android:toXDelta="0%p"
android:toYDelta="20%p" >
</translate>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="1"
android:repeatMode="reverse"
android:toDegrees="360" >
</rotate>
</set>
【7】程式碼中呼叫: 使用 AnimationUtils工具類
// [1]使用工具類來載入動畫
Animation aa = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.aa);
// [2]iv執行動畫
iv.startAnimation(aa);