一个简单的方法实现购物车小球动画

来源:互联网 发布:powermill编程好学吗 编辑:程序博客网 时间:2024/05/17 06:59

只有代码,仔细看看,实现起来其实很简单

 /**     * 执行添加商品动画     *     * @param goodsImg 动画小球     * @param position 所在列表中的位置     */    private void addGoodsToCart(ImageView goodsImg, final int position) {        // 创造出执行动画的ImageView (这个图片就是执行动画的图片,从开始位置出发,经过一个抛物线,移动到购物车里)        final ImageView ivAdd = new ImageView(BuyerMerchantsHomeActivity.this);        ivAdd.setImageDrawable(goodsImg.getDrawable());        //获取动画小球所在父容器中的位置(recyclerView 的父容器)        int startLoc[] = new int[2];        goodsImg.getLocationInWindow(startLoc);        //设置执行动画的ImageView 的大小        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(64, 64);        //获取执行动画的RelativeLayout 在父容器中的位置        int[] parentLocation = new int[2];        rlContent.getLocationInWindow(parentLocation);        //设置动画imageView 在RelativeLayout中的起始位置        params.setMargins(startLoc[0], startLoc[1] - parentLocation[1], 0, 0);        rlContent.addView(ivAdd, params);        //利用属性动画对小球同时作x、y轴位移(x轴减速,y轴加速,合成抛物线)        ObjectAnimator animator = ObjectAnimator.ofFloat(ivAdd, "translationX", endLoc[0] - startLoc[0] + SizeUtils.dp2px(BuyerMerchantsHomeActivity.this, 24));        animator.setInterpolator(new DecelerateInterpolator());        ObjectAnimator animator1 = ObjectAnimator.ofFloat(ivAdd, "translationY", endLoc[1] - startLoc[1]);        animator1.setInterpolator(new AccelerateDecelerateInterpolator());        AnimatorSet set = new AnimatorSet();        set.setDuration(500);        set.playTogether(animator, animator1);        set.start();        //动画完成时进行监听        set.addListener(new Animator.AnimatorListener() {            @Override            public void onAnimationStart(Animator animation) {            }            @Override            public void onAnimationEnd(Animator animation) {                rlContent.removeView(ivAdd);                count++;                total = MyMathUtil.add(total, goods.get(position - 1).getPrice());                badgeView.setBadgeNumber(count);                tvTotal.setText("¥" + String.format("%.2f", total));//                EventBus.getDefault().post(new AddMsg(true, buyerTypeItems.get(position).getBusItems().get(childPosition)));            }            @Override            public void onAnimationCancel(Animator animation) {            }            @Override            public void onAnimationRepeat(Animator animation) {            }        });    }
0 0
原创粉丝点击