零零碎碎的東西老是記不長久,僅僅學習別人的文章也只是他人咀嚼後留下的殘渣。無心中發現了這個每日一道面試題,想了想若是隻是簡單地去思考,那麼不只會收效甚微,甚至難一點的題目本身可能都懶得去想,堅持不下來。因此不如把每一次的思考、理解以及別人的看法記錄下來。不只加深本身的理解,更要激勵本身堅持下去。java
補間動畫是根據咱們設置好的view起始形態與終止形態,經過插值器與duration(時長)自動計算出中間view的平滑變化而實現動畫效果。android
補間動畫有TranslateAnimation(平移)、ScaleAnimation(縮放)、RotateAnimation(旋轉)、AlphaAnimatio(漸變)四個類,這些只是很基本的四種動畫效果,若是咱們須要在此基礎上更爲複雜的動畫效果,能夠繼承這四個類進行裝飾。git
通常狀況下咱們都是在xml文件中定義補間動畫,屬性設置大體有起始值、終止值、時長(duration)、插值(interpolator,即動畫的變化速度,加速、減速、勻速、拋物線速度)四個屬性,四種動畫效果的標籤---translate、scale、rotate、alpha。具體屬性設置及意義以下:github
<!--平移-->
<translate
<!--水平方向、豎直方向初始的view位置-->
android:fromXDelta="0%"
android:fromYDelta="0%"
<!--水平方向、豎直方向終止點的view位置-->
android:toXDelta="100%"
android:toYDelta="100%"
<!--動畫持續時間-->
android:duration="@integer/animation_duration"/>
<!--縮放-->
<scale
<!--水平方向、豎直方向初始的view縮放大小-->
android:fromXScale="0.5"
android:fromYScale="0.5"
<!--水平方向、豎直方向終止點的view縮放大小-->
android:toXScale="1.0"
android:toYScale="1.0"
<!--指定縮放中心點的座標-->
android:pivotX="50%"
android:pivotY="50%"
<!--動畫持續時間-->
android:duration="@integer/animation_duration"/>
<!--旋轉-->
<rotate
<!--起始旋轉角度-->
android:fromDegrees="0"
<!--終止旋轉角度-->
android:toDegrees="1800"
<!--旋轉中心點-->
android:pivotY="50%"
android:pivotX="50%"
<!--動畫持續時間-->
android:duration="@integer/animation_duration"/>
<!--漸變-->
<alpha
<!--起始透明度-->
android:fromAlpha="1"
<!--終止透明度-->
android:toAlpha="0.5"
<!--動畫持續時間-->
android:duration="@integer/animation_duration"/>
複製代碼
java代碼設置面試
Animation animation = AnimationUtils.loadAnimation(this, R.anim.activity_open_enter);
animation.start();
imageView.setAnimation(animation);
複製代碼
屬性動畫更爲強大。補間動畫只能自定義兩個關鍵幀在旋轉、位移、縮放、透明度四個方面的變化,而屬性動畫能夠定義任何屬性的變化。另外,補間動畫只能對UI組件執行動畫,但屬性動畫幾乎能夠對任何對象執行動畫(無論它是否在屏幕上顯示)。數組
屬性動畫有三個關鍵的類---ValueAnimation、ObjectAnimation、AnimationSetide
屬性動畫主要的時間引擎,負責計算各個幀的屬性值。它定義了屬性動畫絕大部分核心功能,包括計算各幀的屬性值,負責處理更新事件、按屬性類型值控制計算規則等。佈局
ValueAnimation建立動畫步驟:學習
ValueAnimator valueAnimator = ValueAnimator.ofInt(0,100);
valueAnimator.setDuration(1000 * 3);
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
//得到變化的屬性值
int time = (int) animation.getAnimatedValue();
...//對指定對象進行屬性值更新操做
}
});
複製代碼
通常來講,ValueAnimation、自己並不顯示任何動畫,它更像是一個數值計算器。經過ofXxx方法指定計算類型,經過steXxx方法設置計算中值的變化方式,產生一段有規律的數字,讓開發者本身爲指定對象動態的設置屬性。動畫
ObjectAnim類繼承了ValueAnimation類,能夠直接將ValueAnimation計算出來的值應用到指定對象的指定屬性上。
ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "translationX", 0,500);
animator.setDuration(2000);
animator.start();
複製代碼
ofFloat方法與ValueAnimation中不一樣的是第一個參數是執行動畫的對象,第二個參數是須要操做的屬性,後面是一個可變長數組,表示屬性值的起始於終止值。
第二個參數須要操做的屬性在第一個參數對象中須要有get和set方法。ObjectAnimation內部是經過反射機制得到set方法動態的設置對象屬性。能夠直接使用的的屬性有:
那麼多炫酷的動畫,固然不可能只是靠單一一個ObjectAnimation來實現的,這就須要多個ObjectAnimation進行組合,就須要AnimationSet類了。
AnimatorSet animatorSet = new AnimatorSet();
ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView, "translationX", 0,500);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(imageView, "translationX", 0,500);
ObjectAnimator animator3 = ObjectAnimator.ofFloat(imageView, "translationX", 0,500);
ObjectAnimator animator4 = ObjectAnimator.ofFloat(imageView, "translationX", 0,500);
animatorSet.play(animator1).after(animator2).with(animator3).before(animator4);
animatorSet.setDuration(2000);
animatorSet.start();
複製代碼
AnimationSet的play方法表示播放當前動畫,其他還有組合方法: