對(duì)于動(dòng)畫(huà)的知識(shí)總是用過(guò)就忘了,所以覺(jué)得有必要整理一下。方便以后忘記了可以查看。
補(bǔ)間動(dòng)畫(huà)常用的方法:
android:duration:動(dòng)畫(huà)單次播放時(shí)間。
android:fillAfter:動(dòng)畫(huà)是否保持播放結(jié)束位置。
android:fillBefore:動(dòng)畫(huà)是否保持播放開(kāi)始位置。
android:interpolator:指定動(dòng)畫(huà)播放的速度曲線,不設(shè)定默認(rèn)為勻速。
android:repeatCount:動(dòng)畫(huà)持續(xù)次數(shù),如2,會(huì)播放三次。
android:repeatMode:動(dòng)畫(huà)播放模式。
android:startOffset:動(dòng)畫(huà)延遲播放的時(shí)長(zhǎng),單位是毫秒。
下面分別介紹Animation的幾個(gè)子類(lèi):
AlphaAnimation:控制動(dòng)畫(huà)透明度的變化。RotateAnimation:控制動(dòng)畫(huà)旋轉(zhuǎn)的變化。
ScaleAnimation:控制動(dòng)畫(huà)成比例縮放的變化。TranslateAnimation:控制動(dòng)畫(huà)移動(dòng)的變化。
AnimationSet:以上幾種變化的組合。
一 、AlphaAnimation控制透明度動(dòng)畫(huà)
創(chuàng)建該動(dòng)畫(huà)的時(shí)候要指定動(dòng)畫(huà)開(kāi)始的透明度、結(jié)束時(shí)候的透明度和動(dòng)畫(huà)的持續(xù)時(shí)間。其中透明度可以使用0~1之間的float類(lèi)型的數(shù)字指定,0為透明,1為不透明。
1、使用java代碼:
/**
* 透明度變化
*/
protected void toAlpha() {
// 動(dòng)畫(huà)從透明變?yōu)椴煌该? AlphaAnimation anim = new AlphaAnimation(1.0f, 0.5f);
// 動(dòng)畫(huà)單次播放時(shí)長(zhǎng)為2秒
anim.setDuration(2000);
// 動(dòng)畫(huà)播放次數(shù)
anim.setRepeatCount(2);
// 動(dòng)畫(huà)播放模式為REVERSE
anim.setRepeatMode(Animation.REVERSE);
// 設(shè)定動(dòng)畫(huà)播放結(jié)束后保持播放之后的效果
anim.setFillAfter(true);
// 開(kāi)始播放,iv_anim是一個(gè)ImageView控件
iv_anim.startAnimation(anim);
}
2、使用xml方式:
res\anim\anim_alpha.xml :
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fillAfter="true"
android:fromAlpha="1.0"
android:repeatCount="2"
android:repeatMode="reverse"
android:toAlpha="0.5" >
</alpha>
tweenanimationdemo \ToXMLActivity.java:
/**
* 透明度變化
*/
protected void toAlpha() {
Animation anim=AnimationUtils.loadAnimation(ToXMLActivity.this, R.anim.anim_alpha);
iv_anim.startAnimation(anim);
}
二、RotateAnimation控制旋轉(zhuǎn)動(dòng)畫(huà)
RotateAnimation有多個(gè)構(gòu)造函數(shù),這里展示一個(gè)參數(shù)最多的,下面是它的完整簽名:RotateAnimation(float fromDegrees,float toDegrees,int pivotXType,float pivotXVlaue,int pivotYType,float pivotYValue)
RotateAnimation的構(gòu)造方法中,fromDegrees和toDegrees分別指定動(dòng)畫(huà)開(kāi)始和結(jié)束的旋轉(zhuǎn)角度,pivotXType和pivotYType指定旋轉(zhuǎn)中心的參照類(lèi)型,它們以靜態(tài)常量的形式定義在Animation中,pivotXVlaue和pivotYVa指定旋轉(zhuǎn)中心的位置
1、java實(shí)現(xiàn):
tweenanimationdemo \ToCodeActivity.java:
/**
* 旋轉(zhuǎn)變化
*/
protected void toRotate() {
// 依照?qǐng)D片的中心,從0°旋轉(zhuǎn)到360°
RotateAnimation anim = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
anim.setDuration(2000);
anim.setRepeatCount(2);
anim.setRepeatMode(Animation.REVERSE);
iv_anim.startAnimation(anim);
}
2、xml實(shí)現(xiàn):
res\anim\anim_alpha.xml:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="2"
android:toDegrees="360" >
</rotate>
tweenanimationdemo \ToXMLActivity.java
/**
* 旋轉(zhuǎn)變化
*/
protected void toRotate() {
Animation anim=AnimationUtils.loadAnimation(ToXMLActivity.this, R.anim.anim_rotate);
iv_anim.startAnimation(anim);
}
三、ScaleAnimation控制縮放動(dòng)畫(huà)
ScaleAnimation有多個(gè)構(gòu)造函數(shù),這里介紹一個(gè)參數(shù)最多的ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
ScaleAnimation的構(gòu)造函數(shù)中,fronX、toX、fromY、toY,分別指定了縮放開(kāi)始和結(jié)束的坐標(biāo),pivotXType和pivotYType設(shè)定了縮放的中心類(lèi)型,pivotXValue和pivotYValue設(shè)定了縮放中心的坐標(biāo)。
1、java代碼實(shí)現(xiàn):
tweenanimationdemo \ToCodeActivity.java
/**
* 比例縮放變化
*/
protected void toScale() {
// 以圖片的中心位置,從原圖的20%開(kāi)始放大到原圖的2倍
ScaleAnimation anim = new ScaleAnimation(0.2f, 2.0f, 0.2f, 2.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
anim.setDuration(2000);
anim.setRepeatCount(2);
anim.setRepeatMode(Animation.REVERSE);
iv_anim.startAnimation(anim);
}
2、xml實(shí)現(xiàn):
res\anim\anim_alpha.xml
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:pivotX="50%"
android:pivotY="50%"
android:fromXScale="0.2"
android:fromYScale="0.2"
android:toXScale="2.0"
android:toYScale="2.0" >
</scale>
tweenanimationdemo \ToXMLActivity.java
/**
* 比例縮放變化
*/
protected void toScale() {
Animation anim=AnimationUtils.loadAnimation(ToXMLActivity.this, R.anim.anim_scale);
iv_anim.startAnimation(anim);
}
四、TranslateAnimation控制位移動(dòng)畫(huà)
- TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue) *
在TranslateAnimation構(gòu)造函數(shù)中,它們指定了動(dòng)畫(huà)開(kāi)始的點(diǎn)類(lèi)型以及點(diǎn)位置和動(dòng)畫(huà)移動(dòng)的X、Y點(diǎn)的類(lèi)型以及值。
1、java代碼:
tweenanimationdemo \ToCodeActivity.java
/**
* 位移變化
*/
protected void toTranslate() {
// 從父窗口的(0.1,0.1)的位置移動(dòng)父窗口X軸20%Y軸20%的距離
TranslateAnimation anim = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.1f,
Animation.RELATIVE_TO_PARENT, 0.2f,
Animation.RELATIVE_TO_PARENT, 0.1f,
Animation.RELATIVE_TO_PARENT, 0.2f);
anim.setDuration(2000);
anim.setRepeatCount(2);
anim.setRepeatMode(Animation.REVERSE);
iv_anim.startAnimation(anim);
}
2、xml實(shí)現(xiàn):
res\anim\anim_translate.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXDelta="10%p"
android:toXDelta="20%p"
android:fromYDelta="10%p"
android:toYDelta="20%p"
android:duration="2000"
android:repeatCount="2"
android:repeatMode="reverse">
</translate>
tweenanimationdemo \ToXMLActivity.java
/**
* 位移變化
*/
protected void toTranslate() {
Animation anim=AnimationUtils.loadAnimation(ToXMLActivity.this, R.anim.anim_translate);
iv_anim.startAnimation(anim);
}
五、AnimationSet控制組合動(dòng)畫(huà)
AnimationSet,組合動(dòng)畫(huà),是Animation的子類(lèi)。有些場(chǎng)景需要完成透明度變化、旋轉(zhuǎn)、縮放、移動(dòng)等多種變化。
AnimationSet有多個(gè)構(gòu)造函數(shù),這里介紹一個(gè)最常用的,下面是它的完整簽名:
AnimationSet(boolean shareInterpolator)
AnimationSet的構(gòu)造方法,只有一個(gè)boolean的參數(shù),指定是否每個(gè)動(dòng)畫(huà)分享自己的Interpolator,如果為false,則AnimationSet中的每個(gè)動(dòng)畫(huà),使用自己的Interpolator,如果為true,則AnimationSet中的每個(gè)動(dòng)畫(huà)的Interpolator被AnimationSet的Interpolator覆蓋。
1、java代碼實(shí)現(xiàn):
/**
* 組合動(dòng)畫(huà)
*/
protected void toSetAnim() {
AnimationSet animSet = new AnimationSet(false);
// 依照?qǐng)D片的中心,從0°旋轉(zhuǎn)到360°
RotateAnimation ra = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
ra.setDuration(2000);
ra.setRepeatCount(2);
ra.setRepeatMode(Animation.REVERSE);
// 以圖片的中心位置,從原圖的20%開(kāi)始放大到原圖的2倍
ScaleAnimation sa = new ScaleAnimation(0.2f, 2.0f, 0.2f, 2.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
sa.setDuration(2000);
sa.setRepeatCount(2);
sa.setRepeatMode(Animation.REVERSE);
// 動(dòng)畫(huà)從透明變?yōu)椴煌该? AlphaAnimation aa = new AlphaAnimation(1.0f, 0.5f);
// 動(dòng)畫(huà)單次播放時(shí)長(zhǎng)為2秒
aa.setDuration(2000);
// 動(dòng)畫(huà)播放次數(shù)
aa.setRepeatCount(2);
// 動(dòng)畫(huà)播放模式為REVERSE
aa.setRepeatMode(Animation.REVERSE);
// 設(shè)定動(dòng)畫(huà)播放結(jié)束后保持播放之后的效果
aa.setFillAfter(true);
animSet.addAnimation(sa);
animSet.addAnimation(aa);
animSet.addAnimation(ra);
iv_anim.startAnimation(animSet);
}
** 二、xml實(shí)現(xiàn):**
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<rotate
android:duration="2000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="2"
android:toDegrees="360" >
</rotate>
<scale
android:duration="2000"
android:fromXScale="0.2"
android:fromYScale="0.2"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="2.0"
android:toYScale="2.0" >
</scale>
<alpha
android:duration="2000"
android:fillAfter="true"
android:fromAlpha="1.0"
android:repeatCount="2"
android:repeatMode="reverse"
android:toAlpha="0.5" >
</alpha>
</set>
/**
* 組合動(dòng)畫(huà)
*/
protected void toSetAnim() {
Animation anim=AnimationUtils.loadAnimation(ToXMLActivity.this, R.anim.anim_set);
iv_anim.startAnimation(anim);
}