Android使用动画动态添加商品进购物车

  

本文实例为大家分享了Android添加商品进购物车的具体代码,供大家参考,具体内容如下

  

1,首先展示下效果图

  

 Android使用动画动态添加商品进购物车

  

2,讲一下思路,小球由加号位置运动到购物车位置,首先得获得这两个点在整个屏幕中的坐标,然后分别计算这两个点的横纵坐标的差值,再通过TranslateAnimation这个类设置小球在X, Y方向上的偏移量,最后通过AnimationSet这个类将这两个动画放在一起执行。这是小球运动的动画,还有就是购物车变大缩小的动画。这个动画通过ObjectAnimator的ofFloat的方法设置缩放,要注意的是当小球落下的时候,购物车才开始动画,所以要设置一下setStartDelay这个方法。

  

3,具体的代码我就贴一下动画部分的代码,如果想要这个演示看下我最后贴出的Github的地址

        @Override   公共空间setAnim(查看视图){//TODO自动生成方法存根   int [] start_location=new int[2];//一个整型数组用来存储按钮在屏幕的X, Y坐标   view.getLocationInWindow (start_location);//购买按钮在屏幕中的坐标   buyImg=new ImageView(这);//动画的小圆圈   buyImg.setImageResource (R.drawable.sign);//设置buyImg的图片   setAnim (buyImg start_location);   }/* *   * hdh:创建动画层   *   * @return   */私人ViewGroup createAnimLayout () {   ViewGroup rootView=(ViewGroup) this.getWindow () .getDecorView();//获得窗口界面的最顶层   LinearLayout animLayout LinearLayout=new ();   LinearLayout。LayoutParams lp=new LinearLayout.LayoutParams (LinearLayout.LayoutParams。MATCH_PARENT LinearLayout.LayoutParams.MATCH_PARENT);   animLayout.setLayoutParams (lp);//animLayout.setId ();   animLayout.setBackgroundResource (android.R.color.transparent);   rootView.addView (animLayout);   返回animLayout;   }/* *   hdh: *   *   * @param副总裁   * @param视图   * @param位置   * @return   */私有视图addViewToAnimLayout(最终ViewGroup vp,最终的观点看来,int[]位置){   int x=位置[0];   int y=位置[1];   LinearLayout。LayoutParams lp=new LinearLayout.LayoutParams (LinearLayout.LayoutParams。WRAP_CONTENT LinearLayout.LayoutParams.WRAP_CONTENT);   lp。leftMargin=x;   lp。页面顶栏=y;   view.setLayoutParams (lp);   返回视图;   }/* *   * hdh:动画   *   * @param v   * @param start_location   */私人空间setAnim(最终视图v, int [] start_location) {   anim_mask_layout=零;   anim_mask_layout=createAnimLayout ();   anim_mask_layout.addView (v);   视图视图=addViewToAnimLayout (anim_mask_layout v, start_location);   int [] end_location=new int[2];//存储动画结束位置的X, Y坐标   text_chart_num.getLocationInWindow (end_location);//将购物车的位置存储起来//计算位移   int endX=end_location [0] - start_location[0];//动画位移的X坐标   恩迪int=end_location [1]——start_location[1];//动画位移的y坐标   endX TranslateAnimation translateAnimationX=new TranslateAnimation (0, 0, 0);   translateAnimationX。setInterpolator(新LinearInterpolator());//设置此动画的加速曲线。默认为一个线性插值。   translateAnimationX.setRepeatCount(0);//动画重复的次数   translateAnimationX.setFillAfter(真正的);      TranslateAnimation translateAnimationY=new TranslateAnimation(0, 0, 0,恩迪);   translateAnimationY。setInterpolator(新AccelerateInterpolator ());   translateAnimationY.setRepeatCount(0);//动画重复次数   translateAnimationY.setFillAfter(真正的);      AnimationSet设置=new AnimationSet(假);   set.setFillAfter(假);   set.addAnimation (translateAnimationX);   set.addAnimation (translateAnimationY);   set.setDuration (1000);   view.startAnimation(组);   set.setAnimationListener(新Animation.AnimationListener () {      @Override   公共空间onAnimationStart(动画动画){//TODO自动生成方法存根   v.setVisibility (View.VISIBLE);   }      @Override   公共空间onAnimationRepeat(动画动画){//TODO自动生成方法存根      }      @Override   公共空间onAnimationEnd(动画动画){//TODO自动生成方法存根   v.setVisibility (View.GONE);   }   });   ObjectAnimator动画=ObjectAnimator//.ofFloat(看来,“规模”,1.0,1.5,1.0 f)//.setDuration (500);//anim.setStartDelay (1000);   anim.start ();   动画。addUpdateListener(新ValueAnimator.AnimatorUpdateListener () {   @Override   公共空间onAnimationUpdate (ValueAnimator动画){   浮动cVal=(浮动)animation.getAnimatedValue ();   image_chart.setScaleX (cVal);   image_chart.setScaleY (cVal);   text_chart_num.setScaleX (cVal);   text_chart_num.setScaleY (cVal);   }   });   }

Android使用动画动态添加商品进购物车