最近工作比較清閑,所以想系統的復習和學習下自己比較短缺的知識,所以。。。
程序運行效果圖:
Android動畫主要包含補間動畫(Tween)View Animation、幀動畫(Frame)Drawable Animation、以及屬性動畫Property Animation。下面依次介紹一下各個動畫。
1. 補間動畫(Tween)
Tween動畫,通過對View的內容進行一系列的圖形變換 (包括平移、縮放、旋轉、改變透明度)來實現動畫效果。動畫效果的定義可以采用XML來做也可以采用編碼來做。Tween動畫有4種類型:
動畫的類型
Xml定義動畫使用的配置節點
編碼定義動畫使用的類
漸變透明度動畫效果
AlphaAnimation
漸變尺寸縮放動畫效果
ScaleAnimation
畫面位置移動動畫效果
TranslateAnimation
畫面旋轉動畫效果
RotateAnimation
我們可以為每一個動畫設置動畫插入器,Android自帶的幾種動畫插入器:
AccelerateInterpolator
加速,開始時慢中間加速
DecelerateInterpolator
減速,開始時快然后減速
AccelerateDecelerateInterolator
先加速后減速,開始結束時慢,中間加速
AnticipateInterpolator
反向,先向相反方向改變一段再加速播放
AnticipateOvershootInterpolator
反向加超越,先向相反方向改變,再加速播放,會超出目的值然后緩慢移動至目的值
BounceInterpolator
跳躍,快到目的值時值會跳躍,如目的值100,后面的值可能依次為85,77,70,80,90,100
CycleIinterpolator
循環,動畫循環一定次數,值的改變為一正弦函數:Math.sin(2* mCycles* Math.PI* input)
LinearInterpolator
線性,線性均勻改變
OvershottInterpolator
超越,最后超出目的值然后緩慢改變到目的值
1.1預備知識:
抽象類Animation是一個實現androidUI界面動畫效果的API,Animation是補間動畫的基類,它的直接子類AlphaAnimation, RotateAnimation, ScaleAnimation, TranslateAnimation,AnimationSet,提供了一系列的動畫效果,可以進行淡入淡出、旋轉、縮放、動畫集等,這些效果可以應用在絕大多數的控件中。
1.2AlphaAnimation實現淡入淡出的動畫效果
//方式一通過代碼的方式定義透明度動畫
AnimationalphaAnimation=newAlphaAnimation(1, (float) 0.1);
alphaAnimation.setDuration(3000);//設置動畫持續時間為3秒
alphaAnimation.setFillAfter(true);//設置動畫結束后保持當前的位置(即不返回到動畫開始前的位置)
imgShow.startAnimation(alphaAnimation);
//方式二通過在xml中定義透明度動畫
第一步:定義xml動畫文件:alpha.xml
"1.0"encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="3000"
android:fillAfter="true"
android:repeatCount="2">
第二步:加載xml動畫文件并將其設置到指定的View上
AnimationalphaAnimation2=AnimationUtils.loadAnimation(this, R.anim.alpha);//加載Xml文件中的動畫
imgShow.startAnimation(alphaAnimation2);
程序運行效果圖:
1.3.RotateAnimation實現旋轉的動畫效果
主要屬性及說明:
repeatCount 重復次數
fromDegrees為動畫起始時物件的角度:
當角度為負數——表示逆時針旋轉
當角度為正數——表示順時針旋轉
(負數fromDegrees——toDegrees正數:順時針旋轉)
(負數fromDegrees——toDegrees負數:逆時針旋轉)
(正數fromDegrees——toDegrees正數:順時針旋轉)
(正數fromDegrees——toDegrees負數:逆時針旋轉)
toDegrees屬性為動畫結束時物件旋轉的角度可以大于360度
pivotX,pivotY 為動畫相對于物件的X、Y坐標的開始位.說明:以上兩個屬性值從0%-100%中取值,50%為物件的X或Y方向坐標上的中點位置。
實例:
//方式一通過代碼的方式定義旋轉動畫
AnimationrotateAnimation=newRotateAnimation(0, 45);
rotateAnimation.setDuration(3000);//設置動畫持續時間為3秒
rotateAnimation.setFillAfter(true);//設置動畫結束后保持當前的位置(即不返回到動畫開始前的位置)
imgShow.startAnimation(rotateAnimation);
//方式二通過在xml中定義旋轉動畫
第一步:定義xml動畫文件:rotate.xml
"1.0"encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="45"
android:duration="300"
android:fillAfter="true">
第二步:加載xml動畫文件并將其設置到指定的View上
Animation rotateAnimation2=AnimationUtils.loadAnimation(this, R.anim.rotate);//加載Xml文件中的動畫
imgShow.startAnimation(rotateAnimation2);
程序運行效果圖:
1.4.ScaleAnimation實現縮放動畫效果
主要屬性及說明:
fromXScale(浮點型)屬性為動畫起始時X坐標上的縮放尺寸
fromYScale(浮點型)屬性為動畫起始時Y坐標上的縮放尺寸
toXScale(浮點型)屬性為動畫結束時X坐標上的縮放尺寸
toYScale(浮點型)屬性為動畫結束時Y坐標上的縮放尺寸
說明: 以上四種屬性值
0.0表示收縮到沒有
1.0表示正常無縮放
值小于1.0表示收縮
值大于1.0表示放大
pivotX(浮點型)屬性為動畫相對于物件的X坐標的開始位置
pivotY(浮點型)屬性為動畫相對于物件的Y坐標的開始位置
說明:
以上兩個屬性值從0%-100%中取值
50%為物件的X或Y方向坐標上的中點位置
duration(長整型)屬性為動畫持續時間。說明: 時間以毫秒為單位
fillAfter(布爾型)屬性當設置為true,該動畫轉化在動畫結束后被應用
實例:
//方式一通過代碼的方式定義縮放動畫
AnimationscaleAnimation=newScaleAnimation(0.5f, 1.0f,1.0f, 1.0f);
scaleAnimation.setDuration(2000);//設置動畫持續時間為3秒
scaleAnimation.setFillAfter(true);//設置動畫結束后保持當前的位置(即不返回到動畫開始前的位置)
scaleAnimation.setRepeatCount(3);
imgShow.startAnimation(scaleAnimation);
//方式二通過在xml中定義縮放動畫
第一步:定義xml動畫文件:scale.xml
"1.0"encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:fromXScale="0.5"
android:toXScale="1.0"
android:fromYScale="1.0"
android:toYScale="1.0"
android:duration="3000"
android:fillAfter="true">
第二步:加載xml動畫文件并將其設置到指定的View上
AnimationscaleAnimation2=AnimationUtils.loadAnimation(this, R.anim.scale);//加載Xml文件中的動畫imgShow.startAnimation(scaleAnimation2);
程序運行效果圖:
1.5. TranslateAnimation實現位移動畫效果
//方式一通過代碼的方式定義位移動畫
AnimationtranslateAnimation=newTranslateAnimation(0, 100, 0, 0);
translateAnimation.setDuration(3000);//設置動畫持續時間為3秒
translateAnimation.setInterpolator(this, android.R.anim.cycle_interpolator);//設置動畫插入器
translateAnimation.setFillAfter(true);//設置動畫結束后保持當前的位置(即不返回到動畫開始前的位置)
imgShow.startAnimation(translateAnimation);
//方式二通過在xml中定義位移動畫
第一步:定義xml動畫文件:translate.xml
"1.0"encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:fromXDelta="0"
android:toXDelta="260"
android:fromYDelta="0"
android:toYDelta="600"
android:duration="3600"
android:fillAfter="true"
android:interpolator="@android:anim/accelerate_decelerate_interpolator">
第二步:加載xml動畫文件并將其設置到指定的View上
AnimationtranslateAnimation2=AnimationUtils.loadAnimation(this, R.anim.translate);//加載Xml文件中的動畫
imgShow.startAnimation(translateAnimation2);
程序運行效果圖:
1.6.AnimationSet實現多種動畫混合效果
定義動畫集主要用到了AnimationSet類,該類可以添加多個補間動畫啊。
//方式一通過代碼的方式定義動畫集
AnimationSetanimationSet=newAnimationSet(true);//定義一個動畫集,并設定所有的動畫使用一個動畫插入其
Animation translateAnimation2=AnimationUtils.loadAnimation(this, R.anim.translate);//加載Xml文件中的動畫
AnimationalphaAnimation2=AnimationUtils.loadAnimation(this, R.anim.alpha);//加載Xml文件中的動畫
Animation rotateAnimation2=AnimationUtils.loadAnimation(this, R.anim.rotate);//加載Xml文件中的動畫
Animation scaleAnimation2=AnimationUtils.loadAnimation(this, R.anim.scale);//加載Xml文件中的動畫
animationSet.addAnimation(translateAnimation2);
animationSet.addAnimation(alphaAnimation2);
animationSet.addAnimation(rotateAnimation2);
animationSet.addAnimation(scaleAnimation2);
animationSet.setInterpolator(this, android.R.anim.anticipate_interpolator);
imgShow.startAnimation(animationSet);
//方式二在xml文件中設置動畫集合
第一步:定義xml動畫文件:animset.xml
"1.0"encoding="utf-8"?>
"http://schemas.android.com/apk/res/android">
android:fromAlpha="1.0"
android:toAlpha="0.1"
android:duration="3000"
android:fillAfter="true"
android:repeatCount="2">
android:fromDegrees="0"
android:toDegrees="45"
android:duration="300"
android:fillAfter="true">
android:fromXScale="0.5"
android:toXScale="1.0"
android:fromYScale="1.0"
android:toYScale="1.0"
android:duration="3000"
android:fillAfter="true">
android:fromXDelta="0"
android:toXDelta="260"
android:fromYDelta="0"
android:toYDelta="600"
android:duration="3600"
android:fillAfter="true"
android:interpolator="@android:anim/accelerate_decelerate_interpolator">
第二步:加載xml動畫文件并將其設置到指定的View上
AnimationSetanimationSet2=(AnimationSet) AnimationUtils.loadAnimation(this, R.anim.animset);
程序運行效果圖:
提示:雖然可以通過代碼的方式定義動畫,但是Android官方還是建議在xml中定義動畫效果,這樣可做到最大程度上的解耦,方便項目的后期維護。
2. 幀動畫(Frame)
Frame動畫,即順序播放事先做好的圖像,跟放膠片電影類似。
開發步驟:
(1)把準備好的圖片放進項目res/ drawable下。
(2)在項目的drawable文件夾下面定義動畫XML文件,文件名稱可以自定義。當然也可以采用編碼方式定義動畫效果(使用AnimationDrawable類)。
(3)為View控件綁定動畫效果。調用代表動畫的AnimationDrawable的start()方法開始動畫。
實例:
framelist.xml
"1.0"encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
android:oneshot="false">
"@drawable/zzlx1"android:duration="200"/>
"@drawable/zzlx2"android:duration="200"/>
"@drawable/zzlx3"android:duration="200"/>
"@drawable/zzlx4"android:duration="200"/>
"@drawable/zzlx5"android:duration="200"/>
"@drawable/zzlx6"android:duration="200"/>
"@drawable/zzlx7"android:duration="200"/>
"@drawable/zzlx8"android:duration="200"/>
代碼分析:
上面的XML就定義了一個Frame動畫,其包含8幀動畫,8幀動畫中分別應用了drawable中的8張圖片:zzlx1、zzlx2、zzlx3....,每幀動畫持續200毫秒。android:oneshot屬性如果為true,表示動畫只播放一次停止在最后一幀上,如果設置為false表示動畫循環播放。
在Xml中定義好幀動畫之后就可以將其設置到View上了,請看下面代碼:
//第一步將animation-list設置為ImageView的背景
imgShow.setBackgroundResource(R.drawable.framelist);
//第二步獲取ImagView的背景并將其轉換成AnimationDrawable
AnimationDrawableanimationDrawable=(AnimationDrawable)imgShow.getBackground();
//第三步開始播放動畫
animationDrawable.start();
提示:
有一點需要強調的是:啟動Frame動畫的代碼animationDrawable.start();不能應用在OnCreate()方法中,因為在OnCreate()中AnimationDrawable還沒有完全的與ImageView綁定。在OnCreate()中啟動動畫,只能看到第一張圖片。這里在觸摸事件中實現的
程序運行效果圖:
3. 屬性動畫(Property Animation)
使用屬性動畫注意事項:
1). object必須要提供setXxx方法,如果動畫的時候沒有傳遞初始值,那么還要提供getXxx方法,因為系統要去拿xxx屬性的初始值(如果這條不滿足,程序直接Crash)
2). object的setXxx對屬性xxx所做的改變必須能夠通過某種方法反映出來,比如會帶來ui的改變啥的(如果這條不滿足,動畫無效果但不會Crash)
以上條件缺一不可。
3).對應沒有getXxx和setXxx方法或有getXxx和setXxx方法但和getXxx和setXxx方法設置的屬性不是我們想要的效果如,我們對Button的width屬性做動畫沒有效果?這是因為Button內部雖然提供了getWidth和setWidth方法,但是這個setWidth方法并不是改變視圖的大小,它是TextView新添加的方法,View是沒有這個setWidth方法的,由于Button繼承了TextView,所以Button也就有了setWidth方法.getWidth的確是獲取View的寬度的,而setWidth是TextView和其子類的專屬方法,它的作用不是設置View的寬度,而是設置TextView的最大寬度和最小寬度的,這個和TextView的寬度不是一個東西,具體來說,TextView的寬度對應Xml中的android :layout_width屬性,而TextView還有一個屬性android :width,這個android:width屬性就對應了TextView的setWidth方法。這里給出一種解決方法及使用包裝類:用一個類來包裝原始對象,間接為其提供get和set方法。如下:
/**
*包裝類用于封裝View的with、height,
*你可以通過getXxx以及setXxx方法設置View的寬和高
*@authorjph
*/
classWrapView{
privateViewview;
privateintwidth;
privateintheight;
publicWrapView(View view){
this.view=view;
}
publicintgetWidth() {
returnview.getLayoutParams().width;
}
publicvoidsetWidth(intwidth) {
this.width = width;
view.getLayoutParams().width=width;//修改View的高度
view.requestLayout();//刷新View的布局
}
publicintgetHeight() {
returnview.getLayoutParams().height;
}
publicvoidsetHeight(intheight) {
this.height=height;
view.getLayoutParams().height = height;
view.requestLayout();
}
}
實例:
packagecom.jph.anim;
importandroid.animation.ObjectAnimator;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.View;
importandroid.widget.ImageView;
/**
* 屬性動畫實例
* @author jph
*
*/
public class PropertyActivity ?extends ?Activity {
privateImageView imgShow;
@Override
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.property);
imgShow=(ImageView)findViewById(R.id.imgShow);
}
publicvoidplay(View view) {
switch(view.getId()) {
caseR.id.btnStart:
ObjectAnimator animator=ObjectAnimator.ofInt(newWrapView(imgShow),
"width",10);
animator.setDuration(2000);//設置動畫持續的時間
animator.setRepeatCount(2);//設置動畫重復的次數
animator.start();//開啟動畫
default:
break;
}
}
/**
* 包裝類用于封裝View的with、height,
* 你可以通過getXxx以及setXxx方法設置View的寬和高
* @author jph
*/
classWrapView{
privateView view;
privateintwidth;
privateintheight;
publicWrapView(View view){
this.view=view;
}
publicintgetWidth() {
returnview.getLayoutParams().width;
}
publicvoidsetWidth(intwidth) {
this.width = width;
view.getLayoutParams().width=width;//修改View的高度
view.requestLayout();//刷新View的布局
}
publicintgetHeight() {
returnview.getLayoutParams().height;
}
publicvoidsetHeight(intheight) {
this.height=height;
view.getLayoutParams().height = height;
view.requestLayout();
}
}
}
程序運行效果圖: