通過這幾天對android動畫的學習,本人對android動畫有了些淺顯的瞭解。html
android動畫分爲三大類:幀動畫(Frame animation)、補間動畫(tween animation)、屬性動畫(property animationjava
),其中幀動畫、補間動畫是3.0以前的產物。屬性動畫是3.0以後才產生的。android
幀動畫比較簡單,它就像播放gif同樣一針一針(一針也就是其中的一張圖片)的播放。下面咱們就以播放一個gif爲例講解一下幀動畫。框架
1) 首先須要作的是下載一個gif,而後將其分解成一張一張的圖片,網上有許多分解的網站,好比:
學習
www.360doc.com/content/13/0314/18/699582_271506280.shtml動畫
2)如今你應該獲得了一個圖片包了,裏邊有許多圖片。將其放入drawable文件夾下。
網站
3)在drawable目錄下建立一個xml文件,代碼以下:this
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" > <!-- 根標籤爲animation-list,其中oneshot表明着是否只展現一遍,設置爲false會不停的循環播放動畫 根標籤下,經過item標籤對動畫中的每個圖片進行聲明 android:duration 表示展現所用的該圖片的時間長度 --> <item android:drawable="@drawable/loading_0" android:duration="100"/> <item android:drawable="@drawable/loading_1" android:duration="100"/> <item android:drawable="@drawable/loading_2" android:duration="100"/> <item android:drawable="@drawable/loading_3" android:duration="100"/> <item android:drawable="@drawable/loading_4" android:duration="100"/> <item android:drawable="@drawable/loading_5" android:duration="100"/> <item android:drawable="@drawable/loading_6" android:duration="100"/> <item android:drawable="@drawable/loading_7" android:duration="100"/> </animation-list>
4)使用spa
ImageView iv = (ImageView) findViewById(R.id.progressbar); iv.setBackgroundResource(R.drawable.progress_dialog); AnimationDrawable ad = (AnimationDrawable) iv.getBackground(); ad.start();
至此幀動畫使用完成code
首先來講說原理,不見動畫tween animation它是經過你給出兩個關鍵狀態即開始狀態和結束狀態,而後中間的過程由android框架給你自動完成。動畫基本就是此原理,其實很簡單。這樣有人會問了,我不要讓它給我實現我要本身去控制中間的過程,包括效果展現,速度的快慢等等,那要怎麼辦呢?android確定會給咱們留接口的,後面再說。
補間動畫主要分這四種:
漸變尺寸放大縮小:ScaleAnimation
透明度漸變:AlphaAnimation
移動漸變:TranslateAnimation
旋轉漸變:RotateAnimation
上面四個類都繼承至Animation類,這四個類的用法很簡單
AlphaAnimation實現淡入淡出的效果
//方式一經過代碼的方式定義透明度動畫 AlphaAnimation aphaAnimation=new AlphaAnimation(1f,0.1f); alphaAnimation.setDuration(3000);//設置動畫持續時間爲3秒 alphaAnimation.setFillAfter(true);//設置動畫結束後保持當前的位置(即不返回到動畫開始前的位置) imgShow.startAnimation(alphaAnimation);
ScaleAnimation實現放大縮小漸變
主要屬性及說明:
fromXScale(浮點型)屬性爲動畫起始時X座標上的縮放尺寸
fromYScale(浮點型)屬性爲動畫起始時Y座標上的縮放尺寸
toXScale(浮點型) 屬性爲動畫結束時X座標上的縮放尺寸
toYScale(浮點型) 屬性爲動畫結束時Y座標上的縮放尺寸
說明: 以上四種屬性值
0.0表示收縮到沒有
1.0表示正常無縮放
值小於1.0表示收縮
值大於1.0表示放大
pivotX(浮點型) 屬性爲動畫相對於物件的X座標的開始位置
pivotY(浮點型) 屬性爲動畫相對於物件的Y座標的開始位置
//方式一經過代碼的方式定義縮放動畫 Animation scaleAnimation=new ScaleAnimation(0.5f, 1.0f,1.0f, 1.0f); scaleAnimation.setDuration(2000);//設置動畫持續時間爲3秒 scaleAnimation.setFillAfter(true);//設置動畫結束後保持當前的位置(即不返回到動畫開始前的位置) scaleAnimation.setRepeatCount(3); imgShow.startAnimation(scaleAnimation);
TranslateAnimation移動動畫
//方式一經過代碼的方式定義位移動畫 Animation translateAnimation=new TranslateAnimation(0, 100, 0, 0); translateAnimation.setDuration(3000);//設置動畫持續時間爲3秒 translateAnimation.setInterpolator(this, android.R.anim.cycle_interpolator);//設置動畫插入器 translateAnimation.setFillAfter(true);//設置動畫結束後保持當前的位置(即不返回到動畫開始前的位置) imgShow.startAnimation(translateAnimation);
主要屬性及說明:
repeatCount 重複次數
fromDegrees爲動畫起始時物件的角度:
當角度爲負數——表示逆時針旋轉
當角度爲正數——表示順時針旋轉
(負數fromDegrees——toDegrees正數:順時針旋轉)
(負數fromDegrees——toDegrees負數:逆時針旋轉)
(正數fromDegrees——toDegrees正數:順時針旋轉)
(正數fromDegrees——toDegrees負數:逆時針旋轉)
toDegrees屬性爲動畫結束時物件旋轉的角度能夠大於360度
pivotX,pivotY 爲動畫相對於物件的X、Y座標的開始位.說明:以上兩個屬性值從0%-100%中取值,50%爲物件的X或Y方向座標上的中點位置。
Animation rotateAnimation=new RotateAnimation(0, 45); rotateAnimation.setDuration(3000);//設置動畫持續時間爲3秒 rotateAnimation.setFillAfter(true);//設置動畫結束後保持當前的位置(即不返回到動畫開始前的位置) imgShow.startAnimation(rotateAnimation);
同時以上全部的動畫實現方式還能夠經過xml文件來配置,這裏就很少作說明。
AnimationSet實現動畫集合
若是我要實現旋轉的時候同時進行淡入淡出怎麼辦呢?AnimationSet能夠解決此問題。
Animation rotateAnimation=new RotateAnimation(0, 45); rotateAnimation.setDuration(3000);//設置動畫持續時間爲3秒 AlphaAnimation aphaAnimation=new AlphaAnimation(1f,0.1f); alphaAnimation.setDuration(3000);//設置動畫持續時間爲3秒 AnimationSet set = new AnimationSet(true); set.add(rotateAnimation); set.add(alphaAnimation); imageShow.startAnimation(set);
上面只是簡單的幾種動畫,可是現實能夠能有各類各樣的動畫須要,大部分能夠用上面的方式解決,可是有時候咱們須要本身定義本身的動畫效果。AlphaAnimation ScaleAnimation RotationAnImation TransexAnimation這幾個類都是繼承至Animation基類,一樣若是根據實際須要咱們也能夠繼承Animation基類來實現本身想要的效果。
下篇本人未來談談動畫更高級的東西包括屬性動畫以及實現自定義動畫效果。
本篇完