Android編程之Animation動(dòng)畫(huà)詳解
本文實(shí)例講述了Android編程之Animation動(dòng)畫(huà)用法。分享給大家供大家參考,具體如下:
Animations
一、Animations介紹
Animations是一個(gè)實(shí)現(xiàn)android UI界面動(dòng)畫(huà)效果的API,Animations提供了一系列的動(dòng)畫(huà)效果,可以進(jìn)行旋轉(zhuǎn)、縮放、淡入淡出等,這些效果可以應(yīng)用在絕大多數(shù)的控件中。
二、Animations的分類(lèi)
Animations從總體上可以分為兩大類(lèi):
1.Tweened Animations:該類(lèi)Animations提供了旋轉(zhuǎn)、移動(dòng)、伸展和淡出等效果。Alpha——淡入淡出,Scale——縮放效果,Rotate——旋轉(zhuǎn),Translate——移動(dòng)效果。
2.Frame-by-frame Animations:這一類(lèi)Animations可以創(chuàng)建一個(gè)Drawable序列,這些Drawable可以按照指定的時(shí)間間歇一個(gè)一個(gè)的顯示。
三、Animations的使用方法(代碼中使用)
Animations extends Object implements Cloneable
使用TweenedAnimations的步驟:
1.創(chuàng)建一個(gè)AnimationSet對(duì)象(Animation子類(lèi));
2.增加需要?jiǎng)?chuàng)建相應(yīng)的Animation對(duì)象;
3.更加項(xiàng)目的需求,為Animation對(duì)象設(shè)置相應(yīng)的數(shù)據(jù);
4.將Animatin對(duì)象添加到AnimationSet對(duì)象當(dāng)中;
5.使用控件對(duì)象開(kāi)始執(zhí)行AnimationSet。
Tweened Animations的分類(lèi)
1、Alpha:淡入淡出效果
2、Scale:縮放效果
3、Rotate:旋轉(zhuǎn)效果
4、Translate:移動(dòng)效果
Animation的四個(gè)子類(lèi):
AlphaAnimation、TranslateAnimation、ScaleAnimation、RotateAnimation
四、具體實(shí)現(xiàn)
1、main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/rotateButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="旋轉(zhuǎn)" /> <Button android:id="@+id/scaleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="縮放" /> <Button android:id="@+id/alphaButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="淡入淡出" /> <Button android:id="@+id/translateButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="移動(dòng)" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:src="@drawable/an" /> </LinearLayout> </LinearLayout>
2、.java文件
importandroid.app.Activity; importandroid.os.Bundle; importandroid.view.View; importandroid.view.View.OnClickListener; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; importandroid.view.animation.AnimationSet; importandroid.view.animation.RotateAnimation; importandroid.view.animation.ScaleAnimation; import android.view.animation.TranslateAnimation; importandroid.widget.Button; importandroid.widget.ImageView; public class Animation1Activity extends Activity { private Button rotateButton = null; private Button scaleButton = null; private Button alphaButton = null; private Button translateButton = null; private ImageView image = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); rotateButton = (Button)findViewById(R.id.rotateButton); scaleButton = (Button)findViewById(R.id.scaleButton); alphaButton = (Button)findViewById(R.id.alphaButton); translateButton = (Button)findViewById(R.id.translateButton); image = (ImageView)findViewById(R.id.image); rotateButton.setOnClickListener(newRotateButtonListener()); scaleButton.setOnClickListener(newScaleButtonListener()); alphaButton.setOnClickListener(newAlphaButtonListener()); translateButton.setOnClickListener( new TranslateButtonListener()); } class AlphaButtonListener implementsOnClickListener{ public void onClick(View v) { //創(chuàng)建一個(gè)AnimationSet對(duì)象,參數(shù)為Boolean型, //true表示使用Animation的interpolator,false則是使用自己的 AnimationSet animationSet = new AnimationSet(true); //創(chuàng)建一個(gè)AlphaAnimation對(duì)象,參數(shù)從完全的透明度,到完全的不透明 AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0); //設(shè)置動(dòng)畫(huà)執(zhí)行的時(shí)間 alphaAnimation.setDuration(500); //將alphaAnimation對(duì)象添加到AnimationSet當(dāng)中 animationSet.addAnimation(alphaAnimation); //使用ImageView的startAnimation方法執(zhí)行動(dòng)畫(huà) image.startAnimation(animationSet); } } class RotateButtonListener implementsOnClickListener{ public void onClick(View v) { AnimationSet animationSet = new AnimationSet(true); //參數(shù)1:從哪個(gè)旋轉(zhuǎn)角度開(kāi)始 //參數(shù)2:轉(zhuǎn)到什么角度 //后4個(gè)參數(shù)用于設(shè)置圍繞著旋轉(zhuǎn)的圓的圓心在哪里 //參數(shù)3:確定x軸坐標(biāo)的類(lèi)型,有ABSOLUT絕對(duì)坐標(biāo)、RELATIVE_TO_SELF相對(duì)于自身坐標(biāo)、RELATIVE_TO_PARENT相對(duì)于父控件的坐標(biāo) //參數(shù)4:x軸的值,0.5f表明是以自身這個(gè)控件的一半長(zhǎng)度為x軸 //參數(shù)5:確定y軸坐標(biāo)的類(lèi)型 //參數(shù)6:y軸的值,0.5f表明是以自身這個(gè)控件的一半長(zhǎng)度為x軸 RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f); rotateAnimation.setDuration(1000); animationSet.addAnimation(rotateAnimation); image.startAnimation(animationSet); } } class ScaleButtonListener implementsOnClickListener{ public void onClick(View v) { AnimationSet animationSet = new AnimationSet(true); //參數(shù)1:x軸的初始值 //參數(shù)2:x軸收縮后的值 //參數(shù)3:y軸的初始值 //參數(shù)4:y軸收縮后的值 //參數(shù)5:確定x軸坐標(biāo)的類(lèi)型 //參數(shù)6:x軸的值,0.5f表明是以自身這個(gè)控件的一半長(zhǎng)度為x軸 //參數(shù)7:確定y軸坐標(biāo)的類(lèi)型 //參數(shù)8:y軸的值,0.5f表明是以自身這個(gè)控件的一半長(zhǎng)度為x軸 ScaleAnimation scaleAnimation = new ScaleAnimation( 0, 0.1f,0,0.1f, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f); scaleAnimation.setDuration(1000); animationSet.addAnimation(scaleAnimation); image.startAnimation(animationSet); } } class TranslateButtonListener implementsOnClickListener{ public void onClick(View v) { AnimationSet animationSet = new AnimationSet(true); //參數(shù)1~2:x軸的開(kāi)始位置 //參數(shù)3~4:y軸的開(kāi)始位置 //參數(shù)5~6:x軸的結(jié)束位置 //參數(shù)7~8:x軸的結(jié)束位置 TranslateAnimation translateAnimation = new TranslateAnimation( Animation.RELATIVE_TO_SELF,0f, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0f, Animation.RELATIVE_TO_SELF,0.5f); translateAnimation.setDuration(1000); animationSet.addAnimation(translateAnimation); image.startAnimation(animationSet); } } }
Tween Animations的通用方法
1、setDuration(long durationMills)
設(shè)置動(dòng)畫(huà)持續(xù)時(shí)間(單位:毫秒)
2、setFillAfter(Boolean fillAfter)
如果fillAfter的值為true,則動(dòng)畫(huà)執(zhí)行后,控件將停留在執(zhí)行結(jié)束的狀態(tài)
3、setFillBefore(Boolean fillBefore)
如果fillBefore的值為true,則動(dòng)畫(huà)執(zhí)行后,控件將回到動(dòng)畫(huà)執(zhí)行之前的狀態(tài)
4、setStartOffSet(long startOffSet)
設(shè)置動(dòng)畫(huà)執(zhí)行之前的等待時(shí)間
5、setRepeatCount(int repeatCount)
設(shè)置動(dòng)畫(huà)重復(fù)執(zhí)行的次數(shù)
在代碼中使用Animations可以很方便的調(diào)試、運(yùn)行,但是代碼的可重用性差,重復(fù)代碼多。同樣可以在xml文件中配置Animations,這樣做可維護(hù)性變高了,只不過(guò)不容易進(jìn)行調(diào)試。
一、在xml中使用Animations步驟
1.在res文件夾下建立一個(gè)anim文件夾;
2.創(chuàng)建xml文件,并首先加入set標(biāo)簽,更改標(biāo)簽如下:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> </set>
3.在該標(biāo)簽當(dāng)中加入rotate,alpha,scale或者translate標(biāo)簽;
<alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:startOffset="500" android:duration="500"/>
4.在代碼當(dāng)中使用AnimationUtils當(dāng)中裝載xml文件,并生成Animation對(duì)象。因?yàn)锳nimation是AnimationSet的子類(lèi),所以向上轉(zhuǎn)型,用Animation對(duì)象接收。
Animation animation = AnimationUtils.loadAnimation( Animation1Activity.this, R.anim.alpha); // 啟動(dòng)動(dòng)畫(huà) image.startAnimation(animation);
二、具體實(shí)現(xiàn)
1、 alpha.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <!-- fromAlpha和toAlpha是起始透明度和結(jié)束時(shí)透明度 --> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:startOffset="500" android:duration="500"/> </set>
2、 rotate.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <!-- fromDegrees:開(kāi)始的角度 toDegrees:結(jié)束的角度,+表示是正的 pivotX:用于設(shè)置旋轉(zhuǎn)時(shí)的x軸坐標(biāo) 例 1)當(dāng)值為"50",表示使用絕對(duì)位置定位 2)當(dāng)值為"50%",表示使用相對(duì)于控件本身定位 3)當(dāng)值為"50%p",表示使用相對(duì)于控件的父控件定位 pivotY:用于設(shè)置旋轉(zhuǎn)時(shí)的y軸坐標(biāo) --> <rotate android:fromDegrees="0" android:toDegrees="+360" android:pivotX="50%" android:pivotY="50%" android:duration="1000"/> </set>
3、 scale.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <!-- 起始x軸坐標(biāo) 止x軸坐標(biāo) 始y軸坐標(biāo) 止y軸坐標(biāo) 軸的坐標(biāo) 軸的坐標(biāo) --> <scale android:fromXScale="1.0" android:toXScale="0.0" android:fromYScale="1.0" android:toYScale="0.0" android:pivotX="50%" android:pivotY="50%" android:duration="1000"/> </set>
4、 translate.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"> <!-- 始x軸坐標(biāo) 止x軸坐標(biāo) 始y軸坐標(biāo) 止y軸坐標(biāo) --> <translate android:fromXDelta="0%" android:toXDelta="100%" android:fromYDelta="0%" android:toYDelta="100%" android:duration="2000"/> </set>
5、 .java文件
importandroid.app.Activity; importandroid.os.Bundle; importandroid.view.View; importandroid.view.View.OnClickListener; import android.view.animation.Animation; importandroid.view.animation.AnimationUtils; import android.widget.Button; import android.widget.ImageView; public class Animation1Activity extends Activity { private Button rotateButton = null; private Button scaleButton = null; private Button alphaButton = null; private Button translateButton = null; private ImageView image = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); rotateButton = (Button) findViewById(R.id.rotateButton); scaleButton = (Button) findViewById(R.id.scaleButton); alphaButton = (Button) findViewById(R.id.alphaButton); translateButton = (Button) findViewById(R.id.translateButton); image = (ImageView) findViewById(R.id.image); rotateButton.setOnClickListener(newRotateButtonListener()); scaleButton.setOnClickListener(newScaleButtonListener()); alphaButton.setOnClickListener(newAlphaButtonListener()); translateButton.setOnClickListener(newTranslateButtonListener()); } class AlphaButtonListener implementsOnClickListener { public void onClick(View v) { // 使用AnimationUtils裝載動(dòng)畫(huà)配置文件 Animation animation = AnimationUtils.loadAnimation( Animation1Activity.this, R.anim.alpha); // 啟動(dòng)動(dòng)畫(huà) image.startAnimation(animation); } } class RotateButtonListener implementsOnClickListener { public void onClick(View v) { Animation animation = AnimationUtils.loadAnimation( Animation1Activity.this, R.anim.rotate); image.startAnimation(animation); } } class ScaleButtonListener implementsOnClickListener { public void onClick(View v) { Animation animation = AnimationUtils.loadAnimation( Animation1Activity.this, R.anim.scale); image.startAnimation(animation); } } class TranslateButtonListener implementsOnClickListener { public void onClick(View v) { Animation animation = AnimationUtils.loadAnimation(Animation1Activity.this, R.anim.translate); image.startAnimation(animation); } } }
AnimationSet的具體使用方法
1.AnimationSet是Animation的子類(lèi);
2.一個(gè)AnimationSet包含了一系列的Animation;
3.針對(duì)AnimationSet設(shè)置一些Animation的常見(jiàn)屬性(如startOffset,duration等),可以被包含在AnimationSet當(dāng)中的Animation集成;
例:一個(gè)AnimationSet中有兩個(gè)Animation,效果疊加
第一種方法:
doubleani.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:shareInterpolator="true"> <!-- fromAlpha和toAlpha是起始透明度和結(jié)束時(shí)透明度 --> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:startOffset="500" android:duration="500"/> <translate android:fromXDelta="0%" android:toXDelta="100%" android:fromYDelta="0%" android:toYDelta="100%" android:duration="2000"/> </set>
.java文件中
classDoubleButtonListener implements OnClickListener { public void onClick(View v) { // 使用AnimationUtils裝載動(dòng)畫(huà)配置文件 Animation animation = AnimationUtils.loadAnimation( Animation2Activity.this, R.anim. doubleani); // 啟動(dòng)動(dòng)畫(huà) image.startAnimation(animation); } }
第二種方法:
.java文件中
classDoubleButtonListener implements OnClickListener { public void onClick(View v) { AnimationSet animationSet = new AnimationSet(true); AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0); RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f); rotateAnimation.setDuration(1000); animationSet.addAnimation(rotateAnimation); animationSet.addAnimation(alphaAnimation); image.startAnimation(animationSet); } }
Interpolator的具體使用方法
Interpolator定義了動(dòng)畫(huà)變化的速率,在Animations框架當(dāng)中定義了以下幾種Interpolator
? AccelerateDecelerateInterpolator:在動(dòng)畫(huà)開(kāi)始與結(jié)束的地方速率改變比較慢,在中間的時(shí)候速率快。
? AccelerateInterpolator:在動(dòng)畫(huà)開(kāi)始的地方速率改變比較慢,然后開(kāi)始加速
? CycleInterpolator:動(dòng)畫(huà)循環(huán)播放特定的次數(shù),速率改變沿著正弦曲線
? DecelerateInterpolator:在動(dòng)畫(huà)開(kāi)始的地方速率改變比較慢,然后開(kāi)始減速
? LinearInterpolator:動(dòng)畫(huà)以均勻的速率改變
分為以下幾種情況:
1、在set標(biāo)簽中
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator"/>
2、如果在一個(gè)set標(biāo)簽中包含多個(gè)動(dòng)畫(huà)效果,如果想讓這些動(dòng)畫(huà)效果共享一個(gè)Interpolator。
3、如果不想共享一個(gè)interpolator,則設(shè)置android:shareInterpolator="true",并且需要在每一個(gè)動(dòng)畫(huà)效果處添加interpolator。
<alpha android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromAlpha="1.0" android:toAlpha="0.0" android:startOffset="500" android:duration="500"/>
4、如果是在代碼上設(shè)置共享一個(gè)interpolator,則可以在AnimationSet設(shè)置interpolator。
AnimationSet animationSet = newAnimationSet(true); animationSet.setInterpolator(new AccelerateInterpolator());
5、如果不設(shè)置共享一個(gè)interpolator則可以在每一個(gè)Animation對(duì)象上面設(shè)置interpolator。
AnimationSet animationSet = newAnimationSet(false); alphaAnimation.setInterpolator(new AccelerateInterpolator()); rotateAnimation.setInterpolator(new DecelerateInterpolator());
Frame-By-Frame Animations的使用方法
Frame-By-Frame Animations是一幀一幀的格式顯示動(dòng)畫(huà)效果。類(lèi)似于電影膠片拍攝的手法。
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="horizontal" android:layout_height="wrap_content" android:layout_width="wrap_content"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="運(yùn)動(dòng)"/> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true"/> </LinearLayout> </LinearLayout>
3、anim.xml
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/a_01" android:duration="50"/> <item android:drawable="@drawable/a_02" android:duration="50"/> <item android:drawable="@drawable/a_03" android:duration="50"/> <item android:drawable="@drawable/a_04" android:duration="50"/> <item android:drawable="@drawable/a_05" android:duration="50"/> <item android:drawable="@drawable/a_06" android:duration="50"/> </animation-list>
4、.java文件
importandroid.app.Activity; importandroid.graphics.drawable.AnimationDrawable; importandroid.os.Bundle; importandroid.view.View; importandroid.view.View.OnClickListener; importandroid.widget.Button; importandroid.widget.ImageView; public class AnimationsActivity extends Activity { private Button button = null; private ImageView imageView = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button)findViewById(R.id.button); imageView = (ImageView)findViewById(R.id.image); button.setOnClickListener(newButtonListener()); } class ButtonListener implementsOnClickListener{ public void onClick(View v) { imageView.setBackgroundResource(R.anim.anim); AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground(); animationDrawable.start(); } } }
LayoutAnimationsController
1、什么是LayoutAnimationsController
LayoutAnimationsController可以用于實(shí)現(xiàn)使多個(gè)控件按順序一個(gè)一個(gè)的顯示。
1)LayoutAnimationsController用于為一個(gè)layout里面的控件,或者是一個(gè)ViewGroup里面的控件設(shè)置統(tǒng)一的動(dòng)畫(huà)效果。
2)每一個(gè)控件都有相同的動(dòng)畫(huà)效果。
3)控件的動(dòng)畫(huà)效果可以在不同的時(shí)間顯示出來(lái)。
4)LayoutAnimationsController可以在xml文件當(dāng)中設(shè)置,以可以在代碼當(dāng)中進(jìn)行設(shè)置。
2、在xml當(dāng)中使用LayoutAnimationController
1)在res/anim文件夾下創(chuàng)建一個(gè)名為list_anim_layout.xml文件:
android:delay - 動(dòng)畫(huà)間隔時(shí)間;子類(lèi)動(dòng)畫(huà)時(shí)間間隔 (延遲) 70% 也可以是一個(gè)浮點(diǎn)數(shù) 如“1.2”等
android:animationOrder - 動(dòng)畫(huà)執(zhí)行的循序(normal:順序,random:隨機(jī),reverse:反向顯示)
android:animation – 引用動(dòng)畫(huà)效果文件
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" android:delay="0.5" android:animationOrder="normal" android:animation="@anim/list_anim"/>
2)創(chuàng)建list_anim.xml文件,設(shè)置動(dòng)畫(huà)效果
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:shareInterpolator="true"> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="1000"/> </set>
3)在布局文件main.xml當(dāng)中為L(zhǎng)istVIew添加如下配置
<ListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scrollbars="vertical" android:layoutAnimation="@anim/list_anim_layout"/>
4)程序結(jié)構(gòu)
5)list_anim_layout.xml
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android" android:delay="0.5" android:animationOrder="normal" android:animation="@anim/list_anim"/>
6)list_anim.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:shareInterpolator="true"> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="1000"/> </set>
7)main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@id/android:list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:scrollbars="vertical" android:layoutAnimation="@anim/list_anim_layout"/> <Button android:id="@+id/button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="測(cè)試"/> </LinearLayout>
8)item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:paddingLeft="10dip" android:paddingRight="10dip" android:paddingTop="1dip" android:paddingBottom="1dip"> <TextView android:id="@+id/name" android:layout_width="180dip" android:layout_height="30dip" android:textSize="5pt" android:singleLine="true" /> <TextView android:id="@+id/sex" android:layout_width="fill_parent" android:layout_height="fill_parent" android:textSize="5pt" android:singleLine="true"/> </LinearLayout>
9)java文件
public class Animation2Activity extendsListActivity { private Button button = null; private ListView listView = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); listView = getListView(); button = (Button)findViewById(R.id.button); button.setOnClickListener(newButtonListener()); } private ListAdapter createListAdapter() { List<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>(); HashMap<String,String> m1 = new HashMap<String,String>(); m1.put("name", "bauble"); m1.put("sex", "male"); HashMap<String,String> m2 = new HashMap<String,String>(); m2.put("name", "Allorry"); m2.put("sex", "male"); HashMap<String,String> m3 = new HashMap<String,String>(); m3.put("name", "Allotory"); m3.put("sex", "male"); HashMap<String,String> m4 = new HashMap<String,String>(); m4.put("name", "boolbe"); m4.put("sex", "male"); list.add(m1); list.add(m2); list.add(m3); list.add(m4); SimpleAdapter simpleAdapter = new SimpleAdapter( this,list,R.layout.item,new String[]{"name","sex"}, new int[]{R.id.name,R.id.sex}); return simpleAdapter; } private class ButtonListener implementsOnClickListener{ public void onClick(View v) { listView.setAdapter(createListAdapter()); } } }
備注:要將整個(gè)動(dòng)畫(huà)效果設(shè)置到LinerLayout中,可以這樣設(shè)置:
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layoutAnimation="@anim/list_anim_layout" >
3、在代碼當(dāng)中使用LayoutAnimationController
1)去掉main.xml中的android:layoutAnimation="@anim/list_anim_layout"/>
2)創(chuàng)建一個(gè)Animation對(duì)象:可以通過(guò)裝載xml文件,或者是直接使用Animation的構(gòu)造方法創(chuàng)建Animation對(duì)象;
Animation animation = (Animation) AnimationUtils.loadAnimation( Animation2Activity.this, R.anim.list_anim);
3)創(chuàng)建LayoutAnimationController對(duì)象:
4)設(shè)置控件的顯示順序以及延遲時(shí)間
controller.setOrder(LayoutAnimationController.ORDER_NORMAL); controller.setDelay(0.5f);
5)為L(zhǎng)istView設(shè)置LayoutAnimationController屬性:
完整代碼:
private class ButtonListener implementsOnClickListener { public void onClick(View v) { listView.setAdapter(createListAdapter()); Animation animation = (Animation) AnimationUtils.loadAnimation( Animation2Activity.this, R.anim.list_anim); LayoutAnimationController controller = new LayoutAnimationController(animation); controller.setOrder(LayoutAnimationController.ORDER_NORMAL); controller.setDelay(0.5f); listView.setLayoutAnimation(controller); } }
AnimationListener
1、什么是AnimationListener
1).AnimationListener是一個(gè)監(jiān)聽(tīng)器,該監(jiān)聽(tīng)器在動(dòng)畫(huà)執(zhí)行的各個(gè)階段會(huì)得到通知,從而調(diào)用相應(yīng)的方法;
2).AnimationListener主要包括如下三個(gè)方法:
① onAnimationEnd(Animation animation) - 當(dāng)動(dòng)畫(huà)結(jié)束時(shí)調(diào)用
② onAnimationRepeat(Animation animation) - 當(dāng)動(dòng)畫(huà)重復(fù)時(shí)調(diào)用
③ onAniamtionStart(Animation animation) - 當(dāng)動(dòng)畫(huà)啟動(dòng)時(shí)調(diào)用
2、具體實(shí)現(xiàn)
1)main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/addButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="添加圖片" /> <Button android:id="@+id/deleteButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_above="@id/addButton" android:text="刪除圖片" /> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_marginTop="100dip" android:src="@drawable/an" /> </RelativeLayout>
2).java文件
public class Animation2Activity extends Activity { private Button addButton = null; private Button deleteButton = null; private ImageView imageView = null; private ViewGroup viewGroup = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); addButton = (Button)findViewById(R.id.addButton); deleteButton = (Button)findViewById(R.id.deleteButton); imageView = (ImageView)findViewById(R.id.image); //LinearLayout下的一組控件 viewGroup = (ViewGroup)findViewById(R.id.layout); addButton.setOnClickListener(newAddButtonListener()); deleteButton.setOnClickListener(newDeleteButtonListener()); } private class AddButtonListener implements OnClickListener{ public void onClick(View v) { //淡入 AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f); animation.setDuration(1000); animation.setStartOffset(500); //創(chuàng)建一個(gè)新的ImageView ImageView newImageView = new ImageView( Animation2Activity.this); newImageView.setImageResource(R.drawable.an); viewGroup.addView(newImageView, new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); newImageView.startAnimation(animation); } } private class DeleteButtonListener implements OnClickListener{ public void onClick(View v) { //淡出 AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f); animation.setDuration(1000); animation.setStartOffset(500); //為Aniamtion對(duì)象設(shè)置監(jiān)聽(tīng)器 animation.setAnimationListener( new RemoveAnimationListener()); imageView.startAnimation(animation); } } private class RemoveAnimationListener implements AnimationListener{ //動(dòng)畫(huà)效果執(zhí)行完時(shí)remove public void onAnimationEnd(Animation animation) { System.out.println("onAnimationEnd"); viewGroup.removeView(imageView); } public void onAnimationRepeat(Animation animation) { System.out.println("onAnimationRepeat"); } public void onAnimationStart(Animation animation) { System.out.println("onAnimationStart"); } } }
3、總結(jié)一下
可以在Activity中動(dòng)態(tài)添加和刪除控件,方法是:
1)取到那個(gè)Layout
2)添加時(shí),先創(chuàng)建對(duì)象,然后添加
ImageView newImageView = new ImageView( Animation2Activity.this); newImageView.setImageResource(R.drawable.an); viewGroup.addView(newImageView, new LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
3)刪除時(shí),直接刪除。
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- 圖文詳解Android屬性動(dòng)畫(huà)
- Android自定義加載控件實(shí)現(xiàn)數(shù)據(jù)加載動(dòng)畫(huà)
- 詳解Android Material Design自定義動(dòng)畫(huà)的編寫(xiě)
- Android中使用Vectors(2)繪制優(yōu)美的路徑動(dòng)畫(huà)
- Android中播放Gif動(dòng)畫(huà)取巧的辦法
- Android模仿知乎的回答詳情頁(yè)的動(dòng)畫(huà)效果
- Android開(kāi)發(fā)之Animations動(dòng)畫(huà)用法實(shí)例詳解
- Android動(dòng)畫(huà)之補(bǔ)間動(dòng)畫(huà)(Tween Animation)實(shí)例詳解
- Android編程中Tween動(dòng)畫(huà)和Frame動(dòng)畫(huà)實(shí)例分析
- Android編程之簡(jiǎn)單逐幀動(dòng)畫(huà)Frame的實(shí)現(xiàn)方法
- Android中Property Animation屬性動(dòng)畫(huà)編寫(xiě)的實(shí)例教程
相關(guān)文章
Android可循環(huán)顯示圖像的Android Gallery組件用法實(shí)例
這篇文章主要介紹了Android可循環(huán)顯示圖像的Android Gallery組件用法,結(jié)合實(shí)例形式分析了Gallery組件的功能,使用方法及相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-04-04Android FTP 多線程斷點(diǎn)續(xù)傳下載\上傳的實(shí)例
本篇文章主要介紹了Android FTP 多線程斷點(diǎn)續(xù)傳下載\上傳的實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08Android項(xiàng)目開(kāi)發(fā)常用工具類(lèi)LightTaskUtils源碼介紹
LightTaskUtils是一個(gè)輕量級(jí)的線程管理工具,本文通過(guò)實(shí)例代碼給大家詳細(xì)介紹Android項(xiàng)目開(kāi)發(fā)常用工具類(lèi)LightTaskUtils的相關(guān)知識(shí),感興趣的朋友一起看看吧2022-06-06Android LeakCanary檢測(cè)內(nèi)存泄露原理
這篇文章主要介紹了分析LeakCanary檢測(cè)內(nèi)存泄露原理,幫助大家更好的理解和學(xué)習(xí)使用Android開(kāi)發(fā),感興趣的朋友可以了解下2021-03-03Android為T(mén)extView添加字體庫(kù)和設(shè)置描邊的方法
本篇文章主要介紹了Android為T(mén)extView添加字體庫(kù)和設(shè)置描邊的方法,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-09Android開(kāi)發(fā)中模仿qq列表信息滑動(dòng)刪除功能
這篇文章主要介紹了Android開(kāi)發(fā)中模仿qq列表信息滑動(dòng)刪除功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01Android實(shí)現(xiàn)的狀態(tài)欄定制和修改方法
這篇文章主要介紹了Android實(shí)現(xiàn)的狀態(tài)欄定制和修改方法,涉及Android針對(duì)狀態(tài)欄屬性設(shè)置的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10詳解Java編程中的反射在Android開(kāi)發(fā)中的應(yīng)用
這篇文章主要介紹了詳解Java編程中的反射在Android開(kāi)發(fā)中的應(yīng)用,主要來(lái)獲取安卓系統(tǒng)的屬性值,需要的朋友可以參考下2015-07-07Android實(shí)現(xiàn)在屏幕上移動(dòng)圖片的方法
這篇文章主要介紹了Android實(shí)現(xiàn)在屏幕上移動(dòng)圖片的方法,實(shí)例分析了Android操作圖片的相關(guān)技巧,需要的朋友可以參考下2015-06-06Android使用RollViewPager實(shí)現(xiàn)輪播圖
這篇文章主要為大家詳細(xì)介紹了Android使用RollViewPager實(shí)現(xiàn)輪播圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04