Android -- 補(bǔ)間動(dòng)畫(huà)
補(bǔ)間動(dòng)畫(huà)的優(yōu)點(diǎn)是可以節(jié)省空間。補(bǔ)間動(dòng)畫(huà)與逐幀動(dòng)畫(huà)在本質(zhì)上是不同的,逐幀動(dòng)畫(huà)通過(guò)連續(xù)播放圖片來(lái)模擬動(dòng)畫(huà)的效果,而補(bǔ)間動(dòng)畫(huà)則是通過(guò)在兩個(gè)關(guān)鍵幀 之間補(bǔ)充漸變的動(dòng)畫(huà)效果來(lái)實(shí)現(xiàn)的。目前Android應(yīng)用框架支持的補(bǔ)間動(dòng)畫(huà)效果有以下5種。具體實(shí)現(xiàn)在android.view.animation類(lèi) 庫(kù)中。
- AlphaAnimation:透明度(alpha)漸變效果,對(duì)應(yīng)<alpha/>標(biāo)簽。
- TranslateAnimation:位移漸變,需要指定移動(dòng)點(diǎn)的開(kāi)始和結(jié)束坐標(biāo),對(duì)應(yīng)<translate/>標(biāo)簽。
- ScaleAnimation:縮放漸變,可以指定縮放的參考點(diǎn),對(duì)應(yīng)<scale/>標(biāo)簽。
RotateAnimation:旋轉(zhuǎn)漸變,可以指定旋轉(zhuǎn)的參考點(diǎn),對(duì)應(yīng)<rotate/>標(biāo)簽。
- AnimationSet:組合漸變,支持組合多種漸變效果,對(duì)應(yīng)<set/>標(biāo)簽。
補(bǔ)間動(dòng)畫(huà)的效果同樣可以使用XML語(yǔ)言來(lái)定義,這些動(dòng)畫(huà)模板文件通常會(huì)被放在Android項(xiàng)目的res/anim/目錄下。
主代碼
public class MainActivity extends Activity { private ImageView iv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv = (ImageView) findViewById(R.id.iv); } public void click1(View v) { AlphaAnimation ani = new AlphaAnimation(0.0f, 1.0f); ani.setDuration(2000); ani.setRepeatCount(2); ani.setRepeatMode(Animation.REVERSE); iv.startAnimation(ani); } public void click11(View v) { Animation ani = AnimationUtils.loadAnimation(this, R.anim.alpha_anim); iv.startAnimation(ani); } public void click2(View v) { ScaleAnimation ani = new ScaleAnimation(0.0f, 2.0f, 0.0f, 2.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); ani.setDuration(2000); ani.setRepeatCount(2); ani.setRepeatMode(Animation.REVERSE); iv.startAnimation(ani); } public void click22(View v) { Animation ani = AnimationUtils.loadAnimation(this, R.anim.rotate_ani); iv.startAnimation(ani); } public void click3(View v) { RotateAnimation ani = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); ani.setDuration(2000); ani.setRepeatCount(2); ani.setRepeatMode(Animation.REVERSE); iv.startAnimation(ani); } public void click33(View v) { Animation ani = AnimationUtils.loadAnimation(this, R.anim.scale_ani); iv.startAnimation(ani); } public void click4(View v) { TranslateAnimation ani = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f); ani.setDuration(2000); ani.setRepeatCount(2); ani.setRepeatMode(Animation.REVERSE); iv.startAnimation(ani); } public void click44(View v) { Animation ani = AnimationUtils.loadAnimation(this, R.anim.translate); iv.startAnimation(ani); } }
Animation的xml
<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="1.0" android:toAlpha="0.5" android:fillAfter="true" android:duration="2000" > </alpha>
<?xml version="1.0" encoding="utf-8"?> <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" > </rotate>
<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0.2" android:toXScale="2.0" android:fromYScale="0.2" android:toYScale="2.0" android:fillAfter="true" android:duration="2000" > </scale>
<?xml version="1.0" encoding="utf-8"?> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="20%p" android:toXDelta="50%p" android:fromYDelta="0" android:toYDelta="50%p" android:duration="2000" android:repeatCount="2" android:repeatMode="reverse" > </translate>
代碼解析
- alpha
fromAlpha :起始透明度
toAlpha:結(jié)束透明度
1.0表示完全不透明
0.0表示完全透明
- rotate
fromDegrees:表示旋轉(zhuǎn)的起始角度
toDegrees:表示旋轉(zhuǎn)的結(jié)束角度
repeatCount:旋轉(zhuǎn)的次數(shù) 默認(rèn)值是0 代表旋轉(zhuǎn)1次 如果值是repeatCount=4 旋轉(zhuǎn)5次,值為-1或者infinite時(shí),表示補(bǔ)間動(dòng)畫(huà)永不停止
repeatMode 設(shè)置重復(fù)的模式。默認(rèn)是restart。當(dāng)repeatCount的值大于0或者為infinite時(shí)才有效。
repeatCount=-1 或者infinite循環(huán)了 還可以設(shè)成reverse,表示偶數(shù)次顯示動(dòng)畫(huà)時(shí)會(huì)做與動(dòng)畫(huà)文件定義的方向相反的方向動(dòng)行。
- scale
fromXScale:表示沿著x軸縮放的起始比例
toXScale:表示沿著x軸縮放的結(jié)束比例
fromYScale:表示沿著y軸縮放的起始比例
toYScale:表示沿著y軸縮放的結(jié)束比例
圖片中心點(diǎn):
android:pivotX="50%" android:pivotY="50%"
- translate
android:interpolator 動(dòng)畫(huà)的渲染器
accelerate_interpolator(動(dòng)畫(huà)加速器) 使動(dòng)畫(huà)在開(kāi)始的時(shí)候 最慢,然后逐漸加速
decelerate_interpolator(動(dòng)畫(huà)減速器)使動(dòng)畫(huà)在開(kāi)始的時(shí)候 最快,然后逐漸減速
accelerate_decelerate_interpolator(動(dòng)畫(huà)加速減速器)
中間位置分層: 使動(dòng)畫(huà)在開(kāi)始的時(shí)候 最慢,然后逐漸加速
使動(dòng)畫(huà)在開(kāi)始的時(shí)候 最快,然后逐漸減速 結(jié)束的位置最慢
fromXDelta 動(dòng)畫(huà)起始位置的橫坐標(biāo)
toXDelta 動(dòng)畫(huà)起結(jié)束位置的橫坐標(biāo)
fromYDelta 動(dòng)畫(huà)起始位置的縱坐標(biāo)
toYDelta 動(dòng)畫(huà)結(jié)束位置的縱坐標(biāo)
duration 動(dòng)畫(huà)的持續(xù)時(shí)間
在實(shí)際項(xiàng)目中,我們經(jīng)常使用補(bǔ)間動(dòng)畫(huà),原因是補(bǔ)間動(dòng)畫(huà)使用起來(lái)比較方便,功能也比逐幀動(dòng)畫(huà)強(qiáng)大不少,而且還可以很方便地進(jìn)行動(dòng)畫(huà)疊加,實(shí)現(xiàn)更加復(fù)雜的效果。