1. 自定义View学习

 

作者:BakerJQ

项目地址:

https://github.com/BakerJQ/Android-InfiniteCards

效果:

 

 

自定义实现神奇的卡片切换效果_JAVA

使用

build.gradle中引用

 

compile 'com.bakerj:infinite-cards:1.0.1'

 

参数

 

 

animType : 动效展示类型

front : 将点击的卡片切换到第一个

switchPosition : 将点击的卡片和第一张卡片互换位置

frontToLast : 将第一张卡片移到最后,后面的卡片往前移动一个

cardRatio : 卡片宽高比

animDuration : 卡片动效时间

animAddRemoveDelay : 卡片组切换时,添加与移出时,相邻卡片展示动效的间隔时间

animAddRemoveDuration : 卡片组切换时,添加与移出时,卡片动效时间

 

 

xml布局

<com.bakerj.infinitecards.InfiniteCardView

        android:id="@+id/view"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        infiniteCard:animDuration="1000"

        infiniteCard:cardRatio="1"/>

 

设置Adapter直接继承BaseAdapter

 

 

class MyAdapter extends BaseAdapter{

  ...

}

mAdapter = new MyAdapter(resId);

mCardView.setAdapter(mAdapter);

动效的转换和插值默认

默认情况下可以不设置,或者设置为各种Default

 

 

mCardView.setAnimInterpolator(new LinearInterpolator());

mCardView.setTransformerToFront(new DefaultTransformerToFront());

mCardView.setTransformerToBack(new DefaultTransformerToBack());

mCardView.setZIndexTransformerToBack(new DefaultZIndexTransformerCommon());

 

自定义通过设置转换器与插值器,根据回调中的参数自定义动画效果

 

mCardView.setTransformerToBack(new AnimationTransformer() {

    @Override

    public void transformAnimation(View view, float fraction, int cardWidth, int cardHeight, int fromPosition, int toPosition) {

        int positionCount = fromPosition - toPosition;

        float scale = (0.8f - 0.1f * fromPosition) + (0.1f * fraction * positionCount);

        ViewHelper.setScaleX(view, scale);

        ViewHelper.setScaleY(view, scale);

        if (fraction < 0.5) {

            ViewCompat.setRotationX(view, 180 * fraction);

        } else {

            ViewCompat.setRotationX(view, 180 * (1 - fraction));

        }

    }

    @Override

    public void transformInterpolatedAnimation(View view, float fraction, int cardWidth, int cardHeight, int fromPosition, int toPosition) {

        int positionCount = fromPosition - toPosition;

        float scale = (0.8f - 0.1f * fromPosition) + (0.1f * fraction * positionCount);

        ViewHelper.setTranslationY(view, -cardHeight * (0.8f - scale) * 0.5f - cardWidth * (0.02f *

                fromPosition - 0.02f * fraction * positionCount));

    }

});

mCardView.setZIndexTransformerToBack(new ZIndexTransformer() {

    @Override

    public void transformAnimation(CardItem card, float fraction, int cardWidth, int cardHeight, int fromPosition, int toPosition) {

        if (fraction < 0.4f) {

            card.zIndex = 1f + 0.01f * fromPosition;

        } else {

            card.zIndex = 1f + 0.01f * toPosition;

        }

    }

    @Override

    public void transformInterpolatedAnimation(CardItem card, float fraction, int cardWidth, int cardHeight, int fromPosition, int toPosition) {

    }

});

https://mp.weixin.qq.com/s/zbZJwDMqr0kRfAo4I-IVxw