Android實(shí)現(xiàn)卡片翻轉(zhuǎn)動(dòng)畫
最近項(xiàng)目上用到了卡片的翻轉(zhuǎn)效果,大致研究了下,也參考了網(wǎng)上的一些Demo,簡(jiǎn)單實(shí)現(xiàn)如下:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/card_main_container" android:layout_height="match_parent" android:layout_width="match_parent"> <include layout="@layout/activity_card_back"/> <include layout="@layout/activity_card_front"/> </FrameLayout>
可以看出,用到了兩個(gè)布局
activity_card_back.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/card_back_container" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="@dimen/activity_horizontal_margin" android:layout_marginTop="@dimen/activity_vertical_margin" android:layout_marginRight="@dimen/activity_horizontal_margin" android:layout_marginBottom="@dimen/activity_vertical_margin" android:background="@drawable/shape_card_back_bg" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="背面" android:textSize="30sp"/> </LinearLayout>
activity_card_front.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/card_font_container" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="@dimen/activity_horizontal_margin" android:layout_marginTop="@dimen/activity_vertical_margin" android:layout_marginRight="@dimen/activity_horizontal_margin" android:layout_marginBottom="@dimen/activity_vertical_margin" android:background="@drawable/shape_card_front_bg" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="正面" android:textSize="30sp"/> </LinearLayout>
附上自定義的圖片:
shape_card_back_bg.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="30dp"/> <solid android:color="@android:color/holo_red_light"/> </shape>
shape_card_front_bg.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="30dp"/> <solid android:color="@android:color/darker_gray"/> </shape>
主要的邏輯如下:
package com.jackie.flipanimationdemo;
import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private FrameLayout mCardMainContainer;
private LinearLayout mCardFontContainer, mCardBackContainer;
private AnimatorSet mRightOutAnimatorSet, mLeftInAnimatorSet;
private boolean mIsShowBack = false; //是否顯示背面
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initEvent();
}
private void initView() {
mCardMainContainer = (FrameLayout) findViewById(R.id.card_main_container);
mCardFontContainer = (LinearLayout) findViewById(R.id.card_font_container);
mCardBackContainer = (LinearLayout) findViewById(R.id.card_back_container);
setAnimators(); // 設(shè)置動(dòng)畫
setCameraDistance(); // 設(shè)置鏡頭距離
}
private void initEvent() {
mCardMainContainer.setOnClickListener(this);
}
private void setAnimators() {
mRightOutAnimatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.anim_right_out);
mLeftInAnimatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.anim_left_in);
// 設(shè)置點(diǎn)擊事件
mRightOutAnimatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
mCardMainContainer.setClickable(false);
}
});
mLeftInAnimatorSet.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
mCardMainContainer.setClickable(true);
}
});
}
private void setCameraDistance() {
int distance = 16000;
float scale = getResources().getDisplayMetrics().density * distance;
mCardFontContainer.setCameraDistance(scale);
mCardBackContainer.setCameraDistance(scale);
}
private void flipCard() {
if (!mIsShowBack) { // 正面朝上
mRightOutAnimatorSet.setTarget(mCardFontContainer);
mLeftInAnimatorSet.setTarget(mCardBackContainer);
mRightOutAnimatorSet.start();
mLeftInAnimatorSet.start();
mIsShowBack = true;
} else { // 背面朝上
mRightOutAnimatorSet.setTarget(mCardBackContainer);
mLeftInAnimatorSet.setTarget(mCardFontContainer);
mRightOutAnimatorSet.start();
mLeftInAnimatorSet.start();
mIsShowBack = false;
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.card_main_container:
flipCard();
break;
}
}
} 用到一些動(dòng)畫的資源:
anim_left_in.xml
<?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>
anim_right_out.xml
<?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>
用到了屬性動(dòng)畫,為了兼容性,別忘了如下配置:
效果圖如下:

效果圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)圖片反轉(zhuǎn)、翻轉(zhuǎn)、旋轉(zhuǎn)、放大和縮小
- Android動(dòng)畫之3D翻轉(zhuǎn)效果實(shí)現(xiàn)函數(shù)分析
- Android圖片翻轉(zhuǎn)動(dòng)畫簡(jiǎn)易實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)Flip翻轉(zhuǎn)動(dòng)畫效果
- Android實(shí)現(xiàn)文字翻轉(zhuǎn)動(dòng)畫的效果
- android使用FlipAnimation實(shí)現(xiàn)3D垂直翻轉(zhuǎn)動(dòng)畫
- Android實(shí)現(xiàn)3D翻轉(zhuǎn)動(dòng)畫效果
- Android利用Camera實(shí)現(xiàn)中軸3D卡牌翻轉(zhuǎn)效果
- android camera yuv幀水平翻轉(zhuǎn)實(shí)例
- android實(shí)現(xiàn)撲克卡片翻轉(zhuǎn)
相關(guān)文章
Android編程判斷是否連接網(wǎng)絡(luò)的方法【W(wǎng)iFi及3G判斷】
這篇文章主要介紹了Android編程判斷是否連接網(wǎng)絡(luò)的方法,結(jié)合實(shí)例形式分析了Android針對(duì)WiFi及3G網(wǎng)絡(luò)連接的判斷方法,需要的朋友可以參考下2017-02-02
Android?Studio實(shí)現(xiàn)簡(jiǎn)單計(jì)算器開發(fā)
這篇文章主要為大家詳細(xì)介紹了Android?Studio實(shí)現(xiàn)簡(jiǎn)單計(jì)算器開發(fā),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Android EditTextView 實(shí)現(xiàn)帶空格分隔的輸入(電話號(hào)碼,銀行卡)
這篇文章主要介紹了Android EditTextView 實(shí)現(xiàn)帶空格分隔的輸入(電話號(hào)碼,銀行卡)的相關(guān)資料,需要的朋友可以參考下2018-02-02
Android中TabLayout結(jié)合ViewPager實(shí)現(xiàn)頁(yè)面切換
這篇文章主要為大家詳細(xì)介紹了Android中TabLayout結(jié)合ViewPager實(shí)現(xiàn)頁(yè)面切換效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
Android中使用開源框架eventbus3.0實(shí)現(xiàn)fragment之間的通信交互
本文主要介紹了Android中使用開源框架eventbus3.0實(shí)現(xiàn)fragment之間的通信交互的方法,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02
Android實(shí)現(xiàn)點(diǎn)擊兩次BACK鍵退出應(yīng)用
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)點(diǎn)擊兩次BACK鍵退出應(yīng)用的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
Android實(shí)現(xiàn)調(diào)用攝像頭拍照并存儲(chǔ)照片
本文主要介紹了如何利用Android調(diào)用攝像頭拍照,并顯示拍照后的圖片到ImageView中,文中的示例代碼講解詳細(xì),感興趣的可以動(dòng)手試一試2022-01-01
textView 添加超鏈接(兩種實(shí)現(xiàn)方式)
在textView添加超鏈接,有兩種方式,第一種通過(guò)HTML格式化你的網(wǎng)址,一種是設(shè)置autolink,讓系統(tǒng)自動(dòng)識(shí)別超鏈接,下面為大家介紹下這兩種方法的實(shí)現(xiàn)2013-06-06
Android AIDL和遠(yuǎn)程Service調(diào)用示例代碼
本文主要介紹Android AIDL和遠(yuǎn)程Service,這里詳細(xì)介紹了相關(guān)知識(shí),并附實(shí)例代碼和實(shí)現(xiàn)效果圖,有興趣的朋友參考下2016-08-08

