android 添加到购物车动画

今天做添加到购物车的动画,之前有做过,只是拷贝被人的代码而已,今天想起来自己整理了一下,没相当这么简单,下面就是我封装的代码,只需要简单的操作就可以完成加入购物车动画。

先来一张效果图:


代码只复制了一部分,用起来方便灵活

   /**
     * @param from:点击位置的view
     * @param to:终点位置的view
     */
    private void showAssignView(View from, View to) {


        // 一个整型数组,用来存储按钮的在屏幕的X、Y坐标
        final int[] startLocation = new int[2];
        // 这是获取购买按钮的在屏幕的X、Y坐标(这也是动画开始的坐标)
        from.getLocationInWindow(startLocation);

        final int[] endLocation = new int[2];
        to.getLocationInWindow(endLocation);

        final int moveX = endLocation[0] - startLocation[0];
        final int moveY = endLocation[1] - startLocation[1];

        final ViewGroup rootView = (ViewGroup) this.getWindow().getDecorView();
        final View addView = getAssignView(startLocation, rootView);

        //水平方向动画
        ObjectAnimator animatorX = ObjectAnimator.ofFloat(addView, "translationX", 0, moveX);
        animatorX.setDuration(1000);


        //竖直方向动画
        ObjectAnimator animatorY = new ObjectAnimator();
        animatorY.setInterpolator(new AccelerateInterpolator());
        animatorY.setPropertyName("translationY");
        animatorY.setTarget(addView);
        animatorY.setFloatValues(0, moveY);
        animatorY.setDuration(1000);


//        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//
//                int move = (int) animation.getAnimatedValue();
//                int tempX =  (int) (moveX / 300f * move);
//                int tempY =  (int) (moveY / 300f * move);
//                addView.setTranslationX(tempX);
//                addView.setTranslationY(tempY);
//            }
//        });
//        animator.addListener(new Animator.AnimatorListener() {
//            @Override
//            public void onAnimationStart(Animator animation) {
//
//            }
//
//            @Override
//            public void onAnimationEnd(Animator animation) {
//
//                if (addView != null)
//                    rootView.removeView(addView);
//            }
//
//            @Override
//            public void onAnimationCancel(Animator animation) {
//
//            }
//
//            @Override
//            public void onAnimationRepeat(Animator animation) {
//
//            }
//        });
//        animator.start();
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(animatorX, animatorY);
        animatorSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {

                if (addView != null)
                    rootView.removeView(addView);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animatorSet.start();

    }

    private View getAssignView(int[] startLocation, ViewGroup rootView) {
        ImageView imageView = new ImageView(this);
        rootView.addView(imageView);

        //显示动画的图片,这里设置添加到购物车的图片
        Drawable drawable = getResources().getDrawable(leftImage[leftPosition]);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                drawable.getMinimumWidth() * 2,
                drawable.getMinimumHeight() * 2);

        imageView.setImageDrawable(drawable);
        layoutParams.leftMargin = startLocation[0] + 50;
        layoutParams.topMargin = startLocation[1] + 50;
        imageView.setLayoutParams(layoutParams);

        return imageView;
    }



猜你喜欢

转载自blog.csdn.net/luck_xiang/article/details/75533371