Android卡片翻转动画

Android卡片翻转动画_第1张图片
build.gradle下加入动画依赖

 implementation 'com.nineoldandroids:library:2.4.0'

1:activity_main.xml




    

    

2:ly_choose_product_item_front.xml正面的布局



    

3:ly_choose_product_item_back.xml反面的布局



    

4:Android卡片翻转动画_第2张图片
4.1:anim_left_in.xml



    
    

    
    

    
    

4.2:anim_right_out.xml



    
    

    
    

5:MainActivity

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);
    }

    @SuppressLint("ResourceType")
    private void setAnimators() {
        mRightOutAnimatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.anim.anim_right_out);
        mLeftInAnimatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.anim.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;
        }
    }
}

你可能感兴趣的:(Android卡片翻转动画)