這是項目總結(jié)第三篇,前兩篇分別為:
1. Android 項目總結(jié)(1)- 之弧形ViewPager 和弧形HeaderView
2 . Android項目總結(jié)(二)時間、數(shù)字選擇器和省市區(qū)三級聯(lián)動
今天為大家分享一個簡單的登錄背景動畫,圖片循環(huán)播放動畫,具體效果是啥樣子的呢?先上一張效果圖:
一、需求
我們開發(fā)APP的時候,一般都有一個注冊登錄的入口頁面,這個頁面的呈現(xiàn)有很多種方式,如:
- 靜態(tài)背景圖 + 注冊登錄按鈕
- 視頻背景 + 注冊登錄按鈕
- 背景動畫 + 注冊登錄按鈕
今天分享的就是第三種 ,背景動畫,效果圖如上所示,接下來就分析一下這個動畫:
1 . 有 N 張圖片切換(項目中用的4張)
2 . 圖片切換過渡:當前圖片放大并且淡出,下一張顯示的圖片淡入。
3 . 圖片循環(huán)播放,顯示到最后一張時又從第一張開始。
二、實現(xiàn)
上面對照效果圖分析了動畫的幾個點,那么接下來就看怎么實現(xiàn),我們選擇用屬性動畫來實現(xiàn),具體實現(xiàn)思路如下:
本例中有4張圖片:A,B,C,D
有4組動畫:
A->B
B->C
C->D
D->A
這樣4組就實現(xiàn)了循環(huán)切換
然后就是每一組動畫的實現(xiàn),其實很簡單,一個Scale 放大效果+ 一個 alpha 效果:
A -> B:
ObjectAnimator animator1 = ObjectAnimator.ofFloat(mBgView1, "alpha", 1.0f, 0f);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(mBgView2, "alpha", 0f, 1.0f);
ObjectAnimator animatorScale1 = ObjectAnimator.ofFloat(mBgView1, "scaleX", 1.0f, 1.3f);
ObjectAnimator animatorScale2 = ObjectAnimator.ofFloat(mBgView1, "scaleY", 1.0f, 1.3f);
AnimatorSet animatorSet1 = new AnimatorSet();
animatorSet1.setDuration(5000);
animatorSet1.play(animator1).with(animator2).with(animatorScale1).with(animatorScale2);
B->C:
ObjectAnimator animator3 = ObjectAnimator.ofFloat(mBgView2, "alpha", 1.0f, 0f);
ObjectAnimator animator4 = ObjectAnimator.ofFloat(mBgView3, "alpha", 0f, 1.0f);
ObjectAnimator animatorScale3 = ObjectAnimator.ofFloat(mBgView2, "scaleX", 1.0f, 1.3f);
ObjectAnimator animatorScale4 = ObjectAnimator.ofFloat(mBgView2, "scaleY", 1.0f, 1.3f);
AnimatorSet animatorSet2 = new AnimatorSet();
animatorSet2.setDuration(5000);
animatorSet2.play(animator3).with(animator4).with(animatorScale3).with(animatorScale4);
C->D:
ObjectAnimator animator5 = ObjectAnimator.ofFloat(mBgView3, "alpha", 1.0f, 0f);
ObjectAnimator animator6 = ObjectAnimator.ofFloat(mBgView4, "alpha", 0f, 1.0f);
ObjectAnimator animatorScale5 = ObjectAnimator.ofFloat(mBgView3, "scaleX", 1.0f, 1.3f);
ObjectAnimator animatorScale6 = ObjectAnimator.ofFloat(mBgView3, "scaleY", 1.0f, 1.3f);
AnimatorSet animatorSet3 = new AnimatorSet();
animatorSet3.setDuration(5000);
animatorSet3.play(animator5).with(animator6).with(animatorScale5).with(animatorScale6);
D->A:
ObjectAnimator animator7 = ObjectAnimator.ofFloat(mBgView4, "alpha", 1.0f, 0f);
ObjectAnimator animator8 = ObjectAnimator.ofFloat(mBgView1, "alpha", 0f, 1.0f);
ObjectAnimator animatorScale7 = ObjectAnimator.ofFloat(mBgView4, "scaleX", 1.0f, 1.3f);
ObjectAnimator animatorScale8 = ObjectAnimator.ofFloat(mBgView4, "scaleY", 1.0f, 1.3f);
AnimatorSet animatorSet4 = new AnimatorSet();
animatorSet4.setDuration(5000);
animatorSet4.play(animator7).with(animator8).with(animatorScale7).with(animatorScale8);
上面的代碼展示了每一組動畫,將每組動畫中的幾個動畫放在一個AnimatorSet 中,設(shè)置為同時播放。最后我們需要將這4組動畫按照順序鏈接起來,怎么鏈接呢?用AnimatorSet
的playSequentially
方法。如下:
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playSequentially(animatorSet1, animatorSet2, animatorSet3, animatorSet4);
animatorSet.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
// 這個是實現(xiàn)循環(huán)播放的關(guān)鍵
animation.start();
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
animatorSet.start();
其中有一個關(guān)鍵點:在監(jiān)聽動畫結(jié)束的回調(diào)方法中,調(diào)用animation.start();
實現(xiàn)循環(huán)播放。
你以為到此這篇文章就結(jié)束了嗎? 當然還沒有,上面的代碼其實效果已經(jīng)出來了,但是還是有點問題?什么問題呢?就是當播放完第一次,后面循環(huán)播放的時候會有一個跳動。 為什么呢? 看看上面的代碼就會發(fā)現(xiàn),當執(zhí)播放完一輪后,4張圖片都放大了 1.3 倍數(shù)。
ObjectAnimator animatorScale1 = ObjectAnimator.ofFloat(mBgView1, "scaleX", 1.0f, 1.3f);
ObjectAnimator animatorScale2 = ObjectAnimator.ofFloat(mBgView1, "scaleY", 1.0f, 1.3f);
然后重復播放的時候,又會執(zhí)行scale 動畫,從 1.0 -> 1.3 ,因此實際上會先從 1.3 -> 1.0。再執(zhí)行縮放動畫,這就是跳動的原因,因此在播放完一輪后,我們要將放大的View 先復位到原大小,然后在執(zhí)行動畫。在onAnimationEnd
方法中復位。
最終代碼如下:
ObjectAnimator animator1 = ObjectAnimator.ofFloat(mBgView1, "alpha", 1.0f, 0f);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(mBgView2, "alpha", 0f, 1.0f);
ObjectAnimator animatorScale1 = ObjectAnimator.ofFloat(mBgView1, "scaleX", 1.0f, 1.3f);
ObjectAnimator animatorScale2 = ObjectAnimator.ofFloat(mBgView1, "scaleY", 1.0f, 1.3f);
AnimatorSet animatorSet1 = new AnimatorSet();
animatorSet1.setDuration(5000);
animatorSet1.play(animator1).with(animator2).with(animatorScale1).with(animatorScale2);
ObjectAnimator animator3 = ObjectAnimator.ofFloat(mBgView2, "alpha", 1.0f, 0f);
ObjectAnimator animator4 = ObjectAnimator.ofFloat(mBgView3, "alpha", 0f, 1.0f);
ObjectAnimator animatorScale3 = ObjectAnimator.ofFloat(mBgView2, "scaleX", 1.0f, 1.3f);
ObjectAnimator animatorScale4 = ObjectAnimator.ofFloat(mBgView2, "scaleY", 1.0f, 1.3f);
AnimatorSet animatorSet2 = new AnimatorSet();
animatorSet2.setDuration(5000);
animatorSet2.play(animator3).with(animator4).with(animatorScale3).with(animatorScale4);
ObjectAnimator animator5 = ObjectAnimator.ofFloat(mBgView3, "alpha", 1.0f, 0f);
ObjectAnimator animator6 = ObjectAnimator.ofFloat(mBgView4, "alpha", 0f, 1.0f);
ObjectAnimator animatorScale5 = ObjectAnimator.ofFloat(mBgView3, "scaleX", 1.0f, 1.3f);
ObjectAnimator animatorScale6 = ObjectAnimator.ofFloat(mBgView3, "scaleY", 1.0f, 1.3f);
AnimatorSet animatorSet3 = new AnimatorSet();
animatorSet3.setDuration(5000);
animatorSet3.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
// 放大的View復位
mBgView1.setScaleX(1.0f);
mBgView1.setScaleY(1.0f);
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
animatorSet3.play(animator5).with(animator6).with(animatorScale5).with(animatorScale6);
ObjectAnimator animator7 = ObjectAnimator.ofFloat(mBgView4, "alpha", 1.0f, 0f);
ObjectAnimator animator8 = ObjectAnimator.ofFloat(mBgView1, "alpha", 0f, 1.0f);
ObjectAnimator animatorScale7 = ObjectAnimator.ofFloat(mBgView4, "scaleX", 1.0f, 1.3f);
ObjectAnimator animatorScale8 = ObjectAnimator.ofFloat(mBgView4, "scaleY", 1.0f, 1.3f);
AnimatorSet animatorSet4 = new AnimatorSet();
animatorSet4.setDuration(5000);
animatorSet4.play(animator7).with(animator8).with(animatorScale7).with(animatorScale8);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playSequentially(animatorSet1, animatorSet2, animatorSet3, animatorSet4);
animatorSet.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
// 將放大的View 復位
mBgView2.setScaleX(1.0f);
mBgView2.setScaleY(1.0f);
mBgView3.setScaleX(1.0f);
mBgView3.setScaleY(1.0f);
mBgView4.setScaleX(1.0f);
mBgView4.setScaleY(1.0f);
// 循環(huán)播放
animation.start();
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
animatorSet.start();
xml代碼:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/login_bg_image4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/login_bg4" />
<ImageView
android:id="@+id/login_bg_image3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/login_bg3" />
<ImageView
android:id="@+id/login_bg_image2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/login_bg2" />
<ImageView
android:id="@+id/login_bg_image1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/login_bg1" />
</FrameLayout>
注意ImageView的順序,第一張圖應該在最上面。
注意: 四個View復位的地方不一樣,第一個是在第二組動畫執(zhí)行完畢后復位的,為什么沒有和其他幾個一起放到最后呢? 因為 D -> A 的時候就需要顯示A,這個時候這一輪是沒有播放完的,因此D->A 的時候會跳動。所以我們把他放到前面復位。
三、總結(jié)
很簡單的一個循環(huán)過渡動畫,本文是用屬性動畫實現(xiàn)的。當然肯定還有其他實現(xiàn)方式,如:放一個gif圖或者幀動畫也是可以的,但是這樣可能就需要切很多張圖,增加了我們apk 的體積。其他方法大家可以去探索一下,歡迎交流。