本文實例講述了Android編程之Animation動畫用法。分享給大家供大家參考,具體如下:
Animations
一、Animations介紹
Animations是一個實現android UI界面動畫效果的API,Animations提供了一系列的動畫效果,可以進行旋轉、縮放、淡入淡出等,這些效果可以應用在絕大多數的控件中。
二、Animations的分類
Animations從總體上可以分為兩大類:
1.Tweened Animations:該類Animations提供了旋轉、移動、伸展和淡出等效果。Alpha――淡入淡出,Scale――縮放效果,Rotate――旋轉,Translate――移動效果。
2.Frame-by-frame Animations:這一類Animations可以創建一個Drawable序列,這些Drawable可以按照指定的時間間歇一個一個的顯示。
三、Animations的使用方法(代碼中使用)
Animations extends Object implements Cloneable
使用TweenedAnimations的步驟:
1.創建一個AnimationSet對象(Animation子類);
2.增加需要創建相應的Animation對象;
3.更加項目的需求,為Animation對象設置相應的數據;
4.將Animatin對象添加到AnimationSet對象當中;
5.使用控件對象開始執行AnimationSet。
Tweened Animations的分類
1、Alpha:淡入淡出效果
2、Scale:縮放效果
3、Rotate:旋轉效果
4、Translate:移動效果
Animation的四個子類:
AlphaAnimation、TranslateAnimation、ScaleAnimation、RotateAnimation
四、具體實現
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="旋轉" /> <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="移動" /> </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) { //創建一個AnimationSet對象,參數為Boolean型, //true表示使用Animation的interpolator,false則是使用自己的 AnimationSet animationSet = new AnimationSet(true); //創建一個AlphaAnimation對象,參數從完全的透明度,到完全的不透明 AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0); //設置動畫執行的時間 alphaAnimation.setDuration(500); //將alphaAnimation對象添加到AnimationSet當中 animationSet.addAnimation(alphaAnimation); //使用ImageView的startAnimation方法執行動畫 image.startAnimation(animationSet); } } class RotateButtonListener implementsOnClickListener{ public void onClick(View v) { AnimationSet animationSet = new AnimationSet(true); //參數1:從哪個旋轉角度開始 //參數2:轉到什么角度 //后4個參數用于設置圍繞著旋轉的圓的圓心在哪里 //參數3:確定x軸坐標的類型,有ABSOLUT絕對坐標、RELATIVE_TO_SELF相對于自身坐標、RELATIVE_TO_PARENT相對于父控件的坐標 //參數4:x軸的值,0.5f表明是以自身這個控件的一半長度為x軸 //參數5:確定y軸坐標的類型 //參數6:y軸的值,0.5f表明是以自身這個控件的一半長度為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); //參數1:x軸的初始值 //參數2:x軸收縮后的值 //參數3:y軸的初始值 //參數4:y軸收縮后的值 //參數5:確定x軸坐標的類型 //參數6:x軸的值,0.5f表明是以自身這個控件的一半長度為x軸 //參數7:確定y軸坐標的類型 //參數8:y軸的值,0.5f表明是以自身這個控件的一半長度為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); //參數1~2:x軸的開始位置 //參數3~4:y軸的開始位置 //參數5~6:x軸的結束位置 //參數7~8:x軸的結束位置 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)
設置動畫持續時間(單位:毫秒)
2、setFillAfter(Boolean fillAfter)
如果fillAfter的值為true,則動畫執行后,控件將停留在執行結束的狀態
3、setFillBefore(Boolean fillBefore)
如果fillBefore的值為true,則動畫執行后,控件將回到動畫執行之前的狀態
4、setStartOffSet(long startOffSet)
設置動畫執行之前的等待時間
5、setRepeatCount(int repeatCount)
設置動畫重復執行的次數
在代碼中使用Animations可以很方便的調試、運行,但是代碼的可重用性差,重復代碼多。同樣可以在xml文件中配置Animations,這樣做可維護性變高了,只不過不容易進行調試。
一、在xml中使用Animations步驟
1.在res文件夾下建立一個anim文件夾;
2.創建xml文件,并首先加入set標簽,更改標簽如下:
<?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.在該標簽當中加入rotate,alpha,scale或者translate標簽;
<alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:startOffset="500" android:duration="500"/>
4.在代碼當中使用AnimationUtils當中裝載xml文件,并生成Animation對象。因為Animation是AnimationSet的子類,所以向上轉型,用Animation對象接收。
Animation animation = AnimationUtils.loadAnimation( Animation1Activity.this, R.anim.alpha);// 啟動動畫image.startAnimation(animation);
二、具體實現
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是起始透明度和結束時透明度 --> <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:開始的角度 toDegrees:結束的角度,+表示是正的 pivotX:用于設置旋轉時的x軸坐標 例 1)當值為"50",表示使用絕對位置定位 2)當值為"50%",表示使用相對于控件本身定位 3)當值為"50%p",表示使用相對于控件的父控件定位 pivotY:用于設置旋轉時的y軸坐標 --> <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軸坐標 止x軸坐標 始y軸坐標 止y軸坐標 軸的坐標 軸的坐標 --> <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軸坐標 止x軸坐標 始y軸坐標 止y軸坐標 --> <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裝載動畫配置文件 Animation animation = AnimationUtils.loadAnimation( Animation1Activity.this, R.anim.alpha); // 啟動動畫 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的子類;
2.一個AnimationSet包含了一系列的Animation;
3.針對AnimationSet設置一些Animation的常見屬性(如startOffset,duration等),可以被包含在AnimationSet當中的Animation集成;
例:一個AnimationSet中有兩個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是起始透明度和結束時透明度 --> <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裝載動畫配置文件 Animation animation = AnimationUtils.loadAnimation( Animation2Activity.this, R.anim. doubleani); // 啟動動畫 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定義了動畫變化的速率,在Animations框架當中定義了以下幾種Interpolator
? AccelerateDecelerateInterpolator:在動畫開始與結束的地方速率改變比較慢,在中間的時候速率快。
? AccelerateInterpolator:在動畫開始的地方速率改變比較慢,然后開始加速
? CycleInterpolator:動畫循環播放特定的次數,速率改變沿著正弦曲線
? DecelerateInterpolator:在動畫開始的地方速率改變比較慢,然后開始減速
? LinearInterpolator:動畫以均勻的速率改變
分為以下幾種情況:
1、在set標簽中
<set xmlns:android="http://schemas.android.com/apk/res/android"android:interpolator="@android:anim/accelerate_interpolator"/>
2、如果在一個set標簽中包含多個動畫效果,如果想讓這些動畫效果共享一個Interpolator。
<alpha android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromAlpha="1.0" android:toAlpha="0.0" android:startOffset="500" android:duration="500"/>
4、如果是在代碼上設置共享一個interpolator,則可以在AnimationSet設置interpolator。
AnimationSet animationSet = newAnimationSet(true);animationSet.setInterpolator(new AccelerateInterpolator());
5、如果不設置共享一個interpolator則可以在每一個Animation對象上面設置interpolator。
AnimationSet animationSet = newAnimationSet(false);alphaAnimation.setInterpolator(new AccelerateInterpolator());rotateAnimation.setInterpolator(new DecelerateInterpolator());
Frame-By-Frame Animations的使用方法
Frame-By-Frame Animations是一幀一幀的格式顯示動畫效果。類似于電影膠片拍攝的手法。
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="運動"/> </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可以用于實現使多個控件按順序一個一個的顯示。
1)LayoutAnimationsController用于為一個layout里面的控件,或者是一個ViewGroup里面的控件設置統一的動畫效果。
2)每一個控件都有相同的動畫效果。
3)控件的動畫效果可以在不同的時間顯示出來。
4)LayoutAnimationsController可以在xml文件當中設置,以可以在代碼當中進行設置。
2、在xml當中使用LayoutAnimationController
1)在res/anim文件夾下創建一個名為list_anim_layout.xml文件:
android:delay - 動畫間隔時間;子類動畫時間間隔 (延遲) 70% 也可以是一個浮點數 如“1.2”等
android:animationOrder - 動畫執行的循序(normal:順序,random:隨機,reverse:反向顯示)
国产亚洲精品久久久久久牛牛| 国产成人免费av| 精品久久久久久亚洲国产300| 欧美日韩电影在线观看| 国产激情久久久久| 美女黄色丝袜一区| 欧美精品videos| 国产精品劲爆视频| 久热精品视频在线观看一区| 久久久综合免费视频| 精品国产1区2区| 欧美激情视频网| 久久韩国免费视频| 亚洲视频在线观看免费| 久久中国妇女中文字幕| 成人精品在线视频| 久久精品青青大伊人av| 欧美高清第一页| 国产精品激情自拍| 久久69精品久久久久久久电影好| 亚洲精品视频网上网址在线观看| 亚洲精品aⅴ中文字幕乱码| 欧美日韩国产精品一区二区三区四区| 久久艹在线视频| 国产成人免费91av在线| 欧美黄色小视频| 精品一区精品二区| 97色在线观看| 成人网在线视频| 欧美激情精品久久久久久黑人| 亚洲美女精品久久| 欧美电影免费观看高清完整| 亚洲福利视频二区| 成人国产精品久久久| 欧美日韩精品在线播放| 国产精品私拍pans大尺度在线| 国产亚洲精品久久久久久牛牛| 亚洲第一男人av| 久久影院资源网| 精品视频www| 欧美性猛交xxxx久久久| 久久影视电视剧免费网站清宫辞电视| 久久久国产精彩视频美女艺术照福利| 色偷偷综合社区| 亚洲视频精品在线| 国产精品日韩久久久久| 最新国产精品亚洲| 亚洲精品黄网在线观看| 欧美激情免费观看| 色偷偷888欧美精品久久久| 狠狠久久五月精品中文字幕| 亚洲最大成人网色| 揄拍成人国产精品视频| 粗暴蹂躏中文一区二区三区| 国产成一区二区| 97国产精品视频| 亚洲第一av网站| 欧美另类在线观看| 亚洲欧美激情另类校园| 亚洲日本aⅴ片在线观看香蕉| 日韩视频免费看| 久久久久久久久91| 日韩美女视频在线观看| 亚洲专区中文字幕| 国产精品视频区| 国产自摸综合网| 久久精品在线播放| 亚洲精品一区二三区不卡| 久久久久久久激情视频| 日韩av一区二区在线| 91系列在线播放| 国产精品情侣自拍| 一区二区三区无码高清视频| 日韩精品在线影院| 精品国偷自产在线视频99| 久久99热精品| 日韩在线视频网站| 国产精品成人av在线| 91成人在线视频| 欧美极品少妇xxxxⅹ裸体艺术| 国产精品毛片a∨一区二区三区|国| 欧美激情一级欧美精品| 国产中文字幕91| 亚洲国产美女精品久久久久∴| 久久久久久999| 久久久久成人网| 国产亚洲精品久久久| 日韩成人性视频| 久久亚洲综合国产精品99麻豆精品福利| 亚洲999一在线观看www| 欧美高清理论片| 久久综合九色九九| 97免费中文视频在线观看| 亚洲精品国产精品久久清纯直播| 26uuu亚洲伊人春色| 亚洲视频999| 日韩视频在线免费| 麻豆国产精品va在线观看不卡| 欧美黄色小视频| 九九热这里只有精品6| 日韩美女视频免费看| 亚洲久久久久久久久久久| 亚洲免费小视频| 在线播放国产一区中文字幕剧情欧美| 国产成人精品综合久久久| 最近2019免费中文字幕视频三| 国产不卡在线观看| 亚洲欧美日韩综合| 精品国产31久久久久久| 欧美精品福利视频| 国产精品极品美女粉嫩高清在线| 宅男66日本亚洲欧美视频| 中文字幕精品一区久久久久| 日韩在线视频观看正片免费网站| 欧美国产日韩二区| 国产精品中文字幕在线| 亚洲精品有码在线| 一夜七次郎国产精品亚洲| 97超视频免费观看| 国产美女精彩久久| 77777少妇光屁股久久一区| 久久五月天色综合| 热久久99这里有精品| 亚洲欧美另类人妖| 国产噜噜噜噜久久久久久久久| 国产日韩欧美在线看| 亚洲成色777777在线观看影院| 亚洲人成在线观看| 亚洲欧美国产va在线影院| 国内精品久久久久| 欧美最近摘花xxxx摘花| 日韩国产精品一区| 亚洲精品久久久久久下一站| 精品福利一区二区| 18久久久久久| 亚洲人成网站免费播放| 4388成人网| 96精品久久久久中文字幕| 日韩精品极品毛片系列视频| 久久综合久中文字幕青草| 久久五月天综合| 精品动漫一区二区三区| 91在线无精精品一区二区| 国产亚洲精品综合一区91| 欧美性精品220| 亚洲精品美女视频| 欧美日韩国产va另类| 色999日韩欧美国产| 欧美激情精品久久久久久大尺度| 欧美大片在线免费观看| 懂色aⅴ精品一区二区三区蜜月| 人体精品一二三区| 在线观看精品国产视频| 国产激情久久久久| 午夜精品一区二区三区av| 午夜精品久久久久久久99黑人| 毛片精品免费在线观看| 91精品国产亚洲| 亚洲已满18点击进入在线看片| 久久国产精品亚洲| 亚洲女同性videos| 亚洲欧洲在线视频| 日韩av中文字幕在线| 精品久久久香蕉免费精品视频|