




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第16章動(dòng)畫技術(shù)本章目標(biāo)TweenedAnimation(漸變動(dòng)畫)的使用;FrameAnimation(幀動(dòng)畫)的使用;Animation在Android之中,如果要對(duì)控件進(jìn)行一些動(dòng)畫的處理操作,則可以使用Animation組件進(jìn)行處理,在Animation之中可以對(duì)控件進(jìn)行一些選轉(zhuǎn)、移動(dòng)、淡入淡出等效果,Animation一共分為兩類進(jìn)行操作:TweenedAnimation(漸變動(dòng)畫):該類的Animation可以完成控件的旋轉(zhuǎn)、移動(dòng)、伸縮、淡入淡出等特效;FrameAnimation(幀動(dòng)畫):可以將預(yù)先定義好的對(duì)象按照電影的形式進(jìn)行播放。TweenedAnimationTweenedAnimation表示的是一些基本的動(dòng)畫元素操作,所有的Animation操作的方法都在android.view.animation.Animation類之中定義對(duì)于TweenedAnimation的動(dòng)畫操作有四個(gè)主要的類型:alpha(android.view.animation.AlphaAnimation):定義漸變透明度動(dòng)畫效果,例如:圖片的淡入淡出;scale(android.view.animation.ScaleAnimation):定義動(dòng)畫的伸縮效果;translate(android.view.animation.TranslateAnimation):定義動(dòng)畫的轉(zhuǎn)換位置移動(dòng)的效果;rotate(android.view.animation.RotateAnimation):定義圖片旋轉(zhuǎn)效果的移動(dòng)動(dòng)畫。AnimationSetandroid.view.animation.AnimationSet類可以理解為一個(gè)動(dòng)畫效果的集合,在里面可以同時(shí)保存多個(gè)動(dòng)畫效果,AnimationSet類的繼承結(jié)構(gòu)如下所示:java.lang.Object ?android.view.animation.Animation ?android.view.animation.AnimationSetAnimationSet本身也是Animation的子類,所以許多的方法也可以直接從Animation類繼承下來(lái)直接使用AnimationSet類的常用方法No.方法名稱類型描述1publicAnimationSet(booleanshareInterpolator)構(gòu)造如果設(shè)置為true,則表示使用AnimationSet所提供的Interpolator(速率),如果為false,則使用各個(gè)動(dòng)畫效果自己的Interpolator2publicvoidaddAnimation(Animationa)普通增加一個(gè)Animation組件3publicList<Animation>getAnimations()普通取得所有的Animation組件4publiclonggetDuration()普通取得動(dòng)畫的持續(xù)時(shí)間5publiclonggetStartTime()普通取得動(dòng)畫的開(kāi)始時(shí)間6publicvoidreset()普通重置動(dòng)畫7publicvoidsetDuration(longdurationMillis)普通設(shè)置動(dòng)畫的持續(xù)時(shí)間8publicvoidsetStartTime(longstartTimeMillis)普通設(shè)置動(dòng)畫的開(kāi)始時(shí)間范例:漸變效果范例:定義布局管理器——main.xml<?xmlversion="1.0"encoding="utf-8"?><LinearLayout 線性布局管理器 xmlns:android="" android:orientation="vertical" 所有組件垂直擺放 android:layout_width="fill_parent" 布局管理器寬度為屏幕寬度 android:layout_height="fill_parent"> 布局管理器高度為屏幕高度 <ImageView 定義圖片組件 android:id="@+id/mldn" 組件ID,程序中使用 android:layout_width="fill_parent" 組件寬度為屏幕寬度 android:layout_height="wrap_content" 組件高度為圖片高度 android:src="@drawable/mldn"/> 圖片源文件</LinearLayout>范例:定義Activity程序操作動(dòng)畫public
classMyAnimationDemoextendsActivity{
privateImageViewmldn=null;
@Override
public
voidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.mldn=(ImageView)super.findViewById(R.id.mldn); //取得組件
this.mldn.setOnClickListener(newOnClickListenerImpl());//設(shè)置監(jiān)聽(tīng)
}
private
classOnClickListenerImplimplementsOnClickListener{
@Override
public
voidonClick(Viewview){ AnimationSetset=newAnimationSet(true);//定義一個(gè)動(dòng)畫集
AlphaAnimationalpha=newAlphaAnimation(1,0); //完全顯示-->完全透明
alpha.setDuration(3000); //3秒完成動(dòng)畫
set.addAnimation(alpha); //增加動(dòng)畫
MyAnimationDemo.this.mldn.startAnimation(set); //啟動(dòng)動(dòng)畫
} }}范例:縮放的部分顯示效果以圖片中心為軸進(jìn)行縮放
范例:定義布局管理器——main.xml<?xmlversion="1.0"encoding="utf-8"?><LinearLayout 線性布局管理器 xmlns:android="" android:orientation="vertical" 所有組件垂直擺放 android:layout_width="fill_parent" 布局管理器寬度為屏幕寬度 android:layout_height="fill_parent"> 布局管理器高度為屏幕高度 <ImageView 定義圖片組件 android:id="@+id/mldn" 組件ID,程序中使用 android:layout_width="fill_parent" 組件寬度為屏幕寬度 android:layout_height="fill_parent" 組件高度為屏幕高度 android:src="@drawable/mldn"/> 圖片資源ID</LinearLayout>范例:定義Activity程序進(jìn)行動(dòng)畫處理public
classMyAnimationDemoextendsActivity{
privateImageViewmldn=null;
@Override
public
voidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.mldn=(ImageView)super.findViewById(R.id.mldn);//取得組件
this.mldn.setOnClickListener(newOnClickListenerImpl());//設(shè)置監(jiān)聽(tīng)
}
private
classOnClickListenerImplimplementsOnClickListener{
@Override
public
voidonClick(Viewview){ AnimationSetset=newAnimationSet(true);//定義一個(gè)動(dòng)畫集
ScaleAnimationscale=newScaleAnimation( 1,0.0f, //X軸從滿屏縮小到無(wú)
1,0.0f, //Y軸從滿屏縮小到無(wú)
Animation.RELATIVE_TO_SELF,0.5f,//以自身0.5寬度為軸縮放
Animation.RELATIVE_TO_SELF,0.5f);//以自身0.5高度為軸縮放
scale.setDuration(3000); //3秒完成動(dòng)畫
set.addAnimation(scale); //增加動(dòng)畫
MyAnimationDemo.this.mldn.startAnimation(set); //啟動(dòng)動(dòng)畫
} }}范例:圖片平移范例:定義布局管理器——main.xml<?xmlversion="1.0"encoding="utf-8"?><LinearLayout 線性布局管理器 xmlns:android="" android:orientation="vertical" 所有組件垂直擺放 android:layout_width="fill_parent" 布局管理器寬度為屏幕寬度 android:layout_height="fill_parent"> 布局管理器高度為屏幕高度 <ImageView 圖片組件 android:id="@+id/mldn" 組件ID,程序中使用 android:layout_width="wrap_content" 組件寬度為圖片寬度 android:layout_height="wrap_content" 組件高度為圖片高度 android:src="@drawable/mldn"/> 文件資源ID</LinearLayout>范例:定義Activity程序進(jìn)行平移操作public
classMyAnimationDemoextendsActivity{
privateImageViewmldn=null;
@Override
public
voidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.mldn=(ImageView)super.findViewById(R.id.mldn);//取得組件
this.mldn.setOnClickListener(newOnClickListenerImpl());//設(shè)置監(jiān)聽(tīng)
}
private
classOnClickListenerImplimplementsOnClickListener{
@Override
public
voidonClick(Viewview){ AnimationSetset=newAnimationSet(true); //定義一個(gè)動(dòng)畫集
TranslateAnimationtran=newTranslateAnimation( Animation.RELATIVE_TO_SELF,0.0f, //X軸開(kāi)始位置
Animation.RELATIVE_TO_SELF,0.5f, //X軸結(jié)束位置
Animation.RELATIVE_TO_SELF,0.0f, //Y軸開(kāi)始位置
Animation.RELATIVE_TO_SELF,1.5f); //Y軸結(jié)束位置
tran.setDuration(3000); //3秒完成動(dòng)畫
set.addAnimation(tran); //增加動(dòng)畫
MyAnimationDemo.this.mldn.startAnimation(set); //啟動(dòng)動(dòng)畫
} }}范例:旋轉(zhuǎn)操作范例:定義布局管理器——main.xml<?xmlversion="1.0"encoding="utf-8"?><LinearLayout 線性布局管理器 xmlns:android="" android:orientation="vertical" 所有組件垂直擺放 android:layout_width="fill_parent" 布局管理器寬度為屏幕寬度 android:layout_height="fill_parent"> 布局管理器高度為屏幕高度 <ImageView 定義圖片組件 android:id="@+id/mldn" 組件ID,程序中使用 android:layout_width="wrap_content" 組件寬度為圖片寬度 android:layout_height="wrap_content" 組件高度為圖片高度 android:src="@drawable/mldn"/> 圖片源文件ID</LinearLayout>范例:定義Activity程序public
classMyAnimationDemoextendsActivity{
privateImageViewmldn=null;
@Override
public
voidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.mldn=(ImageView)super.findViewById(R.id.mldn); //取得組件
this.mldn.setOnClickListener(newOnClickListenerImpl());//設(shè)置監(jiān)聽(tīng)
}
private
classOnClickListenerImplimplementsOnClickListener{
@Override
public
voidonClick(Viewview){ AnimationSetset=newAnimationSet(true); //定義一個(gè)動(dòng)畫集
RotateAnimationrotate=newRotateAnimation( 0,360, //旋轉(zhuǎn)角度
Animation.RELATIVE_TO_PARENT,0.5f,//X軸位置為半個(gè)屏幕寬度
Animation.RELATIVE_TO_PARENT,0.0f); //Y軸從原點(diǎn)計(jì)算
rotate.setDuration(3000); //3秒完成動(dòng)畫
set.addAnimation(rotate); //增加動(dòng)畫
MyAnimationDemo.this.mldn.startAnimation(set); //啟動(dòng)動(dòng)畫
} }}范例:動(dòng)畫疊加范例:定義布局文件——main.xml<?xmlversion="1.0"encoding="utf-8"?><LinearLayout 線性布局管理器 xmlns:android="" android:orientation="vertical" 所有組件垂直擺放 android:layout_width="fill_parent" 布局管理器寬度為屏幕寬度 android:layout_height="fill_parent"> 布局管理器高度為屏幕高度 <ImageView 圖片組件 android:id="@+id/mldn" 組件ID,程序中使用 android:layout_width="wrap_content" 組件寬度為圖片寬度 android:layout_height="wrap_content" 組件高度為圖片高度 android:src="@drawable/mldn"/> 圖片的資源ID</LinearLayout>范例:定義Activity程序,進(jìn)行動(dòng)畫效果疊加public
classMyAnimationDemoextendsActivity{
privateImageViewmldn=null;
@Override
public
voidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.mldn=(ImageView)super.findViewById(R.id.mldn);//取得組件
this.mldn.setOnClickListener(newOnClickListenerImpl());//設(shè)置監(jiān)聽(tīng)
}
private
classOnClickListenerImplimplementsOnClickListener{
@Override
public
voidonClick(Viewview){ AnimationSetset=newAnimationSet(true);//定義一個(gè)動(dòng)畫集
TranslateAnimationtran=newTranslateAnimation( Animation.RELATIVE_TO_SELF,0.0f,//X軸開(kāi)始位置
Animation.RELATIVE_TO_SELF,0.5f,//X軸結(jié)束位置
Animation.RELATIVE_TO_SELF,0.0f,//Y軸開(kāi)始位置
Animation.RELATIVE_TO_SELF,1.5f);//Y軸結(jié)束位置
ScaleAnimationscale=newScaleAnimation( 1,0.0f, //X軸從滿屏縮小到無(wú)
1,0.0f, //Y軸從滿屏縮小到無(wú)
Animation.RELATIVE_TO_SELF,0.5f,//自身0.5寬度為軸縮放
Animation.RELATIVE_TO_SELF,0.5f);//自身0.5高度為軸縮放
scale.setRepeatCount(3); //動(dòng)畫重復(fù)3次
set.addAnimation(tran); //增加動(dòng)畫
set.addAnimation(scale); //增加動(dòng)畫
set.setDuration(3000); //動(dòng)畫持續(xù)時(shí)間為3秒
MyAnimationDemo.this.mldn.startAnimation(set); //啟動(dòng)動(dòng)畫
} }}動(dòng)畫速率:Interpolator每當(dāng)實(shí)例化AnimationSet類對(duì)象的時(shí)候都會(huì)定義如下的一個(gè)構(gòu)造方法:AnimationSetset=newAnimationSet(true);//定義一個(gè)動(dòng)畫集在這個(gè)構(gòu)造方法之中要傳遞一個(gè)boolean型的數(shù)據(jù),而且值設(shè)置為true,實(shí)際上這個(gè)boolean型的數(shù)據(jù)就是定義的interpolator,即:動(dòng)畫的執(zhí)行速率,而此時(shí)設(shè)置為true表示所有的速率將交給AnimationSet對(duì)象統(tǒng)一設(shè)置,而各個(gè)不同的動(dòng)畫中的速率效果不起作用,反之則為false。在Android之中使用android.view.animation.Interpolator接口表示,在Interpolator接口中定義了動(dòng)畫的變化速度,可以實(shí)現(xiàn)勻速、正加速、負(fù)加速、無(wú)規(guī)則變加速等,這些分別由不同的子類所實(shí)現(xiàn)。Interpolator接口的常用子類No.子類描述1AccelerateDecelerateInterpolator加速-減速,動(dòng)畫在開(kāi)始與結(jié)束的地方執(zhí)行速度慢,而中間部分時(shí)加速2AccelerateInterpolator加速,動(dòng)畫在開(kāi)始的時(shí)候執(zhí)行速度慢,然后開(kāi)始加速3DecelerateInterpolator減速,動(dòng)畫在開(kāi)始的時(shí)候執(zhí)行速度塊,然后開(kāi)始減速4CycleInterpolator動(dòng)畫循環(huán)播放特定的次數(shù),速率改變沿著正弦曲線變化5LinearInterpolator動(dòng)畫以勻速的方式運(yùn)行范例:定義布局管理器——main.xml<?xmlversion="1.0"encoding="utf-8"?><LinearLayout 線性布局管理器 xmlns:android="" android:orientation="vertical" 所有組件垂直擺放 android:layout_width="fill_parent" 布局管理器寬度為屏幕寬度 android:layout_height="fill_parent"> 布局管理器高度為屏幕高度 <ImageView 圖片組件 android:id="@+id/mldn" 組件ID,程序中使用 android:layout_width="wrap_content" 組件寬度為圖片寬度 android:layout_height="wrap_content" 組件高度為圖片高度 android:src="@drawable/mldn"/> 圖片的資源ID</LinearLayout>范例:定義Activity程序,控制速率public
classMyAnimationDemoextendsActivity{
privateImageViewmldn=null;
@Override
public
voidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.mldn=(ImageView)super.findViewById(R.id.mldn); //取得組件
this.mldn.setOnClickListener(newOnClickListenerImpl());//設(shè)置監(jiān)聽(tīng)
}
private
classOnClickListenerImplimplementsOnClickListener{
@Override
public
voidonClick(Viewview){ AnimationSetset=newAnimationSet(true);//定義一個(gè)動(dòng)畫集
TranslateAnimationtran=newTranslateAnimation( Animation.RELATIVE_TO_SELF,0.0f,//X軸開(kāi)始位置
Animation.RELATIVE_TO_SELF,0.5f,//X軸結(jié)束位置
Animation.RELATIVE_TO_SELF,0.0f,//Y軸開(kāi)始位置
Animation.RELATIVE_TO_SELF,1.5f);//Y軸結(jié)束位置
ScaleAnimationscale=newScaleAnimation(1,0.0f, //X軸從滿屏縮小到無(wú)
1,0.0f, //Y軸從滿屏縮小到無(wú)
Animation.RELATIVE_TO_SELF,0.5f,//自身0.5寬度為軸縮放
Animation.RELATIVE_TO_SELF,0.5f); //自身0.5高度為軸縮放
scale.setRepeatCount(3); //動(dòng)畫重復(fù)3次
set.setInterpolator(newAccelerateInterpolator());//逐步加速
set.addAnimation(tran); //增加動(dòng)畫
set.addAnimation(scale); //增加動(dòng)畫
set.setDuration(2000); //動(dòng)畫持續(xù)時(shí)間為2秒
MyAnimationDemo.this.mldn.startAnimation(set);//啟動(dòng)動(dòng)畫
} }}動(dòng)畫監(jiān)聽(tīng)器:AnimationListener在進(jìn)行動(dòng)畫的操作過(guò)程之中,也可以對(duì)動(dòng)畫的一些操作狀態(tài)進(jìn)行監(jiān)聽(tīng),例如:動(dòng)畫啟動(dòng)、動(dòng)畫重復(fù)執(zhí)行、動(dòng)畫結(jié)束,在Android中專門提供了一個(gè)android.view.animation.Animation.AnimationListener接口,用于完成動(dòng)畫的監(jiān)聽(tīng)操作,在此接口中定義了三個(gè)監(jiān)聽(tīng)動(dòng)畫的操作方法動(dòng)畫開(kāi)始時(shí)觸發(fā):publicabstractvoidonAnimationStart(Animationanimation)動(dòng)畫重復(fù)時(shí)觸發(fā):publicabstractvoidonAnimationRepeat(Animationanimation)動(dòng)畫結(jié)束時(shí)觸發(fā):publicabstractvoidonAnimationEnd(Animationanimation)范例:定義布局管理器——main.xml<?xmlversion="1.0"encoding="utf-8"?><LinearLayout 線性布局管理器 android:id="@+id/layout" 布局管理器ID,程序中使用 xmlns:android="" android:orientation="vertical" 所有組件垂直擺放 android:layout_width="fill_parent" 布局管理器寬度為屏幕寬度 android:layout_height="fill_parent"> 布局管理器高度為屏幕高度 <ImageView 圖片組件 android:id="@+id/mldn" 組件ID,程序中使用 android:layout_width="wrap_content" 組件寬度為圖片寬度 android:layout_height="wrap_content" 組件高度為圖片高度 android:src="@drawable/mldn"/> 顯示的圖片資源ID</LinearLayout>范例:定義Activity程序,使用動(dòng)畫監(jiān)聽(tīng)進(jìn)行動(dòng)畫操作(A)public
classMyAnimationDemoextendsActivity{
privateImageViewmldn=null; //定義圖片視圖
privateViewGroupgroup=null; //定義ViewGroup對(duì)象
@Override
public
voidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.mldn=(ImageView)super.findViewById(R.id.mldn);//取得組件
this.group=(ViewGroup)super.findViewById(R.id.layout);//取得布局管理器
AnimationSetset=newAnimationSet(true);//定義一個(gè)動(dòng)畫集
TranslateAnimationtran=newTranslateAnimation( Animation.RELATIVE_TO_SELF,0.0f,//X軸開(kāi)始位置
Animation.RELATIVE_TO_SELF,0.5f,//X軸結(jié)束位置
Animation.RELATIVE_TO_SELF,0.0f,//Y軸開(kāi)始位置
Animation.RELATIVE_TO_SELF,1.5f);//Y軸結(jié)束位置
tran.setDuration(3000); //3秒完成動(dòng)畫
set.addAnimation(tran); //增加動(dòng)畫
set.setAnimationListener(newAnimationListenerImpl());//設(shè)置監(jiān)聽(tīng)
this.mldn.startAnimation(set); //啟動(dòng)動(dòng)畫
}范例:定義Activity程序,使用動(dòng)畫監(jiān)聽(tīng)進(jìn)行動(dòng)畫操作(B)
private
classAnimationListenerImplimplementsAnimationListener{
@Override
public
voidonAnimationEnd(Animationanimation){ //動(dòng)畫結(jié)束時(shí)觸發(fā)
MyAnimationDemo.this.group .removeView(MyAnimationDemo.this.mldn); //動(dòng)畫結(jié)束后組件消失
}
@Override
public
voidonAnimationRepeat(Animationanimation){ //動(dòng)畫重復(fù)執(zhí)行時(shí)觸發(fā)
}
@Override
public
voidonAnimationStart(Animationanimation){ //動(dòng)畫開(kāi)始時(shí)觸發(fā)
if(animationinstanceofAnimationSet){ //判斷類型
AnimationSetset=(AnimationSet)animation; AlphaAnimationalpha=newAlphaAnimation(1,0);//完全顯示-->完全透明
alpha.setDuration(3000); //3秒完成動(dòng)畫
set.addAnimation(alpha); //增加動(dòng)畫
} } }}通過(guò)XML文件配置動(dòng)畫在Android開(kāi)發(fā)之中,除了可以通過(guò)代碼實(shí)現(xiàn)動(dòng)畫的配置外,也可以通過(guò)XML文件進(jìn)行配置,這樣就使得用戶在不修改程序的情況下實(shí)現(xiàn)對(duì)動(dòng)畫的控制,以達(dá)到有效的程序與配置相分離。在Android中所有定義好的XML文件都要求保存在res/anim文件夾之中,在定義動(dòng)畫的XML文件之中,可以使用下表定義的動(dòng)畫效果元素進(jìn)行配置??啥x的動(dòng)畫效果元素No.可配置的元素描述1<set>為根節(jié)點(diǎn),定義全部的動(dòng)畫元素2<alpha>定義漸變動(dòng)畫效果3<scale>定義縮放動(dòng)畫效果4<translate>定義平移動(dòng)畫效果5<rotate>定義旋轉(zhuǎn)動(dòng)畫效果可以配置的公共屬性No.可配置的屬性數(shù)據(jù)類型描述1android:durationlong定義動(dòng)畫的持續(xù)時(shí)間,以毫秒為單位2android:fillAfterboolean設(shè)置為true表示該動(dòng)畫轉(zhuǎn)化在動(dòng)畫結(jié)束后被應(yīng)用3android:fillBeforeboolean當(dāng)設(shè)置為true,該動(dòng)畫轉(zhuǎn)化在動(dòng)畫開(kāi)始前被應(yīng)用4android:interpolatorString動(dòng)畫插入器,例如:accelerate_decelerate_interpolator(加速-減速動(dòng)畫)、accelerate_interpolator(加速動(dòng)畫)、decelerate_interpolator(減速動(dòng)畫)5android:repeatCountint動(dòng)畫重復(fù)執(zhí)行的次數(shù)6android:repeatModeString動(dòng)畫重復(fù)的模式(restart、reverse)7android:startOffsetlong動(dòng)畫之間的間隔8android:zAdjustmentint動(dòng)畫的ZOrder配置:0(保持ZOrder不變)、1(保持在最上層)、-1(保持在最下層)9android:interpolatorString指定動(dòng)畫的執(zhí)行速率范例:定義res/anim/alpha.xml文件,定義漸變操作配置<?xmlversion="1.0"encoding="utf-8"?><setxmlns:android=""> <alpha 定義漸變動(dòng)畫 android:fromAlpha="1.0" 動(dòng)畫開(kāi)始的alpha值,1表示不透明 android:toAlpha="0.0" 動(dòng)畫結(jié)束的alpha值,0表示完全透明 android:duration="3000"/> 動(dòng)畫持續(xù)的時(shí)間為3秒</set>范例:定義布局管理器——main.xml<?xmlversion="1.0"encoding="utf-8"?><LinearLayout 線性布局管理器 android:id="@+id/layout" 布局管理器ID,程序中使用 xmlns:android="" android:orientation="vertical" 所有組件垂直擺放 android:layout_width="fill_parent" 布局管理器寬度為屏幕寬度 android:layout_height="fill_parent"> 布局管理器高度為屏幕高度 <ImageView 圖片組件 android:id="@+id/mldn" 組件ID,程序中使用 android:layout_width="fill_parent" 組件寬度為圖片寬度 android:layout_height="wrap_content" 組件高度為圖片高度 android:src="@drawable/mldn"/> 顯示的圖片資源ID</LinearLayout>范例:定義Activity程序,讀取alpha.xml文件public
classMyAnimationDemoextendsActivity{
privateImageViewmldn=null;
@Override
public
voidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.mldn=(ImageView)super.findViewById(R.id.mldn); //取得組件
this.mldn.setOnClickListener(newOnClickListenerImpl());//設(shè)置監(jiān)聽(tīng)
}
private
classOnClickListenerImplimplementsOnClickListener{
@Override
public
voidonClick(Viewview){ Animationanim=AnimationUtils.loadAnimation( MyAnimationDemo.this,R.anim.alpha);//讀取動(dòng)畫配置文件
MyAnimationDemo.this.mldn.startAnimation(anim); //啟動(dòng)動(dòng)畫
} }}范例:定義縮放動(dòng)畫配置文件——res/anim/scale.xml<?xmlversion="1.0"encoding="utf-8"?><setxmlns:android=""> <scale 定義縮放動(dòng)畫效果 android:fromXScale="1.0" 組件從X軸滿屏顯示開(kāi)始 android:toXScale="0.0" 組件縮小到無(wú) android:fromYScale="1.0" 組件從Y軸滿屏顯示開(kāi)始 android:toYScale="0.0" 組件縮小到無(wú) android:pivotX="50%" 以自身0.5寬度為軸縮放 android:pivotY="50%" 以自身0.5高度為軸縮放 android:startOffset="100" 動(dòng)畫間隔0.1秒 android:repeatCount="3" 縮放動(dòng)畫重復(fù)三次 android:duration="3000"/> 動(dòng)畫持續(xù)時(shí)間為3秒</set>范例:定義Activity程序,讀取動(dòng)畫配置(部分代碼)
private
classOnClickListenerImplimplementsOnClickListener{
@Override
public
voidonClick(Viewview){ Animationanim=AnimationUtils.loadAnimation( MyAnimationDemo.this,R.anim.scale); //讀取動(dòng)畫配置文件
MyAnimationDemo.this.mldn.startAnimation(anim); //啟動(dòng)動(dòng)畫
} }范例:定義平移動(dòng)畫的配置文件——res/anim/translate.xml<?xmlversion="1.0"encoding="utf-8"?><setxmlns:android=""> <translate 平移動(dòng)畫 android:fromXDelta="0.0" 動(dòng)畫開(kāi)始的X軸位置 android:toXDelta="50%" 動(dòng)畫結(jié)束的長(zhǎng)度為組件的50% android:fromYDelta="0.0" 動(dòng)畫開(kāi)始的Y軸位置 android:toYDelta="150%" 動(dòng)畫結(jié)束的長(zhǎng)度為組件的150% android:duration="3000"/> 動(dòng)畫持續(xù)效果為3秒</set>范例:定義旋轉(zhuǎn)動(dòng)畫的配置文件——res/anim/rotate.xml<?xmlversion="1.0"encoding="utf-8"?><setxmlns:android=""> <rotate 旋轉(zhuǎn)動(dòng)畫 android:fromDegrees="0.0" 動(dòng)畫開(kāi)始角度 android:toDegrees="+360.0" 動(dòng)畫結(jié)束角度 android:pivotX="50%p" 相對(duì)于父控件的50%寬,其中p表示的是parent android:pivotY="0%p" 相對(duì)于父控件的0%高 android:duration="3000"/> 動(dòng)畫持續(xù)時(shí)間為3秒</set>范例:定義平移及縮放的動(dòng)畫配置文件——res/anim/all.xml<?xmlversion="1.0"encoding="utf-8"?><setxmlns:android=""> <translate 平移動(dòng)畫 android:fromXDelta="0.0" 動(dòng)畫開(kāi)始的X軸位置 android:toXDelta="50%" 動(dòng)畫結(jié)束的長(zhǎng)度為組件的50% android:fromYDelta="0.0" 動(dòng)畫開(kāi)始的Y軸位置 android:toYDelta="150%" 動(dòng)畫結(jié)束的長(zhǎng)度為組件的150% android:duration="3000"/> 動(dòng)畫持續(xù)效果為3秒 <scale 定義縮放動(dòng)畫效果 android:fromXScale="1.0" 組件從X軸滿屏顯示開(kāi)始 android:toXScale="0.0" 組件縮小到無(wú) android:fromYScale="1.0" 組件從Y軸滿屏顯示開(kāi)始 android:toYScale="0.0" 組件縮小到無(wú) android:pivotX="50%" 以自身0.5寬度為軸縮放 android:pivotY="50%" 以自身0.5高度為軸縮放 android:startOffset="100" 動(dòng)畫間隔0.1秒 android:repeatCount="3" 縮放動(dòng)畫重復(fù)三次 android:duration="3000"/> 動(dòng)畫持續(xù)時(shí)間為3秒</set>范例:定義速率的配置文件——all.xml<?xmlversion="1.0"encoding="utf-8"?><set xmlns:android="" android:interpolator="@android:anim/accelerate_interpolator" 定義速率為增速 android:shareInterpolator="true"> 所有動(dòng)畫共享此速率配置 <translate 平移動(dòng)畫 android:fromXDelta="0.0" 動(dòng)畫開(kāi)始的X軸位置 android:toXDelta="50%" 動(dòng)畫結(jié)束的長(zhǎng)度為組件的50% android:fromYDelta="0.0" 動(dòng)畫開(kāi)始的Y軸位置 android:toYDelta="150%" 動(dòng)畫結(jié)束的長(zhǎng)度為組件的150% android:duration="3000"/> 動(dòng)畫持續(xù)效果為3秒 <scale 定義縮放動(dòng)畫效果 android:fromXScale="1.0" 組件從X軸滿屏顯示開(kāi)始 android:toXScale="0.0" 組件縮小到無(wú) android:fromYScale="1.0" 組件從Y軸滿屏顯示開(kāi)始 android:toYScale="0.0" 組件縮小到無(wú) android:pivotX="50%" 以自身0.5寬度為軸縮放 android:pivotY="50%" 以自身0.5高度為軸縮放 android:startOffset="100" 動(dòng)畫間隔0.1秒 android:repeatCount="3" 縮放動(dòng)畫重復(fù)三次 android:duration="3000"/> 動(dòng)畫持續(xù)時(shí)間為3秒</set>FrameAnimationFrameAnimation的主要功能是采用幀的方式進(jìn)行動(dòng)畫效果的編排,所有的動(dòng)畫會(huì)按照事先定義好順序執(zhí)行,而后就像電影那樣展現(xiàn)給用戶,如果要想使用這種動(dòng)畫則需要利用android.graphics.drawable.AnimationDrawable類進(jìn)行處理。AnimationDrawable類的常用方法:?jiǎn)?dòng)動(dòng)畫:publicvoidstart()設(shè)置動(dòng)畫執(zhí)行次數(shù):publicvoidsetOneShot(booleanoneShot)true表示一次,false表示執(zhí)行多次可以配置的屬性對(duì)于FrameAnimation動(dòng)畫也可以在XML文件之中進(jìn)行動(dòng)畫的配置,同樣需要將配置文件保存在res/anim文件夾之中,但是此配置文件的根節(jié)點(diǎn)為:“<animation-list>”,里面包含多個(gè)“<item>”元素,用于定義每一幀動(dòng)畫,這兩個(gè)元素可以配置的屬性如表所示。No.屬性描述1android:drawable每一幀動(dòng)畫的資源2android:duration動(dòng)畫的持續(xù)時(shí)間3android:oneshot是否只顯示一次,true為只顯示一次,false為重復(fù)顯示4android:visible定義drawable是否初始可見(jiàn)范例:定義一個(gè)動(dòng)畫配置資源——res/anim/allface.xml<animation-list 定義動(dòng)畫集合 xmlns:android="" android:oneshot="true"> 默認(rèn)為顯示一次 <item 定義動(dòng)畫幀 android:drawable="@drawable/face_01" 引入的圖片資源 android:duration="200"/> 動(dòng)畫持續(xù)時(shí)間為0.2秒 <item 定義動(dòng)畫幀 android:drawable="@drawable/face_02" 引入的圖片資源 android:duration="200"/> 動(dòng)畫持續(xù)時(shí)間為0.2秒 <item 定義動(dòng)畫幀 android:drawable="@drawable/face_03" 引入的圖片資源 android:duration="200"/> 動(dòng)畫持續(xù)時(shí)間為0.2秒 <item 定義動(dòng)畫幀 android:drawable="@drawable/face_04" 引入的圖片資源 android:duration="200"/> 動(dòng)畫持續(xù)時(shí)間為0.2秒 <item 定義動(dòng)畫幀 android:drawable="@drawable/face_05" 引入的圖片資源 android:duration="200"/> 動(dòng)畫持續(xù)時(shí)間為0.2秒 <item 定義動(dòng)畫幀 android:drawable="@drawable/face_06" 引入的圖片資源 android:duration="200"/> 動(dòng)畫持續(xù)時(shí)間為0.2秒 <item 定義動(dòng)畫幀 android:drawable="@drawable/face_07" 引入的圖片資源 android:duration="200"/> 動(dòng)畫持續(xù)時(shí)間為0.2秒 <item 定義動(dòng)畫幀 android:drawable="@drawable/face_08" 引入的圖片資源 android:duration="200"/> 動(dòng)畫持續(xù)時(shí)間為0.2秒</animation-list>范例:定義布局文件,操作動(dòng)畫——main.xml<?xmlversion="1.0"encoding="utf-8"?><LinearLayout 線性布局管理器 xmlns:android="" android:orientation="vertical" 所有組件垂直擺放 android:layout_width="fill_parent" 布局管理器寬度為屏幕寬度 android:layout_height="fill_parent" 布局管理器高度為屏幕高度 android:background="#FFFFFF"> 背景為白色 <ImageView 圖片組件 android:id="@+id/face" 組件ID,程序中使用 android:layout_width="wrap_content" 組件寬度為圖片寬度 android:layout_height="wrap_content"/> 組件高度為圖片高度 <Button 按鈕組件 android:id="@+id/start" 組件ID,程序中使用 android:layout_width="wrap_content" 組件寬度為文字寬度 android:layout_height="wrap_content" 組件高度為文字高度 android:text="開(kāi)始動(dòng)畫"/> 默認(rèn)顯示文字</LinearLayout>范例:定義Activity程序,操作幀動(dòng)畫public
classMyAnimationDemoextendsActivity{
privateImageViewface=null; //圖片組件
privateButtonstart=null; //按鈕組件
privateAnimationDrawabledraw=null; //動(dòng)畫操作
@Override
public
voidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.face=(ImageView)super.findViewById(R.id.face);//取得圖片
this.start=(Button)super.findViewById(R.id.start); //取得按鈕
this.start.setOnClickListener(newOnClickListenerImpl());//設(shè)置監(jiān)聽(tīng)
}
private
classOnClickListenerImplimplementsOnClickListener{
@Override
public
voidonClick(Viewview){ MyAnimationDemo.this.face .setBackgroundResource(R.anim.allface); //設(shè)置動(dòng)畫資源
MyAnimationDemo.this.draw=(AnimationDrawable) MyAnimationDemo.this.face .getBackground(); //取得背景的Drawable MyAnimationDemo.this.draw.setOneShot(false);//動(dòng)畫執(zhí)行次數(shù)
MyAnimationDemo.this.draw.start(); //開(kāi)始動(dòng)畫
} }}LayoutAnimationController組件LayoutAnimationController表示的是在Layout組件上使用動(dòng)畫的操作效果,例如:在進(jìn)行圖片列表顯示的時(shí)候增加一些動(dòng)畫效果,或者是使用ListView增加一些動(dòng)畫效果等,而所增加的動(dòng)畫效果就是之前所使用的漸變、縮放、旋轉(zhuǎn)、平移,與之前的動(dòng)畫操作一樣,LayoutAnimationController可以通過(guò)配置文件完成,也可以利用程序代碼完成。范例:在GridView組件中配置動(dòng)畫范例:定義動(dòng)畫配置文件——res/anim/anim_set.xml<?xmlversion="1.0"encoding="utf-8"?><setxmlns:android=""> <alpha 定義漸變動(dòng)畫 android:fromAlpha="1.0" 動(dòng)畫開(kāi)始的alpha值,1表示不透明 android:toAlpha="0.0" 動(dòng)畫結(jié)束的alpha值,0表示完全透明 android:duration="3000"/> 動(dòng)畫持續(xù)的時(shí)間為3秒 <scale 定義縮放動(dòng)畫效果 android:fromXScale="1.0" 組件從X軸滿屏顯示開(kāi)始 android:toXScale="0.0" 組件縮小到無(wú) android:fromYScale="1.0" 組件從Y軸滿屏顯示開(kāi)始 android:toYScale="0.0" 組件縮小到無(wú) android:pivotX="50%" 以自身0.5寬度為軸縮放 android:pivotY="50%" 以自身0.5高度為軸縮放 android:startOffset="100" 動(dòng)畫間隔0.1秒 android:repeatCount="3" 縮放動(dòng)畫重復(fù)三次 android:duration="3000"/> 動(dòng)畫持續(xù)時(shí)間為3秒</set>范例:配置LayoutAnimationController的配置文件——res/anim/layout_animation.xml<layoutAnimation 配置LayoutAnimationController xmlns:android="" android:delay="0.5" 動(dòng)畫間隔為0.5秒 android:animationOrder="random" 動(dòng)畫是隨機(jī)執(zhí)行 android:animation="@anim/anim_set"/> 引用的動(dòng)畫配置文件在此配置中的有四個(gè)配置屬性:
·android:delay:多個(gè)動(dòng)畫間的間隔時(shí)間,此處設(shè)置的單位為秒;
·android:animationOrder:表示動(dòng)畫的執(zhí)行順序,有三種可選順序:
|-normal:按照順序從頭到尾依次執(zhí)行動(dòng)畫;
|-reverse:按照逆序的方式依次執(zhí)行每一個(gè)動(dòng)畫;
|-random:隨機(jī)順序執(zhí)行動(dòng)畫;
·android:animation:表示要引入的動(dòng)畫配置文件,此時(shí)引入的配置文件是之前所講解的anim_set.xml;
·android:interpolator:配置動(dòng)畫的執(zhí)行速率;范例:定義GridView顯示的圖片適配器——ImageAdapter.javapublic
classImageAdapterextendsBaseAdapter{
privateList<Integer>picRes=newArrayList<Integer>();
privateContextmyContext=null;
publicImageAdapter(Contextc){
this.myContext=c;
this.initPic(); //將所有的圖片資源ID讀取進(jìn)來(lái)
}
public
intgetCount(){
return
this.picRes.size(); }
publicObjectgetItem(intarg0){
return
this.picRes.get(arg0); }
public
longgetItemId(intposition){
return
this.picRes.get(position).intValue(); }
publicViewgetView(intposition,ViewconvertView,ViewGroupparent){ ImageViewimg=newImageView(this.myContext); img.setBackgroundColor(0xFF000000); img.setImageResource(this.picRes.get(position));//給ImageView設(shè)置資源
img.setScaleType(ImageView.ScaleType.CENTER); //居中顯示
img.setLayoutParams(newGridView.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));//布局參數(shù)
img.setPadding(3,3,3,3);//左、上、右、下邊距
returnimg; }
public
voidinitPic(){ Field[]fields=R.drawable.class.getDeclaredFields();
for(intx=0;x<fields.length;x++){
if(fields[x].getName().startsWith("png_")){ //所有png_*命名的圖片
try{ //保存圖片ID
this.picRes.add(fields[x].getInt(R.drawable.class)); }catch(Exceptione){ e.printStackTrace(); } } } }}范例:定義布局管理器——mai
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度競(jìng)業(yè)協(xié)議失效一個(gè)月競(jìng)業(yè)限制解除補(bǔ)償合同
- 二零二五年度大型商場(chǎng)裝修合同(含室內(nèi)外環(huán)境美化)
- 二零二五年度特色主題展臺(tái)設(shè)計(jì)制作安裝一體化合同
- 二零二五年度紋身技藝培訓(xùn)與加盟合作協(xié)議
- 二零二五年度新能源產(chǎn)業(yè)臨時(shí)研發(fā)人員服務(wù)協(xié)議
- 2025年度網(wǎng)絡(luò)安全防護(hù)合同價(jià)款調(diào)整與網(wǎng)絡(luò)安全事件應(yīng)對(duì)
- 二零二五年度虛擬現(xiàn)實(shí)產(chǎn)業(yè)利潤(rùn)分配協(xié)議書
- 二零二五年度搏擊教練員免責(zé)責(zé)任書
- 農(nóng)業(yè)現(xiàn)代化技術(shù)推廣合作協(xié)議
- 智能建筑系統(tǒng)合同
- 工作室成員成長(zhǎng)檔案模板(內(nèi)部版)課件
- 項(xiàng)目滯后趕工措施
- 預(yù)防接種人員崗位培訓(xùn)習(xí)題(Ⅰ類培訓(xùn)練習(xí)題庫(kù)共385題)
- 現(xiàn)場(chǎng)經(jīng)濟(jì)簽證單范本
- 固定義齒工藝流程圖
- 《網(wǎng)店運(yùn)營(yíng)與管理》課件(完整版)
- (高職)員工培訓(xùn)與開(kāi)發(fā)(第四版)完整版教學(xué)課件全套電子教案
- 相親相愛(ài) 簡(jiǎn)譜
- 第四章工具鋼
- 2022年春新冀人版科學(xué)五年級(jí)下冊(cè)全冊(cè)課件
- 服裝購(gòu)銷合同最新版
評(píng)論
0/150
提交評(píng)論