自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

實現(xiàn)翻轉(zhuǎn)卡片的動畫效果

移動開發(fā) Android
在Android設(shè)計中, 經(jīng)常會使用卡片元素, 正面顯示圖片或主要信息, 背面顯示詳細內(nèi)容, 如網(wǎng)易有道詞典的單詞翻轉(zhuǎn)和海底撈的食譜展示. 實現(xiàn)卡片視圖非常容易, 那么如何實現(xiàn)翻轉(zhuǎn)動畫呢?

在Android設(shè)計中, 經(jīng)常會使用卡片元素, 正面顯示圖片或主要信息, 背面顯示詳細內(nèi)容, 如網(wǎng)易有道詞典的單詞翻轉(zhuǎn)和海底撈的食譜展示. 實現(xiàn)卡片視圖非常容易, 那么如何實現(xiàn)翻轉(zhuǎn)動畫呢? 

 

 

[[182648]] 

在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.

  1. <?xml version="1.0" encoding="utf-8"?> 
  2.  
  3. <FrameLayout 
  4.  
  5.     android:id="@+id/main_fl_container" 
  6.  
  7.     xmlns:android="http://schemas.android.com/apk/res/android" 
  8.  
  9.     xmlns:tools="http://schemas.android.com/tools" 
  10.  
  11.     android:layout_width="match_parent" 
  12.  
  13.     android:layout_height="match_parent" 
  14.  
  15.     android:onClick="flipCard" 
  16.  
  17.     android:paddingBottom="@dimen/activity_vertical_margin" 
  18.  
  19.     android:paddingLeft="@dimen/activity_horizontal_margin" 
  20.  
  21.     android:paddingRight="@dimen/activity_horizontal_margin" 
  22.  
  23.     android:paddingTop="@dimen/activity_vertical_margin" 
  24.  
  25.     tools:context="me.chunyu.spike.wcl_flip_anim_demo.MainActivity"
  26.  
  27.   
  28.  
  29.     <include 
  30.  
  31.         layout="@layout/cell_card_back"/> 
  32.  
  33.   
  34.  
  35.     <include 
  36.  
  37.         layout="@layout/cell_card_front"/> 
  38.  
  39.   
  40.  </FrameLayout>  

邏輯, 初始化動畫和鏡頭距離.

  1. @Override 
  2.  
  3. protected void onCreate(Bundle savedInstanceState) { 
  4.  
  5.     super.onCreate(savedInstanceState); 
  6.  
  7.     setContentView(R.layout.activity_main); 
  8.  
  9.     ButterKnife.bind(this); 
  10.  
  11.   
  12.  
  13.     setAnimators(); // 設(shè)置動畫 
  14.  
  15.     setCameraDistance(); // 設(shè)置鏡頭距離 
  16.  
  17.  

動畫

初始化右出(RightOut)和左入(LeftIn)動畫, 使用動畫集合AnimatorSet.

當(dāng)右出動畫開始時, 點擊事件無效, 當(dāng)左入動畫結(jié)束時, 點擊事件恢復(fù).

  1. // 設(shè)置動畫 
  2.  
  3. private void setAnimators() { 
  4.  
  5.     mRightOutSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.anim_out); 
  6.  
  7.     mLeftInSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.anim_in); 
  8.  
  9.   
  10.  
  11.     // 設(shè)置點擊事件 
  12.  
  13.     mRightOutSet.addListener(new AnimatorListenerAdapter() { 
  14.  
  15.         @Override public void onAnimationStart(Animator animation) { 
  16.  
  17.             super.onAnimationStart(animation); 
  18.  
  19.             mFlContainer.setClickable(false); 
  20.  
  21.         } 
  22.  
  23.     }); 
  24.  
  25.     mLeftInSet.addListener(new AnimatorListenerAdapter() { 
  26.  
  27.         @Override public void onAnimationEnd(Animator animation) { 
  28.  
  29.             super.onAnimationEnd(animation); 
  30.  
  31.             mFlContainer.setClickable(true); 
  32.  
  33.         } 
  34.  
  35.     }); 
  36.  
  37.  

右出動畫

  1. <?xml version="1.0" encoding="utf-8"?> 
  2.  
  3. <set xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.     <!--旋轉(zhuǎn)--> 
  6.  
  7.     <objectAnimator 
  8.  
  9.         android:duration="@integer/anim_length" 
  10.  
  11.         android:propertyName="rotationY" 
  12.  
  13.         android:valueFrom="0" 
  14.  
  15.         android:valueTo="180"/> 
  16.  
  17.   
  18.  
  19.     <!--消失--> 
  20.  
  21.     <objectAnimator 
  22.  
  23.         android:duration="0" 
  24.  
  25.         android:propertyName="alpha" 
  26.  
  27.         android:startOffset="@integer/anim_half_length" 
  28.  
  29.         android:valueFrom="1.0" 
  30.  
  31.         android:valueTo="0.0"/> 
  32.  
  33. </set 

旋轉(zhuǎn)180°, 當(dāng)旋轉(zhuǎn)一半時, 卡片消失.

左入動畫

  1. <?xml version="1.0" encoding="utf-8"?> 
  2.  
  3. <set xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.   
  6.  
  7.     <!--消失--> 
  8.  
  9.     <objectAnimator 
  10.  
  11.         android:duration="0" 
  12.  
  13.         android:propertyName="alpha" 
  14.  
  15.         android:valueFrom="1.0" 
  16.  
  17.         android:valueTo="0.0"/> 
  18.  
  19.   
  20.  
  21.     <!--旋轉(zhuǎn)--> 
  22.  
  23.     <objectAnimator 
  24.  
  25.         android:duration="@integer/anim_length" 
  26.  
  27.         android:propertyName="rotationY" 
  28.  
  29.         android:valueFrom="-180" 
  30.  
  31.         android:valueTo="0"/> 
  32.  
  33.   
  34.  
  35.     <!--出現(xiàn)--> 
  36.  
  37.     <objectAnimator 
  38.  
  39.         android:duration="0" 
  40.  
  41.         android:propertyName="alpha" 
  42.  
  43.         android:startOffset="@integer/anim_half_length" 
  44.  
  45.         android:valueFrom="0.0" 
  46.  
  47.         android:valueTo="1.0"/> 
  48.  
  49. </set 

在開始時是隱藏, 逆向旋轉(zhuǎn), 當(dāng)旋轉(zhuǎn)一半時, 顯示卡片.

鏡頭視角

改變視角, 涉及到旋轉(zhuǎn)卡片的Y軸, 即rotationY, 需要修改視角距離.

如果不修改, 則會超出屏幕高度, 影響視覺體驗.

  1. // 改變視角距離, 貼近屏幕 
  2.  
  3. private void setCameraDistance() { 
  4.  
  5.     int distance = 16000; 
  6.  
  7.     float scale = getResources().getDisplayMetrics().density * distance; 
  8.  
  9.     mFlCardFront.setCameraDistance(scale); 
  10.  
  11.     mFlCardBack.setCameraDistance(scale); 
  12.  
  13.  

旋轉(zhuǎn)控制

設(shè)置右出和左入動畫的目標(biāo)控件, 兩個動畫同步進行, 并區(qū)分正反面朝上.

  1. // 翻轉(zhuǎn)卡片 
  2.  
  3. public void flipCard(View view) { 
  4.  
  5.     // 正面朝上 
  6.  
  7.     if (!mIsShowBack) { 
  8.  
  9.         mRightOutSet.setTarget(mFlCardFront); 
  10.  
  11.         mLeftInSet.setTarget(mFlCardBack); 
  12.  
  13.         mRightOutSet.start(); 
  14.  
  15.         mLeftInSet.start(); 
  16.  
  17.         mIsShowBack = true
  18.  
  19.     } else { // 背面朝上 
  20.  
  21.         mRightOutSet.setTarget(mFlCardBack); 
  22.  
  23.         mLeftInSet.setTarget(mFlCardFront); 
  24.  
  25.         mRightOutSet.start(); 
  26.  
  27.         mLeftInSet.start(); 
  28.  
  29.         mIsShowBack = false
  30.  
  31.     } 
  32.  
  33.  

動畫效果 

 

 

 

動畫效果非常簡單, 全部邏輯都不足50行, 這么好玩的動畫, 用起來吧.

OK, that’s all! Enjoy it!

責(zé)任編輯:龐桂玉 來源: 安卓開發(fā)精選
相關(guān)推薦

2011-05-30 13:23:11

Android 動畫

2024-06-04 14:17:26

2011-07-08 10:15:15

IPhone 動畫

2012-06-04 14:47:42

HTML5

2011-07-22 18:20:04

IOS View 動畫

2022-03-29 11:28:24

HarmonyOS動畫css

2015-06-18 10:33:02

iOS粘性動畫

2011-08-12 14:04:53

iPhone動畫

2011-08-16 18:13:42

IPhone開發(fā)UIView動畫

2011-07-29 13:55:10

IPhone 動畫

2024-07-25 08:55:47

進度條水缸進度動畫效果

2009-09-22 12:59:58

ibmdwDojo

2011-08-10 14:40:23

iPhone動畫

2011-07-19 13:07:26

iOS4 HTML5 動畫

2015-09-16 09:20:34

WWDC蘋果動畫效果

2017-03-22 10:35:06

AndroidRecyclerVie滑動效果

2022-06-29 21:22:49

CSS動感倒計時

2009-07-15 14:10:26

Swing控件

2017-05-08 14:35:14

JavascriptBOOM動畫效果

2014-04-15 09:28:14

移動界面動畫效果創(chuàng)意
點贊
收藏

51CTO技術(shù)棧公眾號