RecycleView添加动画效果

一、结合ObjectAnimator和AnimatorSet给item添加动画效果

完整代码

 /**
     * 结合ObjectAnimator和AnimatorSet给item
     * 添加动画效果:Item添加时的动画效果
     */
    private ArrayList<BaseViewHolder> baseViewHolderArrayList=new ArrayList<>();
    private void animateAddImp(BaseViewHolder holder) {
        final View view=holder.itemView;
        baseViewHolderArrayList.add(holder);
        /**
         * 抖动效果
         */
        ObjectAnimator translationX=ObjectAnimator
                .ofFloat(view,"translationX",0,20,-20,0,20,-20,0,20,-20,0)//初始化值和结束值
                .setDuration(300);//设置时长
        ObjectAnimator scaleX=ObjectAnimator//创建实例
                .ofFloat(view,"scaleX",1,0.95f,1.05f,1,0.95f,1.05f,1,0.95f,1.05f,1)
                .setDuration(300);
        /**
         * 定轴旋转
         */
        ObjectAnimator fixedRotation=ObjectAnimator
                .ofFloat(view,"rotationX",0,360)
                .setDuration(300);
        /**
         * 其他效果
         */
        ObjectAnimator scaleX2=ObjectAnimator
                .ofFloat(view,"ScaleX",1,0.5f,1.2f,0.8f,1)
                .setDuration(300);
        ObjectAnimator scaleY2=ObjectAnimator
                .ofFloat(view,"ScaleY",1,0.5f,1.2f,0.8f,1)
                .setDuration(300);

        AnimatorSet set=new AnimatorSet();
        //set.playTogether(scaleX,translationX);//结合两个效果,亦可以只使用其中一个
        set.playTogether(fixedRotation);//使用定轴旋转的效果
        //set.playTogether(scaleX2,scaleY2);//使用其他效果
        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationCancel(Animator animation) {
                view.setAlpha(1);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
            }

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
            }
        });
        set.start();
    }

步骤:

1.使用ObjectAnimator创建动画效果

/**
         * 抖动效果
         */
        ObjectAnimator translationX=ObjectAnimator
                .ofFloat(view,"translationX",0,20,-20,0,20,-20,0,20,-20,0)//初始化值和结束值
                .setDuration(300);//设置时长
        ObjectAnimator scaleX=ObjectAnimator//创建实例
                .ofFloat(view,"scaleX",1,0.95f,1.05f,1,0.95f,1.05f,1,0.95f,1.05f,1)
                .setDuration(300);
        /**
         * 定轴旋转
         */
        ObjectAnimator fixedRotation=ObjectAnimator
                .ofFloat(view,"rotationX",0,360)
                .setDuration(300);
        /**
         * 其他效果
         */
        ObjectAnimator scaleX2=ObjectAnimator
                .ofFloat(view,"ScaleX",1,0.5f,1.2f,0.8f,1)
                .setDuration(300);
        ObjectAnimator scaleY2=ObjectAnimator
                .ofFloat(view,"ScaleY",1,0.5f,1.2f,0.8f,1)
                .setDuration(300);

2.定义AnimatorSet

AnimatorSet set=new AnimatorSet();
        //set.playTogether(scaleX,translationX);//结合两个效果,亦可以只使用其中一个
        set.playTogether(fixedRotation);//使用定轴旋转的效果
        //set.playTogether(scaleX2,scaleY2);//使用其他效果

为set添加监听事件并把view联系起来:

iew.setAlpha(1);

 set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationCancel(Animator animation) {
                view.setAlpha(1);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
            }

            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
            }
        });
        set.start();

set.start();就可以启动了

猜你喜欢

转载自blog.csdn.net/weixin_42182191/article/details/85059480
今日推荐