Android关于仿外卖购物车小球的思路

      想生成一个 + 号小球,并 让  + 号  小球运动起来

1、必须在点击的位置处生成一个  + 号 小球,这里使用ImageView,设置src 图片

2、生成的ImageView 需要添加在其父View 上

3、所以 通过 setX 和 setY ,必须是相对于父View的位置 而存在

    即 通过    点击位置(相对于屏幕的位置) 和父View 的位置  计算得出
    
    x 轴不变, y 轴=点击位置y - 父View的位置y
4、开始执行动画


整理代码:        
   

   /**
    * 添加 加号 小球到 购物车
    */ 
    public void addBallToShoppingCar(int[] clickLocation) {

        ImageView imageview = new ImageView(mCtx);
        imageview.setImageResource(R.mipmap.img_add);
        int[] desLocation = new int[2];
       
        shoppingCarView.getLocationInWindow(desLocation);
        
        Log.d("目的地 x 轴 " + desLocation[0], "目的地  y 轴" + desLocation[1]);
        addViewToWindowView((ViewGroup) getWindow().getDecorView(), imageview, clickLocation, desLocation);

        // 添加的 ImageView 必须是  添加在最底层的 DecorView ,这样ImageView的移动范围才是整个屏幕,
        //    (ViewGroup) getWindow().getDecorView()
        //  而不是 convertView ,如果是 converView 的话,那么 其 Y 轴  移动的 范围就只是convertView 的高度,
        // 就会产生 imgaeView被 listView的item吃掉的错觉

    }

    /**
     * 添加一个ImageView 到 父View 上
     *
     * @param clickLocation
     * @param desLocation
     */
    private void addViewToWindowView(ViewGroup fatherView,View imageview, int[] clickLocation, int[] desLocation) {

        int x = clickLocation[0];
        int y = clickLocation[1];
        int[] fatherLocation = new int[2];
        fatherView.getLocationInWindow(fatherLocation);  //测出父view的位置

        imageview.setX(x);
        imageview.setY(y - fatherLocation[1]);

        fatherView.addView(imageview,50,50);  // 因为是添加在父View 上的,而且 使用的是setX  和 setY,所以要用  相对位置来表示imageView的位置
                                                                       //  指定其宽高为50
        startAnimation(fatherView, imageview, clickLocation, desLocation);

    }

    public void startAnimation(final ViewGroup vg, final View view, int[] start_location, int[] desLocation) {

        //动画主要是根据 物理的平抛运动,水平方向匀速,竖直方向加速

        AnimationSet set = new AnimationSet(false);
        Animation translationX = new TranslateAnimation(
                Animation.ABSOLUTE, 0,
                Animation.ABSOLUTE, desLocation[0] - start_location[0],
                Animation.ABSOLUTE, 0,
                Animation.ABSOLUTE, 0
        );
        //线性插值器 默认就是线性
        translationX.setInterpolator(new LinearInterpolator());

        Animation translationY = new TranslateAnimation(
                Animation.ABSOLUTE, 0,
                Animation.ABSOLUTE, 0,
                Animation.ABSOLUTE, 0,
                Animation.ABSOLUTE, desLocation[1] - start_location[1]);

        //设置加速插值器
        translationY.setInterpolator(new AccelerateInterpolator());

     //   Animation scale = new ScaleAnimation(0.5f, 0.5f, 0.5f, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

        Animation alpha = new AlphaAnimation(1, 0.5f);
        set.addAnimation(translationX);
        set.addAnimation(translationY);
        set.addAnimation(alpha);
     //   set.addAnimation(scale);

        set.setDuration(500);
        view.startAnimation(set);

        set.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            public void onAnimationEnd(final Animation animation) {
               hanlder.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        view.setVisibility(View.GONE);
                        vg.removeView(view);  //结束后,父View必须将 添加进来的ImageView移除,因为它本来就不属于父View
                    }
                }, 100);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });


    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值