溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時間:2021-11-20 16:47:38 來源:億速云 閱讀:154 作者:小新 欄目:移動開發(fā)

小編給大家分享一下android如何實現(xiàn)翻轉(zhuǎn)卡片的動畫效果,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

首頁

首頁由正面和背面兩張卡片組成, 同時, 設(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&deg;, 當(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è)置右出和左入動畫的目標控件, 兩個動畫同步進行, 并區(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;      }  }

動畫效果

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

以上是“android如何實現(xiàn)翻轉(zhuǎn)卡片的動畫效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI