前言:当我们写商城类的项目的时候,一般都会有加入购物车的功能,加入购物车的时候会有一些抛物线动画,最近做到这个功能,借助别人的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实现购物车添加物品的动画效果全部内容,希望文章能够帮你解决Android实现购物车添加物品的动画效果所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)