實現(xiàn)翻轉(zhuǎn)卡片的動畫效果
在Android設(shè)計中, 經(jīng)常會使用卡片元素, 正面顯示圖片或主要信息, 背面顯示詳細內(nèi)容, 如網(wǎng)易有道詞典的單詞翻轉(zhuǎn)和海底撈的食譜展示. 實現(xiàn)卡片視圖非常容易, 那么如何實現(xiàn)翻轉(zhuǎn)動畫呢?
在TB吃海底撈時, 使用Pad點餐, 發(fā)現(xiàn)應(yīng)用的食譜功能使用卡片控件, 就準(zhǔn)備和大家分享一下實現(xiàn)方式. 有興趣的朋友可以去海底撈看看:)
本文源碼的GitHub下載地址
https://github.com/SpikeKing/wcl-flip-anim-demo
歡迎Follow我的GitHub: https://github.com/SpikeKing
首頁
首頁由正面和背面兩張卡片組成, 同時, 設(shè)置點擊事件flipCard.
- <?xml version="1.0" encoding="utf-8"?>
- <FrameLayout
- android:id="@+id/main_fl_container"
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:onClick="flipCard"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context="me.chunyu.spike.wcl_flip_anim_demo.MainActivity">
- <include
- layout="@layout/cell_card_back"/>
- <include
- layout="@layout/cell_card_front"/>
- </FrameLayout>
邏輯, 初始化動畫和鏡頭距離.
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- ButterKnife.bind(this);
- setAnimators(); // 設(shè)置動畫
- setCameraDistance(); // 設(shè)置鏡頭距離
- }
動畫
初始化右出(RightOut)和左入(LeftIn)動畫, 使用動畫集合AnimatorSet.
當(dāng)右出動畫開始時, 點擊事件無效, 當(dāng)左入動畫結(jié)束時, 點擊事件恢復(fù).
- // 設(shè)置動畫
- private void setAnimators() {
- mRightOutSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.anim_out);
- mLeftInSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.anim_in);
- // 設(shè)置點擊事件
- mRightOutSet.addListener(new AnimatorListenerAdapter() {
- @Override public void onAnimationStart(Animator animation) {
- super.onAnimationStart(animation);
- mFlContainer.setClickable(false);
- }
- });
- mLeftInSet.addListener(new AnimatorListenerAdapter() {
- @Override public void onAnimationEnd(Animator animation) {
- super.onAnimationEnd(animation);
- mFlContainer.setClickable(true);
- }
- });
- }
右出動畫
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <!--旋轉(zhuǎn)-->
- <objectAnimator
- android:duration="@integer/anim_length"
- android:propertyName="rotationY"
- android:valueFrom="0"
- android:valueTo="180"/>
- <!--消失-->
- <objectAnimator
- android:duration="0"
- android:propertyName="alpha"
- android:startOffset="@integer/anim_half_length"
- android:valueFrom="1.0"
- android:valueTo="0.0"/>
- </set>
旋轉(zhuǎn)180°, 當(dāng)旋轉(zhuǎn)一半時, 卡片消失.
左入動畫
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android">
- <!--消失-->
- <objectAnimator
- android:duration="0"
- android:propertyName="alpha"
- android:valueFrom="1.0"
- android:valueTo="0.0"/>
- <!--旋轉(zhuǎn)-->
- <objectAnimator
- android:duration="@integer/anim_length"
- android:propertyName="rotationY"
- android:valueFrom="-180"
- android:valueTo="0"/>
- <!--出現(xiàn)-->
- <objectAnimator
- android:duration="0"
- android:propertyName="alpha"
- android:startOffset="@integer/anim_half_length"
- android:valueFrom="0.0"
- android:valueTo="1.0"/>
- </set>
在開始時是隱藏, 逆向旋轉(zhuǎn), 當(dāng)旋轉(zhuǎn)一半時, 顯示卡片.
鏡頭視角
改變視角, 涉及到旋轉(zhuǎn)卡片的Y軸, 即rotationY, 需要修改視角距離.
如果不修改, 則會超出屏幕高度, 影響視覺體驗.
- // 改變視角距離, 貼近屏幕
- private void setCameraDistance() {
- int distance = 16000;
- float scale = getResources().getDisplayMetrics().density * distance;
- mFlCardFront.setCameraDistance(scale);
- mFlCardBack.setCameraDistance(scale);
- }
旋轉(zhuǎn)控制
設(shè)置右出和左入動畫的目標(biāo)控件, 兩個動畫同步進行, 并區(qū)分正反面朝上.
- // 翻轉(zhuǎn)卡片
- public void flipCard(View view) {
- // 正面朝上
- if (!mIsShowBack) {
- mRightOutSet.setTarget(mFlCardFront);
- mLeftInSet.setTarget(mFlCardBack);
- mRightOutSet.start();
- mLeftInSet.start();
- mIsShowBack = true;
- } else { // 背面朝上
- mRightOutSet.setTarget(mFlCardBack);
- mLeftInSet.setTarget(mFlCardFront);
- mRightOutSet.start();
- mLeftInSet.start();
- mIsShowBack = false;
- }
- }
動畫效果
動畫效果非常簡單, 全部邏輯都不足50行, 這么好玩的動畫, 用起來吧.
OK, that’s all! Enjoy it!