補間動畫(Tween Animation)
java
a. 漸變更畫支持四種類型:平移(Translate)、旋轉(Rotate)、縮放(Scale)、不透明度(Alpha)。android
b. 只是顯示的位置變更,View的實際位置未改變,表現爲View移動到其餘地方,點擊事件仍在原處才能響應。異步
c. 組合使用步驟較複雜。動畫
d. View Animation 也是指此動畫。this
幀動畫(Frame Animation)
lua
a. 用於生成連續的Gif效果圖。spa
b. DrawableAnimation也是指此動畫。.net
屬性動畫(Property Animation)
設計
a. 支持對全部View能更新的屬性的動畫(須要屬性的setXxx()和getXxx())。code
b. 更改的是View實際的屬性,因此不會影響其在動畫執行後所在位置的正常使用。
逐幀動畫 frame_anim.xml
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" > <!-- android:oneshot="false" 表示不循環播放 --> <item android:drawable="@drawable/ni" android:duration="500"/> <item android:drawable="@drawable/hao" android:duration="500"/> <item android:drawable="@drawable/huan" android:duration="500"/> <item android:drawable="@drawable/yin" android:duration="500"/> <item android:drawable="@drawable/lai" android:duration="500"/> <item android:drawable="@drawable/dao" android:duration="500"/> <item android:drawable="@drawable/wo" android:duration="500"/> <item android:drawable="@drawable/de" android:duration="500"/> <item android:drawable="@drawable/bo" android:duration="500"/> <item android:drawable="@drawable/ke" android:duration="500"/> </animation-list>
java中代碼
//anim的backgroud="@anim/frame_anim" final ImageView anim = (ImageView) findViewById(R.id.anim); final AnimationDrawable animation = (AnimationDrawable) anim .getBackground(); animation.start(); animation.stop();
http://download.csdn.net/download/qq_26972449/9373567參考例子
補間動畫 透明
<alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="0.1" android:toAlpha="1.0" android:duration="1000" /> fromAlpha 屬性爲動畫起始時透明度 toAlpha 屬性爲動畫結束時透明度 0.0 表示徹底透明 1.0 表示徹底不透明 duration 屬性爲動畫持續時間 時間以毫秒爲單位
java代碼
AnimationalphaAnimation=new AlphaAnimation(1, (float) 0.1); alphaAnimation.setDuration(3000);//設置動畫持續時間爲3秒 alphaAnimation.setFillAfter(true);//設置動畫結束後保持當前的位置(即不返回到動畫開始前的位置) imgShow.startAnimation(alphaAnimation); AnimationalphaAnimation2=AnimationUtils.loadAnimation(this, R.anim.alpha);//加載Xml文件中的動畫 imgShow.startAnimation(alphaAnimation2);
旋轉
<rotate android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromDegrees="0" android:toDegrees="+350" android:pivotX="50%" android:pivotY="50%" android:duration="3000" /> <!-- rotate 旋轉動畫效果屬性: interpolator 指定一個動畫的插入器 accelerate_decelerate_interpolator 加速-減速動畫插入器 accelerate_interpolator 加速-動畫插入器 decelerate_interpolator 減速-動畫插入器 fromDegrees 屬性爲動畫起始時物件的角度 toDegrees 屬性爲動畫結束時物件旋轉的角度 能夠大於360度 說明: 當角度爲負數——表示逆時針旋轉 當角度爲正數——表示順時針旋轉 (負數from——to正數:順時針旋轉) (負數from——to負數:逆時針旋轉) (正數from——to正數:順時針旋轉) (正數from——to負數:逆時針旋轉) pivotX 屬性爲動畫相對於物件的X座標的開始位置 pivotY 屬性爲動畫相對於物件的Y座標的開始位置 說明: 以上兩個屬性值從0%-100%中取值50%爲物件的X或Y方向座標上的中點位置 長整型值:duration 屬性爲動畫持續時間 時間以毫秒爲單位-->
java代碼
AnimationrotateAnimation=new RotateAnimation(0, 45); rotateAnimation.setDuration(3000);//設置動畫持續時間爲3秒 rotateAnimation.setFillAfter(true);//設置動畫結束後保持當前的位置(即不返回到動畫開始前的位置) imgShow.startAnimation(rotateAnimation); myAnimation_Rotate=new RotateAnimation(0.0f, +350.0f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
縮放
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" > <!-- 尺寸伸縮動畫效果scale 屬性:interpolator 指定一個動畫的插入器 accelerate_decelerate_interpolator 加速-減速 動畫插入器 accelerate_interpolator 加速- 動畫插入器 decelerate_interpolator 減速- 動畫插入器 其餘的屬於特定的動畫效果 浮點型值:fromXScale 屬性爲動畫起始時X座標上的伸縮尺寸 toXScale 屬性爲動畫結束時X座標上的伸縮尺寸 fromYScale 屬性爲動畫起始時Y座標上的伸縮尺寸 toYScale 屬性爲動畫結束時Y座標上的伸縮尺寸 startOffset 屬性爲從上次動畫停多少時間開始執行下個動畫 說明: 以上四種屬性值 0.0表示收縮到沒有 1.0表示正常無伸縮 值小於1.0表示收縮 值大於1.0表示放大 pivotX 屬性爲動畫相對於物件的X座標的開始位置 pivotY 屬性爲動畫相對於物件的Y座標的開始位置 說明: 以上兩個屬性值從0%-100%中取值50%爲物件的X或Y方向座標上的中點位置 長整型值:duration 屬性爲動畫持續時間 時間以毫秒爲單位 布爾型值: fillAfter 屬性當設置爲true ,該動畫轉化在動畫結束後被應用 --> <scale android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromXScale="1" android:toXScale="1" android:fromYScale="0" android:toYScale="1" android:pivotX="0" android:pivotY="1" android:startOffset="1" android:duration="700" /> </set>
java 代碼
Animationscale Animation=new ScaleAnimation(0.5f, 1.0f,1.0f, 1.0f); scaleAnimation.setDuration(2000);//設置動畫持續時間爲3秒 scaleAnimation.setFillAfter(true);//設置動畫結束後保持當前的位置(即不返回到動畫開始前的位置) scaleAnimation.setRepeatCount(3); imgShow.startAnimation(scaleAnimation); myAnimation_Scale =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
移動
<?xml version="1.0" encoding="utf-8"?> <!-- translate 整型值: fromXDelta 屬性爲動畫起始時X座標上的位置 toXDelta 屬性爲動畫結束時X座標上的位置 fromYDelta 屬性爲動畫起始時Y座標上的位置 toYDelta 屬性爲動畫結束時Y座標上的位置 注意: 沒有指定fromXType toXType from YType toYType 時候,默認是以本身爲相對參照物長整型值: duration 屬性爲動畫持續時間 說明:時間以毫秒爲單位--> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:fromXDelta="0" android:toXDelta="10" android:duration="1000" android:interpolator="@anim/cycle_4" />
Java 代碼
AnimationtranslateAnimation=new TranslateAnimation(0, 100, 0, 0); translateAnimation.setDuration(3000);//設置動畫持續時間爲3秒 translateAnimation.setInterpolator(this, android.R.anim.cycle_interpolator);//設置動畫插入器 translateAnimation.setFillAfter(true);//設置動畫結束後保持當前的位置(即不返回到動畫開始前的位置) imgShow.startAnimation(translateAnimation);
多個動畫組合
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);
屬性動畫
相關的類
ObjectAnimator 動畫的執行類,後面詳細介紹
ValueAnimator 動畫的執行類,後面詳細介紹
AnimatorSet 用於控制一組動畫的執行:線性,一塊兒,每一個動畫的前後執行等。
AnimatorInflater 用戶加載屬性動畫的xml文件
TypeEvaluator 類型估值,主要用於設置動畫操做屬性的值。
總的來講,屬性動畫就是,動畫的執行類來設置動畫操做的對象的屬性、持續時間,開始和結束的屬性值,時間差值等,而後系統會根據設置的參數動態的變化對象的屬性。
ObjectAnimator// .ofFloat(view, "rotationX", 0.0F, 360.0F)// x軸旋轉 .setDuration(500)// .start(); //rotationY Y軸旋轉 //rotation 旋轉 //translationX X軸移動 //translationY Y軸移動 //scaleX X軸縮放 //scaleY Y軸縮放 ObjectAnimator animator1 = ObjectAnimator.ofFloat(textView, "scaleX", 0F, 1F,0F,2F); //alpha 透明度 ObjectAnimator animator1 = ObjectAnimator.ofFloat(textView, "alpha", 0F, 1F,0F);
多個動畫
//同步動畫設計 PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("translationX", 0, 360F); PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("translationY", 0, 360F); PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("rotation", 0, 360F); ObjectAnimator.ofPropertyValuesHolder(imageView, p1, p2 ,p3).setDuration(1000).start(); //經過AnimatiorSet來設計同步執行的多個屬性動畫 ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView, "translationX", 0F, 360F);//X軸平移旋轉 ObjectAnimator animator2 = ObjectAnimator.ofFloat(imageView, "translationY", 0F, 360F);//Y軸平移旋轉 ObjectAnimator animator3 = ObjectAnimator.ofFloat(imageView, "rotation", 0F, 360F);//360度旋轉 AnimatorSet set = new AnimatorSet(); set.playSequentially(animator1, animator2, animator3);//分步執行 set.playTogether(animator1, animator2, animator3);//同步執行 //屬性動畫的執行順序控制 set.play(animator3).with(animator1); //同步 set.play(animator2).after(animator3); //異步 set.setDuration(1000); set.start();
屬性動畫更多高級用法