当前位置: 移动技术网 > IT编程>移动开发>Android > 详解Android实现购物车页面及购物车效果(点击动画)

详解Android实现购物车页面及购物车效果(点击动画)

2019年07月24日  | 移动技术网IT编程  | 我要评论

视网膜屏幕,快播人人,陈二狗电视剧下架

本文介绍了android实现购物车页面及购物车效果(点击动画),分享给大家,具体如下:

效果图如下:

思路:

(1)思考每个条目中的数字的更新原理。

(2)购物车的动画效果。

(3)购物清单怎么显示(这个我暂时没有写,如果需要的话,可以在我的简书下给我留言)。

1.因为进入页面,所有的商品个数都显示为零,所以我用 arraylist<hashmap<string, object>> data,把商品集合都附上零:

    //下面把data都添加0,为了刚开始显示时,显示的是0
    for (int i = 0; i < list.size(); i++) {
      hashmap<string, object> myhashmap = new hashmap<string, object>();
      myhashmap.put("number", "" + 0);
      data.add(myhashmap);
    }

然后把data传入adapter:

adapter = new myadapter(data);

当我们对商品进行增减时,我们可以通过hashmap来更改,如下是增加商品的部分代码:

  b = integer.parseint((string) data.get(position).get(
              "number"));
          data.get(position).put("number", "" + (b + 1));

2.购物车动画效果:

首先获取点击时的xy坐标,并且设置动画图片:

 // ball是个imageview
 startlocation = new int[2];// 一个整型数组,用来存储按钮的在屏幕的x、y坐标
          view.getlocationinwindow(startlocation);// 这是获取购买按钮的在屏幕的x、y坐标(这也是动画开始的坐标)
          ball = new imageview(mainactivity.this);
          ball.setimageresource(r.mipmap.sign);// 设置动画的图片我的是一个小球(r.mipmap.sign)

然后是开始执行动画:

   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坐标
    re_zhongcai_tanchu.getlocationinwindow(endlocation);// re_zhongcai_tanchu是那个抛物线最后掉落的控件

    // 计算位移
    int endx = 0 - startlocation[0] + 40;// 动画位移的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(800);// 动画的执行时间
    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();
      }
    });

  }

需要注意的是,当动画结束必须关闭动画:

  v.setvisibility(view.gone);
        set.cancel();
        animation.cancel();

购物车的弹出清单功能,我没有写,需要的话,可以去我的简书留言.

github地址:https://github.com/javaexception/shoppingcart

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网