自定义商品加入购物车动画

自定义加入购物车动画

最近有个加入购物车的动画需求,类似叮咚买菜的效果,先看效果

 

可以看到是图片裁剪成圆角,利用贝塞尔曲线,加入缩小旋转等动画效果

由于我的图片加载框架用的是glide,这里需要注意的是调用Glide Api不能错,不然无法进行bitmap裁剪,下面看代码

1.图片加载(Glide)

Glide.with(mActivity).load(listBean.getMainPicture()).asBitmap().into(holder.mIvIcon);

2.图片裁剪圆形加入贝塞尔动画

public static void shopCarAnimator(Activity activity, ImageView imageView,
                                      View cartView, final ViewGroup parentView,
                                      final OnAnimatorListener listener) {
        //第一步:
        //创造出执行动画的主题---imageView
        //代码new一个imageView,图片资源是上面的imageView的图片
        // (这个图片就是执行动画的图片,从开始位置出发,经过一个抛物线(贝塞尔曲线),移动到购物车里)
        if (activity == null || imageView == null || cartView == null || parentView == null) return;
        final ImageView goods = new ImageView(activity);

        Bitmap image = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
        Drawable drawable = new BitmapDrawable(toRoundBitmap(image));
        goods.setImageDrawable(drawable);
        //设置RelativeLayout容器(这里必须设置RelativeLayout 设置LinearLayout动画会失效)
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(300, 300);
        //把动画view添加到动画层
        parentView.addView(goods, params);

        //第二步:
        //得到父布局的起始点坐标(用于辅助计算动画开始/结束时的点的坐标)
        int[] parentLocation = new int[2];
        //获取购买按钮的在屏幕的X、Y坐标(这也是动画开始的坐标)
        parentView.getLocationInWindow(parentLocation);
        int startLoc[] = new int[2];
        //获取商品图片在屏幕中的位置
        imageView.getLocationInWindow(startLoc);
        //得到购物车图片的坐标(用于计算动画结束后的坐标)
        int endLoc[] = new int[2];
        cartView.getLocationInWindow(endLoc);

        //第三步:
        //正式开始计算动画开始/结束的坐标
        //开始掉落的商品的起始点:商品起始点-父布局起始点+该商品图片的一半
        float startX = startLoc[0] - parentLocation[0] + imageView.getWidth() / 2;// 动画开始的X坐标
        float startY = startLoc[1] - parentLocation[1] + imageView.getHeight() / 2;//动画开始的Y坐标

        //商品掉落后的终点坐标:购物车起始点-父布局起始点+购物车图片的1/5
        float toX = endLoc[0] - parentLocation[0] - imageView.getWidth()/5;
        float toY = endLoc[1] - parentLocation[1];

        //第四步:
        //计算中间动画的插值坐标,绘制贝塞尔曲线
        Path path = new Path();
        //移动到起始点(贝塞尔曲线的起点)
        path.moveTo(startX, startY);
        //第一个起始坐标越大,贝塞尔曲线的横向距离就会越大 toX,toY:为终点
        path.quadTo((startX + toX) / 2, startY, toX, toY);
        final PathMeasure pathMeasure = new PathMeasure(path, false);
        //实现动画具体博客可参考 鸿洋大神的https://blog.csdn.net/lmj623565791/article/details/38067475
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, pathMeasure.getLength());
        AnimatorSet animSet = new AnimatorSet();
        //缩放
        ObjectAnimator anim1 = ObjectAnimator.ofFloat(goods, "scaleX", 1.0f, 0.2f).setDuration(400);
        ObjectAnimator anim2 = ObjectAnimator.ofFloat(goods, "scaleY", 1.0f, 0.2f).setDuration(400);
        //顺时针旋转360°
        ObjectAnimator anim3 = ObjectAnimator.ofFloat(goods, "rotation", 0f, 360f).setDuration(200);
        animSet.play(anim1).with(anim2).with(anim3).with(valueAnimator);
        //设置动画时间
        valueAnimator.setDuration(400);
        //LinearInterpolator补间器:它的主要作用是控制动画的变化速率
        valueAnimator.setInterpolator(new LinearInterpolator());

        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) { //更新动画
                float value = (Float) animation.getAnimatedValue();
                float[] currentPosition = new float[2];
                pathMeasure.getPosTan(value, currentPosition, null);
                goods.setTranslationX(currentPosition[0]);//改变了ImageView的X位置
                goods.setTranslationY(currentPosition[1]);//改变了ImageView的Y位置
            }
        });

        //第五步:
        animSet.start();

        //第六步:
        //对动画添加监听
        animSet.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                //onAnimationStart()方法会在动画开始的时候调用
            }

            //onAnimationEnd()方法会在动画结束的时候调用
            @Override
            public void onAnimationEnd(Animator animation) {
                //把移动的图片imageView从父布局里移除
                parentView.removeView(goods);
                if (listener != null) {
                    listener.onAnimationEnd(animation);
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {
                //onAnimationCancel()方法会在动画被取消的时候调用
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
                //onAnimationRepeat()方法会在动画重复执行的时候调用
            }
        });
    }

    public interface OnAnimatorListener {
        void onAnimationEnd(Animator animator);
    }

    /**
     * 转换图片成圆形
     *
     * @param bitmap 传入Bitmap对象
     * @return
     */
    public static Bitmap toRoundBitmap(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        float roundPx;
        float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom;
        if (width <= height) {
            roundPx = width / 2;
            left = 0;
            top = 0;
            right = width;
            bottom = width;
            height = width;
            dst_left = 0;
            dst_top = 0;
            dst_right = width;
            dst_bottom = width;
        } else {
            roundPx = height / 2;
            float clip = (width - height) / 2;
            left = clip;
            right = width - clip;
            top = 0;
            bottom = height;
            width = height;
            dst_left = 0;
            dst_top = 0;
            dst_right = height;
            dst_bottom = height;
        }

        Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom);
        final Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom);
        final RectF rectF = new RectF(dst);

        paint.setAntiAlias(true);// 设置画笔无锯齿

        canvas.drawARGB(0, 0, 0, 0); // 填充整个Canvas
        paint.setColor(color);

        // 以下有两种方法画圆,drawRounRect和drawCircle
        // canvas.drawRoundRect(rectF, roundPx, roundPx, paint);// 画圆角矩形,第一个参数为图形显示区域,第二个参数和第三个参数分别是水平圆角半径和垂直圆角半径。
        canvas.drawCircle(roundPx, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, src, dst, paint); //以Mode.SRC_IN模式合并bitmap和已经draw了的Circle

        return output;
    }

3.调用改方法传入指定参数即可

以上就是一个添加购物车动画完成啦,感谢各位看官~

 

 

js加入购物车抛物线动画购物车效果特效,亲测可用, 当您在电商购物网站浏览中意的商品时,您可以点击页面中的“加入购物车”按钮即可将商品加入购物车中。本文介绍借助一款基于jQuery的动画插件,点击加入购物车按钮时,实现商品将飞入到右侧的购物车中的效果。 HTML 首先载入jQuery库文件和jquery.fly.min.js插件。 复制代码 代码如下: 接着,将商品信息html结构布置好,本例中,我们用四个商品并排布置,每个商品box中包括有商品图片、价格、名称以及加入购物车按钮等信息。 复制代码 代码如下: ¥3499.00 LG 49LF5400-CA 49寸IPS硬屏富贵招财铜钱设计 加入购物车 ¥3799.00 Hisense/海信 LED50T1A 海信电视官方旗舰店 加入购物车 ¥¥3999.00 Skyworth/创维 50E8EUS 8核4Kj极清酷开系统智能液晶电视 加入购物车 ¥6969.00 乐视TV Letv X60S 4核1080P高清3D安卓智能超级电视 加入购物车 然后,我们还需要在页面的右侧加上购物车以及提示信息。 复制代码 代码如下: 购物车 已成功加入购物车! CSS 我们使用CSS先将商品排列美化,然后设置右侧购物车样式,具体请看代码: 复制代码 代码如下: .box{float:left; width:198px; height:320px; margin-left:5px; border:1px solid #e0e0e0; text-align:center} .box p{line-height:20px; padding:4px 4px 10px 4px; text-align:left} .box:hover{border:1px solid #f90} .box h4{line-height:32px; font-size:14px; color:#f30;font-weight:500} .box h4 span{font-size:20px} .u-flyer{display: block;width: 50px;height: 50px;border-radius: 50px;position: fixed;z-index: 9999;} .m-sidebar{position: fixed;top: 0;right: 0;background: #000;z-index: 2000;width: 35px;height: 100%;font-size: 12px;color: #fff;} .cart{color: #fff;t
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值