栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

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

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

1、首先展示下效果图

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

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

@Override
  public void setAnim(View view) {
    // TODO Auto-generated method stub
    int[] start_location = new int[2];// 一个整型数组用来存储按钮在屏幕的X,Y坐标
    view.getLocationInWindow(start_location);// 购买按钮在屏幕中的坐标
    buyImg = new ImageView(this);// 动画的小圆圈
    buyImg.setImageResource(R.drawable.sign);// 设置buyImg的图片
    setAnim(buyImg, start_location);
  }
 
  
  private ViewGroup createAnimLayout() {
    ViewGroup rootView = (ViewGroup) this.getWindow().getDecorView();// 获得Window界面的最顶层
    LinearLayout animLayout = new LinearLayout(this);
    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);
    return animLayout;
  }
 
  
  private View addViewToAnimLayout(final ViewGroup vp, 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;
  }
 
  
  private void setAnim(final View v, int[] start_location) {
    anim_mask_layout = null;
    anim_mask_layout = createAnimLayout();
    anim_mask_layout.addView(v);
    View view = 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 endY = end_location[1] - start_location[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);// 动画重复次数
    translateAnimationY.setFillAfter(true);
 
    AnimationSet set = new AnimationSet(false);
    set.setFillAfter(false);
    set.addAnimation(translateAnimationX);
    set.addAnimation(translateAnimationY);
    set.setDuration(1000);
    view.startAnimation(set);
    set.setAnimationListener(new Animation.AnimationListener() {
 
      @Override
      public void onAnimationStart(Animation animation) {
 // TODO Auto-generated method stub
 v.setVisibility(View.VISIBLE);
      }
 
      @Override
      public void onAnimationRepeat(Animation animation) {
 // TODO Auto-generated method stub
 
      }
 
      @Override
      public void onAnimationEnd(Animation animation) {
 // TODO Auto-generated method stub
 v.setVisibility(View.GONE);
      }
    });
    ObjectAnimator anim = ObjectAnimator//
 .ofFloat(view, "scale", 1.0F, 1.5F, 1.0f)//
 .setDuration(500);//
    anim.setStartDelay(1000);
    anim.start();
    anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
      @Override
      public void onAnimationUpdate(ValueAnimator animation) {
 float cVal = (Float) animation.getAnimatedValue();
 image_chart.setScaleX(cVal);
 image_chart.setScaleY(cVal);
 text_chart_num.setScaleX(cVal);
 text_chart_num.setScaleY(cVal);
      }
    });
  }

4、GitHub地址:点击打开链接

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/155943.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号