实现加入购物车动画

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaoshuxgh/article/details/85096992

最近一直在总结之前写过的一些功能,今年就记录一下之前实现的加入购物车有动画

1.点击的位置是开始位置

2.购物车图片是结束位置

3.点击加入的时候获取开始位置坐标,并获取动画图片

4.加入购物车成功后,开始发送handle 执行动画

下面直接上代码:

private int[] startLocation;
private ImageView ball;// 小圆点
private ViewGroup anim_mask_layout;//动画层
private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case 0:
                setAnim(ball, startLocation);// 开始执行动画
                break;
            case 4:
                int page1 = (Integer) msg.obj;
                viewPager.setCurrentItem(page1);
                break;
        }
    }
};
//弹出动画
startLocation = new int[2];// 一个整型数组,用来存储按钮的在屏幕的X、Y坐标
view.getLocationInWindow(startLocation);// 这是获取购买按钮的在屏幕的X、Y坐标(这也是动画开始的坐标)
ball = new ImageView(MakerApplyProductActivity.this);// buyImg是动画的图片,我的是一个小球(R.drawable.sign)

ball.setLayoutParams(new ViewGroup.LayoutParams(80, 80));
//    ball.setImageResource(R.drawable.mall_cart);// 设置buyImg的图片
GlideUtils.loadPic(MakerApplyProductActivity.this,
        item.get商品主图(),
        ball);
 new Thread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Message msg = new Message();
                                        msg.arg1 = 0;
                                        msg.obj = tab;
//                                            handler.sendEmptyMessage(0);
                                        handler.sendMessage(msg);
                                    }
                                }).start();

                            }
 private void setAnim(final View v, int[] startLocation) {

        anim_mask_layout = null;
        anim_mask_layout = createAnimLayout();
        anim_mask_layout.addView(v);//把动画小球添加到动画层
        final View view = addViewToAnimLayout(anim_mask_layout, v,
                startLocation);
        int[] endLocation = new int[2];// 存储动画结束位置的X、Y坐标

//        tab.getTabAt(tab.getSelectedTabPosition() + 1).getLocationInWindow(endLocation);// re_zhongcai_tanchu是那个抛物线最后掉落的控件
        tab.getLocationInWindow(endLocation);

        // 计算位移
        int endX = 0 - startLocation[0] + 250;// 动画位移的X坐标
        int endY = endLocation[1] - startLocation[1];// 动画位移的y坐标
        TranslateAnimation translateAnimationX = new TranslateAnimation(0,
                endX, 0, 0);
        translateAnimationX.setInterpolator(new LinearInterpolator());
        translateAnimationX.setRepeatCount(0);// 动画重复执行的次数
        translateAnimationX.setFillAfter(true);

        TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0,
                0, endY);
        translateAnimationY.setInterpolator(new AccelerateInterpolator());
        translateAnimationY.setRepeatCount(0);// 动画重复执行的次数
        translateAnimationX.setFillAfter(true);

        final AnimationSet set = new AnimationSet(false);
        set.setFillAfter(false);
        set.addAnimation(translateAnimationY);
        set.addAnimation(translateAnimationX);
        set.setDuration(1000);// 动画的执行时间
        view.startAnimation(set);
        // 动画监听事件
        set.setAnimationListener(new Animation.AnimationListener() {
            // 动画的开始
            @Override
            public void onAnimationStart(Animation animation) {
                v.setVisibility(View.VISIBLE);
                //    Log.e("动画","asdasdasdasd");
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub
            }

            // 动画的结束
            @Override
            public void onAnimationEnd(Animation animation) {
                v.setVisibility(View.GONE);
                set.cancel();
                animation.cancel();
//                anim_mask_layout.removeView(v);
            }
        });

    }

    /**
     * @param
     * @return void
     * @throws
     * @Description: 创建动画层
     */
    private ViewGroup createAnimLayout() {
//        final Dialog dialog = new Dialog(context, R.style.dialogStyle);
//        Window window = dialog.getWindow();
        ViewGroup rootView = (ViewGroup) this.getWindow().getDecorView();
//        if(rootView != null) rootView.removeAllViews();
        LinearLayout animLayout = new LinearLayout(this);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        animLayout.setLayoutParams(lp);
        animLayout.setId(Integer.MAX_VALUE);
        animLayout.setBackgroundResource(android.R.color.transparent);
        rootView.addView(animLayout);
        return animLayout;
    }

    private View addViewToAnimLayout(final ViewGroup parent, final View view,
                                     int[] location) {
        int x = location[0];
        int y = location[1];
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        lp.leftMargin = x;
        lp.topMargin = y;
        view.setLayoutParams(lp);
        return view;
    }

猜你喜欢

转载自blog.csdn.net/xiaoshuxgh/article/details/85096992