Android仿饿了么购物车功能

布满荆棘的人生 2022-11-21 01:03 405阅读 0赞

现在商城应用上,购物车功能可以说是不可缺少的,每个应用的购物车模块实现方式都差不多,这篇我们来仿一下饿了么的购物车功能,用过的童鞋都知道,印象最深刻的应该是添加或减少数量时,抛物线的动画效果,话不多说,现在让我们来看看是怎么实现的吧.

先看效果图
在这里插入图片描述
效果图可以看出,这个布局还是比较简单的,重点应该还是在抛物线的动画上.

1、列表布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/container"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:background="#ffffff">
  7. <RelativeLayout
  8. android:id="@+id/rl_main"
  9. android:layout_width="match_parent"
  10. android:layout_height="48dp"
  11. android:background="#ffffff"
  12. android:orientation="vertical">
  13. <ImageView
  14. android:id="@+id/tv_titlebar_left"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:layout_centerVertical="true"
  18. android:layout_marginLeft="15dp"
  19. android:padding="5dp"
  20. android:src="@drawable/danghanglan_fanhui" />
  21. <TextView
  22. android:id="@+id/tv_titlebar_center"
  23. android:layout_width="200dp"
  24. android:layout_height="match_parent"
  25. android:layout_centerHorizontal="true"
  26. android:ellipsize="end"
  27. android:gravity="center"
  28. android:maxLength="18"
  29. android:singleLine="true"
  30. android:text="购物车"
  31. android:textColor="#2f302b"
  32. android:textSize="17sp"
  33. android:visibility="visible" />
  34. <TextView
  35. android:id="@+id/tv_titlebar_right"
  36. android:layout_width="wrap_content"
  37. android:layout_height="match_parent"
  38. android:layout_alignParentRight="true"
  39. android:background="@null"
  40. android:gravity="center"
  41. android:paddingLeft="15dp"
  42. android:paddingRight="15dp"
  43. android:singleLine="true"
  44. android:text="编辑"
  45. android:textColor="#2f302b"
  46. android:textSize="14sp"
  47. android:visibility="gone" />
  48. <View
  49. android:layout_width="match_parent"
  50. android:layout_height="0.5dp"
  51. android:layout_alignParentBottom="true"
  52. android:background="#cccccc" />
  53. </RelativeLayout>
  54. <androidx.recyclerview.widget.RecyclerView
  55. android:id="@+id/rv"
  56. android:layout_width="match_parent"
  57. android:layout_height="wrap_content"
  58. android:layout_below="@id/rl_main"
  59. android:layout_marginBottom="40dp"/>
  60. <LinearLayout
  61. android:layout_width="match_parent"
  62. android:layout_height="wrap_content"
  63. android:layout_alignParentBottom="true"
  64. android:background="#fff"
  65. android:gravity="center_vertical"
  66. android:orientation="horizontal">
  67. <FrameLayout
  68. android:layout_width="wrap_content"
  69. android:layout_height="wrap_content"
  70. android:layout_marginLeft="10dp">
  71. <ImageView
  72. android:id="@+id/iv_shop_cart"
  73. android:layout_width="40dp"
  74. android:layout_height="40dp"
  75. android:padding="2dp"
  76. android:src="@mipmap/img_revision_shopping_car" />
  77. <TextView
  78. android:id="@+id/tv_num"
  79. android:layout_width="20dp"
  80. android:layout_height="20dp"
  81. android:layout_gravity="right"
  82. android:background="@drawable/tip_message_revision_bg"
  83. android:gravity="center"
  84. android:text="11"
  85. android:textColor="#fff"
  86. android:visibility="gone" />
  87. </FrameLayout>
  88. <View
  89. android:layout_width="0dp"
  90. android:layout_height="1dp"
  91. android:layout_weight="1" />
  92. <TextView
  93. android:layout_width="80dp"
  94. android:layout_height="30dp"
  95. android:layout_marginRight="10dp"
  96. android:background="#d4ab7f"
  97. android:gravity="center"
  98. android:text="购买"
  99. android:textColor="#ffffff" />
  100. </LinearLayout>
  101. </RelativeLayout>

2、模拟初始化数据

  1. //添加商品数据
  2. final List<CartModel> list = new ArrayList<>();
  3. for (int i = 0; i < 20; i++) {
  4. CartModel cartModel = new CartModel();
  5. cartModel.setName("麻辣肉碎面-----" + i);
  6. list.add(cartModel);
  7. }

3、设置数据

  1. final ShoppingAdapter shoppingAdapter = new ShoppingAdapter(this, list);
  2. mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
  3. mRecyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
  4. mRecyclerView.setAdapter(shoppingAdapter);

4、点击加号操作这里分二钟情况:

一是当数量为0时减号会执行旋转和平移渐变的动画,
二是数量不为0时只会进行抛物线动画,其中抛物线动画实现思路就是得到加号和购物车的坐标,然后得到最外层容器添加一个view来执行这个动画,动画执行完成后移除这个动画:

  1. //点击加号
  2. myHolder.imageViewAdd.setOnClickListener(new View.OnClickListener() {
  3. @Override
  4. public void onClick(View v)
  5. final CartModel cartModel = mDatas.get(position);
  6. //如果该商品数量为0就进行这个动画
  7. if (cartModel.getCount() == 0) {
  8. myHolder.imageViewReduce.setVisibility(View.VISIBLE);
  9. myHolder.tvAmount.setVisibility(View.VISIBLE);
  10. AnimatorSet set = new AnimatorSet();
  11. //减号
  12. ObjectAnimator ta1 = ObjectAnimator.ofFloat(myHolder.imageViewReduce, "translationX", mAddLeft - mReduceLeft, 0);
  13. ObjectAnimator ra1 = ObjectAnimator.ofFloat(myHolder.imageViewReduce, "rotation", 0, 360);
  14. ObjectAnimator aa1 = ObjectAnimator.ofFloat(myHolder.imageViewReduce, "alpha", 0, 1);
  15. //数字
  16. ObjectAnimator ta2 = ObjectAnimator.ofFloat(myHolder.tvAmount, "translationX", mAddLeft - mAmountLeft, 0);
  17. ObjectAnimator ra2 = ObjectAnimator.ofFloat(myHolder.tvAmount, "rotation", 0, 360);
  18. ObjectAnimator aa2 = ObjectAnimator.ofFloat(myHolder.tvAmount, "alpha", 0, 1);
  19. set.play(ta1).with(ra1).with(ta2).with(ra2).with(aa1).with(aa2);
  20. set.setDuration(500).start();
  21. }
  22. //addGoods2CartAnim((ImageView) v,cartModel);
  23. //得到加号在屏幕的坐标
  24. int[] addLocation = new int[2];
  25. v.getLocationInWindow(addLocation);
  26. //得到购物车图标的坐标
  27. int[] cartLocation = mActivity.getCartLocation();
  28. //添加一个imageview
  29. final ImageView iv = new ImageView(v.getContext());
  30. iv.setBackgroundResource(R.drawable.icon_shop_add);
  31. RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(v.getWidth(), v.getHeight());
  32. lp.leftMargin = addLocation[0];
  33. lp.topMargin = addLocation[1] - v.getHeight();
  34. mActivity.getContainer().addView(iv, lp);
  35. //横向移动
  36. ObjectAnimator oaX = ObjectAnimator.ofFloat(iv, "translationX", cartLocation[0] - addLocation[0] + v.getWidth() / 2);
  37. //纵向
  38. ObjectAnimator oaY = ObjectAnimator.ofFloat(iv, "translationY", cartLocation[1] - addLocation[1]);
  39. oaX.setInterpolator(new LinearInterpolator());
  40. oaY.setInterpolator(new AccelerateInterpolator());
  41. AnimatorSet set = new AnimatorSet();
  42. set.play(oaX).with(oaY);
  43. set.setDuration(500).start();
  44. set.addListener(new Animator.AnimatorListener() {
  45. @Override
  46. public void onAnimationStart(Animator animation) {
  47. }
  48. @Override
  49. public void onAnimationEnd(Animator animation) {
  50. //移除这个view
  51. mActivity.getContainer().removeView(iv);
  52. //跟新购物车
  53. cartModel.setCount(cartModel.getCount() + 1);
  54. ((MyHolder) holder).tvAmount.setText(String.valueOf(cartModel.getCount()));
  55. mActivity.mCount++;
  56. mActivity.setMtvNum();
  57. }
  58. @Override
  59. public void onAnimationCancel(Animator animation) {
  60. }
  61. @Override
  62. public void onAnimationRepeat(Animator animation) {
  63. }
  64. });
  65. }
  66. });

5、减法动画效果

  1. //点击减号
  2. myHolder.imageViewReduce.setOnClickListener(new View.OnClickListener() {
  3. @Override
  4. public void onClick(View v) {
  5. final CartModel cartModel = mDatas.get(position);
  6. //如果该商品数量为1就进行这个动画
  7. if (cartModel.getCount() == 1) {
  8. AnimatorSet set = new AnimatorSet();
  9. //减号
  10. ObjectAnimator ta1 = ObjectAnimator.ofFloat(myHolder.imageViewReduce, "translationX", 0, mAddLeft - mReduceLeft);
  11. ObjectAnimator ra1 = ObjectAnimator.ofFloat(myHolder.imageViewReduce, "rotation", 0, 360);
  12. ObjectAnimator aa1 = ObjectAnimator.ofFloat(myHolder.imageViewReduce, "alpha", 1, 0);
  13. //数字
  14. ObjectAnimator ta2 = ObjectAnimator.ofFloat(myHolder.tvAmount, "translationX", 0, mAddLeft - mAmountLeft);
  15. ObjectAnimator ra2 = ObjectAnimator.ofFloat(myHolder.tvAmount, "rotation", 0, 360);
  16. ObjectAnimator aa2 = ObjectAnimator.ofFloat(myHolder.tvAmount, "alpha", 1, 0);
  17. set.play(ta1).with(ra1).with(ta2).with(ra2).with(aa1).with(aa2);
  18. set.setDuration(500).start();
  19. set.addListener(new Animator.AnimatorListener() {
  20. @Override
  21. public void onAnimationStart(Animator animation) {
  22. }
  23. @Override
  24. public void onAnimationEnd(Animator animation) {
  25. cartModel.setCount(cartModel.getCount() - 1);
  26. myHolder.tvAmount.setText(String.valueOf(cartModel.getCount()));
  27. if(cartModel.getCount()==0){
  28. myHolder.tvAmount.setVisibility(View.INVISIBLE);
  29. myHolder.imageViewReduce.setVisibility(View.INVISIBLE);
  30. }
  31. mActivity.mCount--;
  32. mActivity.setMtvNum();
  33. }
  34. @Override
  35. public void onAnimationCancel(Animator animation) {
  36. }
  37. @Override
  38. public void onAnimationRepeat(Animator animation) {
  39. }
  40. });
  41. } else {
  42. cartModel.setCount(cartModel.getCount() - 1);
  43. myHolder.tvAmount.setText(String.valueOf(cartModel.getCount()));
  44. mActivity.mCount--;
  45. mActivity.setMtvNum();
  46. }
  47. }
  48. });

该功能的实现重点在抛物线的动画效果上,这里就不把完整的代码添加进去了,不然影响阅读体验,需要完整源码的童鞋底部关注公众号回复:“仿饿了么购物车” 即可获得哦.

到这里就结束啦.


小编整理了一份Android电子书籍,需要的童鞋关注底部公众号(longxuanzhigu)回复:“e_books” 即可获取哦!
在这里插入图片描述

以下是公众号(longxuanzhigu),之后发布的文章会同步到该公众号,方便交流学习Android知识,欢迎关注:

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,405人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Android仿饿购物功能

    现在商城应用上,购物车功能可以说是不可缺少的,每个应用的购物车模块实现方式都差不多,这篇我们来仿一下饿了么的购物车功能,用过的童鞋都知道,印象最深刻的应该是添加或减少数量时,抛

    相关 Vue 仿淘宝购物

    效果图: ![这里写图片描述][70] ![这里写图片描述][70 1] 我们只是做演示,其中的数据应该从后台来取得,这里我们模拟几条数据来显示。 页面布局: