最近項(xiàng)目上用到了卡片的翻轉(zhuǎn)效果,大致研究了下,也參考了網(wǎng)上的一些Demo,簡(jiǎn)單實(shí)現(xiàn)如下:
成都創(chuàng)新互聯(lián)公司專業(yè)為企業(yè)提供銅川網(wǎng)站建設(shè)、銅川做網(wǎng)站、銅川網(wǎng)站設(shè)計(jì)、銅川網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)與制作、銅川企業(yè)網(wǎng)站模板建站服務(wù),十余年銅川做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
可以看出,用到了兩個(gè)布局
activity_card_back.xml
<?xml version="1.0" encoding="utf-8"?>
activity_card_front.xml
<?xml version="1.0" encoding="utf-8"?>
附上自定義的圖片:
shape_card_back_bg.xml
<?xml version="1.0" encoding="utf-8"?>
shape_card_front_bg.xml
<?xml version="1.0" encoding="utf-8"?>
主要的邏輯如下:
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"?>
anim_right_out.xml
<?xml version="1.0" encoding="utf-8"?>
用到了屬性動(dòng)畫,為了兼容性,別忘了如下配置:
效果圖如下:
效果圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。