加入购物车动画实现

/**
     * 
     * @param itemView
     * @param targetView
     */
    public void animationItem(ImageView itemView, View targetView) {
        // 一,将imageview加入到根view中
        final ImageView imageView = new ImageView(mContext);
        imageView.setImageDrawable(itemView.getDrawable());
        imageWH = UiUtil.dip2px(mContext, 50);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(imageWH, imageWH);
        // 开始和结束View的父View
        mRootRl.addView(imageView, layoutParams);
        // 计算动画开始和结束的点的坐标
        // 获取父布局的起始点坐标
        int[] parentLoc = new int[2];
        mRootRl.getLocationInWindow(parentLoc);

        // 获取选中图片的坐标
        int[] startLoc = new int[2];
        itemView.getLocationInWindow(startLoc);

        // 获取预览区的RecyclerView的坐标
        int[] endLoc = new int[2];
        targetView.getLocationInWindow(endLoc);

        float startX = startLoc[0] - parentLoc[0] + itemView.getWidth() / 2;
        float startY = startLoc[1] - parentLoc[1] + itemView.getWidth() / 2;

        
        float toX = endLoc[0] - parentLoc[0];
        float toY = endLoc[1] - parentLoc[1];

        // 开始绘制贝塞尔曲线
        Path path = new Path();
        path.moveTo(startX, startY);
        // 使用二次贝塞尔曲线
        path.quadTo((startX + toX) / 2, startY, toX, toY);

        pathMeasure = new PathMeasure(path, false);
        // 属性动画
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, pathMeasure.getLength());
        valueAnimator.setDuration(500);
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                // 平移
                pathMeasure.getPosTan(value, mCurrentPosition, null);
                imageView.setTranslationX(mCurrentPosition[0]);
                imageView.setTranslationY(mCurrentPosition[1]);
                // 缩放
//                float fraction = animation.getAnimatedFraction();
//                imageView.setScaleX(1 - fraction);
//                imageView.setScaleY(1 - fraction);
            }
        });

        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                mRootRl.removeView(imageView);
                // TODO other thing
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        valueAnimator.start();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值