android實現(xiàn)撲克卡片翻轉(zhuǎn)
今天看見一個Android 撲克卡片翻轉(zhuǎn)效果的帖子,于是手癢想學(xué)一學(xué),由于接觸過的Animation動畫等比較少,所以感覺很新奇。
首先,說一下布局,是FrameLayout,這個布局設(shè)置一點點擊方法,要設(shè)置id,之后會用到。這個布局還包括兩個子布局,分別是Poke的正面和反面布局。上代碼:
還需額外注意一點:這是剛剛才發(fā)現(xiàn)的問題,在主activity中,正反面那個xml文件放在后面就會優(yōu)先默認顯示那個xml文件,所以,如果我需要一打開app就看到正面的話,那么正面xml文件需要放到反面xml文件的下面,就是
<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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:paddingBottom="@dimen/activity_vertical_margin" ? ? android:paddingLeft="@dimen/activity_horizontal_margin" ? ? android:paddingRight="@dimen/activity_horizontal_margin" ? ? android:paddingTop="@dimen/activity_vertical_margin" ? ? android:onClick="flipCard" ? ? android:id="@+id/main_fl_container" ? ? tools:context="com.example.chenxuanhe.poketest.MainActivity"> ? ? <include layout="@layout/cell_card_back" ? ? ? ? /> ? ? <include layout="@layout/cell_card_front" ? ? ? ? /> </FrameLayout>
根據(jù)代碼的邏輯線走,則是接下來的兩個layout:
這兩個FrameLayout也是需要寫id的,之后會用到。
cell_card_back.xml:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:id="@+id/main_fl_card_back" ? ? > ? ? <ImageView ? ? ? ? android:src="@drawable/rectangle_back" ? ? ? ? android:contentDescription="@null" ? ? ? ? android:padding="16dp" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent" ? ? ? ? /> ? ? <TextView ? ? ? ? android:textColor="@color/colorAccent" ? ? ? ? android:text="反面" ? ? ? ? android:textSize="40dp" ? ? ? ? android:layout_gravity="center" ? ? ? ? android:gravity="center" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent" /> </FrameLayout>
cell_card_front.xml:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:id="@+id/main_fl_card_front"> ? ? <ImageView ? ? ? ? android:src="@drawable/rectangle_front" ? ? ? ? android:padding="16dp" ? ? ? ? android:contentDescription="@null" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent" /> ? ? <TextView ? ? ? ? android:textSize="40dp" ? ? ? ? android:textColor="@color/colorPrimary" ? ? ? ? android:text="正面" ? ? ? ? android:gravity="center" ? ? ? ? android:layout_gravity="center" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent" /> </FrameLayout>
繼續(xù)順著上面兩個布局的邏輯線來走,就需要用到兩個Drawable的文件作為背景圖,所以接著看drawable文件:
rectangle_back.xml:
大概是一個黑邊紅底色帶圓角的卡片界面
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:shape="rectangle"> ? ? <corners android:radius="16dp"/> ? ? <solid android:color="@color/cardBack"/> ? ? <stroke android:width="2dp" ? ? ? ? android:color="@android:color/black"/> </shape>
rectangle_front.xml:
大概是一個黑邊灰底色帶圓角的卡片界面
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:shape="rectangle"> ? ? <corners android:radius="16dp"/> ? ? <solid android:color="@color/cardFront"/> ? ? <stroke android:width="2dp" ? ? ? ? android:color="@android:color/black"/> </shape>
走完界面UI的邏輯線之后,就去看Java代碼,發(fā)現(xiàn)需要兩個Animator,于是在res文件下創(chuàng)建一個animator資源文件夾,在下創(chuàng)建兩個動畫文件:
anim-in.xml:
這是一個從左邊進入的動畫,一開始是隱藏的,逆向旋轉(zhuǎn),當(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="3000" ? ? ? ? android:propertyName="rotationY" ? ? ? ? android:valueFrom="-180" ? ? ? ? android:valueTo="0"/> ? ? <!--出現(xiàn)--> ? ? <objectAnimator ? ? ? ? android:duration="0" ? ? ? ? android:propertyName="alpha" ? ? ? ? android:startOffset="1500" ? ? ? ? android:valueFrom="0.0" ? ? ? ? android:valueTo="1.0"/> </set>
anim_out.xml:
這是一個右邊出去的動畫,旋轉(zhuǎn)180度,旋轉(zhuǎn)到一半時,卡片就消失了。
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> ? ? <objectAnimator ? ? ? ? android:duration="3000" ? ? ? ? android:propertyName="rotationY" ? ? ? ? android:valueFrom="0" ? ? ? ? android:valueTo="180"/> ? ? <!--消失--> ? ? <objectAnimator ? ? ? ? android:duration="0" ? ? ? ? android:propertyName="alpha" ? ? ? ? android:startOffset="1500" ? ? ? ? android:valueFrom="1.0" ? ? ? ? android:valueTo="0.0"/> </set>
接著看Java代碼:
MainActivity:
public class MainActivity extends AppCompatActivity { ? ? @Bind(R.id.main_fl_card_back) ? ? FrameLayout mFlCardBack; ? ? @Bind(R.id.main_fl_card_front) ? ? FrameLayout mFlCardFront; ? ? @Bind(R.id.main_fl_container) ? ? FrameLayout mFlContainer; ? ? private AnimatorSet mLeftInSet; ? ? private AnimatorSet mRightOutSet; ? ? private boolean mIsShowBack; ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.activity_main); ? ? ? ? ButterKnife.bind(this); ? ? ? ? setAnimation();//設(shè)置動畫 ? ? ? ? setCameraDistance();//設(shè)置鏡頭距離,在這里不是太懂 ? ? } ? ? private void setAnimation() { ? ? //mLeftInSet是左邊進入的動畫 ? ? ? ? mLeftInSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.anim_in); ? ?//mRightOutSet是右邊出去的動畫 ? ? ? ? mRightOutSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.anim_out); ? ? ? ? //點擊事件 ? ? ? ? //通過ListenerAdapter就不需重寫所有方法,只需寫需要寫的方法 ? ? ? ? mRightOutSet.addListener(new AnimatorListenerAdapter() { ? ? ? ? //動畫開始時候 ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onAnimationStart(Animator animation) { ? ? ? ? ? ? ? ? super.onAnimationStart(animation); ? ? ? ? ? ? ? ? mFlContainer.setClickable(false); ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? //動畫結(jié)束的時候 ? ? ? ? mLeftInSet.addListener(new AnimatorListenerAdapter() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public void onAnimationEnd(Animator animation) { ? ? ? ? ? ? ? ? super.onAnimationEnd(animation); ? ? ? ? ? ? ? ? mFlContainer.setClickable(true);//主布局中framelayouy的就允許你去點擊了 ? ? ? ? ? ? } ? ? ? ? }); ? ? } //一直不是很懂的設(shè)置鏡頭距離, //帖子上的注釋寫著:改變視角距離,貼近屏幕 ? ? private void setCameraDistance() { ? ? ? ? int distance = 16000; ? ? ? ? float scale = getResources().getDisplayMetrics().density*distance; ? ? ? ? mFlCardFront.setCameraDistance(scale);//設(shè)置距離 ? ? ? ? mFlCardBack.setCameraDistance(scale);//設(shè)置距離 ? ? } //這是主Framelayout的點擊方法 ? ? public void flipCard(View view){ ? ? //mIsShowBack可以理解為互斥,所以為boolean ? ? ? ? if(!mIsShowBack){ ? ? ? ? //右出動畫設(shè)置在正面卡片界面 ? ? ? ? ? ? mRightOutSet.setTarget(mFlCardFront); ? ? ? ? //左入動畫設(shè)置在反面卡片界面 ? ? ? ? ? ? ? ? mLeftInSet.setTarget(mFlCardBack); ? ? ? ? ? ? //開始動畫? ? ? ? ? ? ? mRightOutSet.start(); ? ? ? ? ? ? mLeftInSet.start(); ? ? ? ? ? ? mIsShowBack = true; ? ? ? ? }else { ? ? ? ? //右出動畫設(shè)置在卡片背面界面 ? ? ? ? ? ? mRightOutSet.setTarget(mFlCardBack); ? ? ? ? ?//左入動畫設(shè)置在卡片正面界面 ? ? ? ? ? ? mLeftInSet.setTarget(mFlCardFront); ? ? ? ? ? ? mRightOutSet.start(); ? ? ? ? ? ? mLeftInSet.start(); ? ? ? ? ? ? mIsShowBack = false; ? ? ? ? } ? ? } ? ? //這一點我一直沒想到,還可以在onDestroy方法中解綁ButterKnife ? ? protected void onDestroy(){ ? ? ? ? super.onDestroy(); ? ? ? ? ButterKnife.unbind(this); ? ? } }
到這一步,基本就全部完成了。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實現(xiàn)圖片反轉(zhuǎn)、翻轉(zhuǎn)、旋轉(zhuǎn)、放大和縮小
- Android動畫之3D翻轉(zhuǎn)效果實現(xiàn)函數(shù)分析
- Android圖片翻轉(zhuǎn)動畫簡易實現(xiàn)代碼
- Android實現(xiàn)Flip翻轉(zhuǎn)動畫效果
- Android實現(xiàn)文字翻轉(zhuǎn)動畫的效果
- android使用FlipAnimation實現(xiàn)3D垂直翻轉(zhuǎn)動畫
- Android實現(xiàn)3D翻轉(zhuǎn)動畫效果
- Android利用Camera實現(xiàn)中軸3D卡牌翻轉(zhuǎn)效果
- Android實現(xiàn)卡片翻轉(zhuǎn)動畫
- android camera yuv幀水平翻轉(zhuǎn)實例
相關(guān)文章
Android應(yīng)用圖標(biāo)在狀態(tài)欄上顯示實現(xiàn)原理
Android應(yīng)用圖標(biāo)在狀態(tài)欄上顯示,以及顯示不同的圖標(biāo),其實很研究完后,才發(fā)現(xiàn),很簡單,具體實現(xiàn)如下,感興趣的朋友可以參考下哈2013-06-06android異步任務(wù)設(shè)計思詳解(AsyncTask)
AsyncTask在Android十分常用,那為什么如此常用呢,不用行不行呢,內(nèi)部又是怎么實現(xiàn)的呢,為什么Java的API中沒有這個類呢,看完本文后,你將會知道答案2014-02-02Android中導(dǎo)航組件Navigation的實現(xiàn)原理
大家好,本篇文章主要講的是Android中導(dǎo)航組件Navigation的實現(xiàn)原理,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-02-02Kotlin使用flow實現(xiàn)倒計時功能(示例詳解)
這篇文章主要介紹了Kotlin使用flow實現(xiàn)倒計時功能,本文通過圖文實例相結(jié)合給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2024-02-02Android?RecyclerLineChart實現(xiàn)圖表繪制教程
這篇文章主要為大家介紹了Android?RecyclerLineChart實現(xiàn)圖表繪制教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12