android動畫淺析
經過這幾天對android動畫的學習,本人對android動畫有了些淺顯的了解。
android動畫分為三大類:幀動畫(Frame animation)、補間動畫(tween animation)、屬性動畫(property animation
),其中幀動畫、補間動畫是3.0之前的產物。屬性動畫是3.0之后才產生的。
1、幀動畫
幀動畫比較簡單,它就像播放gif一樣一針一針(一針也就是其中的一張圖片)的播放。下面我們就以播放一個gif為例講解一下幀動畫。
1) 首先需要做的是下載一個gif,然后將其分解成一張一張的圖片,網上有許多分解的網站,比如:
www.360doc.com/content/13/0314/18/699582_271506280.shtml
2)現在你應該得到了一個圖片包了,里邊有許多圖片。將其放入drawable文件夾下。
3)在drawable目錄下創建一個xml文件,代碼如下:
<?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)使用
ImageView iv = (ImageView) findViewById(R.id.progressbar); iv.setBackgroundResource(R.drawable.progress_dialog); AnimationDrawable ad = (AnimationDrawable) iv.getBackground(); ad.start();
至此幀動畫使用完成
2、補間動畫,補間動畫很強大能實現我們大部分時候的需要。
首先來說說原理,不見動畫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);
RotateAnimation實現旋轉的動畫效果
主要屬性及說明:
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基類來實現自己想要的效果。
下篇本人將來談談動畫更高級的東西包括屬性動畫以及實現自定義動畫效果。
本篇完