欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android实现购物车添加物品的动画效果

时间:2020-12-04 04:41:41|栏目:Android代码|点击:

前言:当我们写商城类的项目的时候,一般都会有加入购物车的功能,加入购物车的时候会有一些抛物线动画,最近做到这个功能,借助别人的demo写了一个。

效果:

开发环境:AndroidStudio2.1.2+gradle-2.10

涉及知识:1.沉浸式状态栏,2.单位精度计算(价格),3.List之Iterator。

部分代码:

public class MainActivity extends AppCompatActivity implements FoodAdapter.FoodActionCallback {
 private ListView listView;
 private TextView goods_all_pricetv;
 private TextView good_numtv;
 private List<FoodModel> list;
 private List<FoodModel> selectList = new ArrayList<>();
 private FoodAdapter adapter = null;
 @Override
 protected void onCreate(@Nullable final Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 //沉浸式状态栏
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
 //透明状态栏
 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
 //透明导航栏
 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
 }
 listView = (ListView) findViewById(R.id.listView);
 goods_all_pricetv = (TextView) findViewById(R.id.goods_all_pricetv);
 good_numtv = (TextView) findViewById(R.id.good_numtv);
 findViewById(R.id.goods_submit_tv).setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View v) {
 if (null != selectList && selectList.size() > 0) {
 Toast.makeText(MainActivity.this, "你一共加入购物车" + good_numtv.getText() +
 "份商品,总价格为:" + goods_all_pricetv.getText(), Toast.LENGTH_SHORT).show();
 } else {
 Toast.makeText(MainActivity.this, "你的购物车为空", Toast.LENGTH_SHORT).show();
 }

 }
 });
 initData();
 }
 private void initData() {
 list = AppConfig.factoryFoods();
 adapter = new FoodAdapter(this, list, this);
 listView.setAdapter(adapter);
 }
 //添加商品动画
 @Override
 public void addAction(View view, int item) {
 ShoppingCartAnimationView shoppingCartAnimationView = new ShoppingCartAnimationView(this);
 int position[] = new int[2];
 view.getLocationInWindow(position);
 shoppingCartAnimationView.setStartPosition(new Point(position[0], position[1]));
 ViewGroup rootView = (ViewGroup) this.getWindow().getDecorView();
 rootView.addView(shoppingCartAnimationView);
 int endPosition[] = new int[2];
 good_numtv.getLocationInWindow(endPosition);
 shoppingCartAnimationView.setEndPosition(new Point(endPosition[0], endPosition[1]));
 shoppingCartAnimationView.startBeizerAnimation();
 FoodModel model = list.get(item);
 model.setNum(model.getNum() + 1);
 adapter.notifyDataSetChanged();
 calculatePrice();
 }
 //减少商品
 @Override
 public void reduceGood(int position) {
 FoodModel model = list.get(position);
 model.setNum(model.getNum() - 1);
 adapter.notifyDataSetChanged();
 calculatePrice();
 }
 //购物车份数+总价格计算
 private void calculatePrice() {
 selectList.clear();
 double price = 0;
 int num = 0;
 Iterator<FoodModel> iterator = list.iterator();
 while (iterator.hasNext()) {
 FoodModel model = iterator.next();
 if (model.getNum() != 0) {
 selectList.add(model);
 price += model.getPrice() * model.getNum();
 num += model.getNum();
 }
 }
 goods_all_pricetv.setText("¥" + adapter.priceResult(price) + "元");
 good_numtv.setText(num + "");
 }
}

源码下载

上一篇:Android编程经典代码集锦(复制,粘贴,浏览器调用,Toast显示,自定义Dialog等)

栏    目:Android代码

下一篇:Android中ListView分页加载数据功能实现

本文标题:Android实现购物车添加物品的动画效果

本文地址:http://www.codeinn.net/misctech/29453.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有