最近项目上用到了卡片的翻转效果,大致研究了下,也参考了网上的一些Demo,简单实现如下:
activity_main.xml
<&#63;xml version="1.0" encoding="utf-8"&#63;>
可以看出,用到了两个布局
activity_card_back.xml
<&#63;xml version="1.0" encoding="utf-8"&#63;>
activity_card_front.xml
<&#63;xml version="1.0" encoding="utf-8"&#63;>
附上自定义的图片:
shape_card_back_bg.xml
<&#63;xml version="1.0" encoding="utf-8"&#63;>
shape_card_front_bg.xml
<&#63;xml version="1.0" encoding="utf-8"&#63;>
主要的逻辑如下:
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(); // 设置动画 setCameraDistance(); // 设置镜头距离 } 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); // 设置点击事件 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; } } }
用到一些动画的资源:
anim_left_in.xml
<&#63;xml version="1.0" encoding="utf-8"&#63;>
anim_right_out.xml
<&#63;xml version="1.0" encoding="utf-8"&#63;>
用到了属性动画,为了兼容性,别忘了如下配置:
效果图如下:
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。