Android動畫大體分為兩大類:
視圖動畫(View Animation):Tween Animations、Frame Animations(Drawable Animation)
屬性動畫:Property Animations
Android 的動畫細(xì)分主要有三種:
補間動畫:Tween Animations (),只能給View使用
幀動畫:Frame Animations
屬性動畫:Property Animations,Android 3.0(API 11)系統(tǒng)才有效!這種動畫可以設(shè)置給任何Object,包括那些還沒有渲染到屏幕上的對象。這種動畫是可擴展的,可以讓你自定義任何類型和屬性的動畫。
補間動畫 Tween Animation
| java類名 | xml關(guān)鍵字 | 描述信息 |
|---|---|---|
| AlphaAnimation | <alpha>放置在res/anim/xx.xml | 漸變透明度動畫效果 |
| RotateAnimation | <rotate>放置在res/anim/xx.xml | 畫面轉(zhuǎn)移旋轉(zhuǎn)動畫效果 |
| ScaleAnimation | <scale>放置在res/anim/xx.xml | 漸變尺寸伸縮動畫效果 |
| TranslateAnimation | <translate>放置在res/anim/xx.xml | 畫面旋轉(zhuǎn)位置移動動畫效果 |
| AnimationSet | <set>放置在res/anim/xx.xml | 一個持有其他動畫元素alpha、scale、translate、rotate或者其他set元素的容器 |
1.主要的類:
父類:Animation 是一個抽象類
子類:AnimationSet、AlphaAnimation、TranslateAnimation、ScaleAnimation、RotateAnimation
2.主要實現(xiàn)的效果:
Alpha:淡入淡出
Scale:縮放
Rotate:旋轉(zhuǎn)
Translate:移動
3.實現(xiàn)方式:
| 實現(xiàn)方式 | 優(yōu)點 | 缺點 |
|---|---|---|
| 代碼 | 方便調(diào)試、運行 | 代碼的可重用性差、代碼量大 |
| xml | 可維護(hù)性高 | 不方便調(diào)試 |
4.代碼實現(xiàn)步驟:
第一步:創(chuàng)建一個AnimationSet對象AS,控件ImageView對象IV;
第二步:創(chuàng)建一個動畫對象eg:AlphaAnimation,AA;
第三步:設(shè)置動畫相應(yīng)地數(shù)據(jù)和值A(chǔ)A.set.....;
第四步:AS 裝載AA,AS.addAnimation(AA)
第四步:IV.startAnimation(AS)控件執(zhí)行動畫;
Tween有兩種實現(xiàn)方式:代碼實現(xiàn)、XML實現(xiàn)
//代碼實現(xiàn),稍后添加代碼
5.xml實現(xiàn)Tween動畫:
步驟:
在res文件夾下建立一個anim文件件
創(chuàng)建xml文件,并首先添加一個set標(biāo)簽
設(shè)置相應(yīng)地屬性的標(biāo)簽<alpha>
代碼中使用AnimationUtils.loadAnimation(activity,R,anim.alpha)裝載xml文件,生成Animation對象Am
控件使用startAnimation(Am)
6.xml具體的實現(xiàn)
//代碼稍后貼上
補間動畫的一個重要類 Interpolator 插值器
http://www.cnblogs.com/ldq2016/p/5407061.html
1.插值器
| java類 | xml id值 | 描述 |
|---|---|---|
| AccelerateDecelerateInterpolator | @android:anim/accelerate_decelerate_interpolator | 動畫始末速率較慢,中間加速 |
| AccelerateInterpolator | @android:anim/accelerate_interpolator | 動畫開始速率較慢,之后慢慢加速 |
| AnticipateInterpolator | @android:anim/anticipate_interpolator | 開始的時候從后向前甩 |
| AnticipateOvershootInterpolator | @android:anim/anticipate_overshoot_interpolator | 類似上面AnticipateInterpolator |
| BounceInterpolator | @android:anim/bounce_interpolator | 動畫結(jié)束時彈起 |
| CycleInterpolator | @android:anim/cycle_interpolator | 循環(huán)播放速率改變?yōu)檎仪€ |
| DecelerateInterpolator | @android:anim/decelerate_interpolator | 動畫開始快然后慢 |
| LinearInterpolator | @android:anim/linear_interpolator | 動畫勻速改變 |
| OvershootInterpolator | @android:anim/overshoot_interpolator | 向前彈出一定值之后回到原來位置 |
| PathInterpolator | 新增,定義路徑坐標(biāo)后按照路徑坐標(biāo)來跑。 |
都是實現(xiàn)了Interpotator接口,系統(tǒng)有很多已經(jīng)實現(xiàn)的插值器,具體如下:
| java類 | xml id值 | 描述 |
|---|---|---|
| AccelerateDecelerateInterpolator | @android:anim/accelerate_decelerate_interpolator | 動畫始末速率較慢,中間加速 |
| AccelerateInterpolator | @android:anim/accelerate_interpolator | 動畫開始速率較慢,之后慢慢加速 |
| AnticipateInterpolator | @android:anim/anticipate_interpolator | 開始的時候從后向前甩 |
| AnticipateOvershootInterpolator | @android:anim/anticipate_overshoot_interpolator | 類似上面AnticipateInterpolator |
| BounceInterpolator | @android:anim/bounce_interpolator | 動畫結(jié)束時彈起 |
| CycleInterpolator | @android:anim/cycle_interpolator | 循環(huán)播放速率改變?yōu)檎仪€ |
| DecelerateInterpolator | @android:anim/decelerate_interpolator | 動畫開始快然后慢 |
| LinearInterpolator | @android:anim/linear_interpolator | 動畫勻速改變 |
| OvershootInterpolator | @android:anim/overshoot_interpolator | 向前彈出一定值之后回到原來位置 |
| PathInterpolator | 新增,定義路徑坐標(biāo)后按照路徑坐標(biāo)來跑。 |
2.如何使用插值器
<set android:interpolator="@android:anim/accelerate_interpolator">
...
</set>
3.自定義插值器
插值器的自定義,兩種方式:xml方式和java代碼方式
xml自定義步驟:
1.在res/anim/filename.xml文件,
2.修改自定義插值器的值,
3.在補間動畫文件中引用該文件。
<?xml version="1.0" encoding="utf-8"?>
<InterpolatorName xmlns:android="http://schemas.android.com/apk/res/android"
android:attribute_name="value"
/>
l