Android实现卡片翻转动画
最近项目上用到了卡片的翻转效果,大致研究了下,也参考了网上的一些Demo,简单实现如下:
成都创新互联公司专业为企业提供铜川网站建设、铜川做网站、铜川网站设计、铜川网站制作等企业网站建设、网页设计与制作、铜川企业网站模板建站服务,十余年铜川做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
可以看出,用到了两个布局
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(); // 设置动画 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
<?xml version="1.0" encoding="utf-8"?>
anim_right_out.xml
<?xml version="1.0" encoding="utf-8"?>
用到了属性动画,为了兼容性,别忘了如下配置:
效果图如下:
效果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持创新互联。
分享标题:Android实现卡片翻转动画
链接分享:http://scjbc.cn/article/gejsjs.html