欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-11-02 21:17:22
前言:当我们写商城类的项目的时候,一般都会有加入购物车的功能,加入购物车的时候会有一些抛物线动画,最近做到这个功能,借助别人的demo写了一个。 效果: 开发环境:...

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

效果:

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

开发环境: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 + "");
 }
}

源码下载

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!