這篇文章將為大家詳細(xì)講解有關(guān)怎么在Android應(yīng)用中添加一個(gè)添加物品動(dòng)畫(huà),文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
創(chuàng)新互聯(lián)是一家專注網(wǎng)站建設(shè)、網(wǎng)絡(luò)營(yíng)銷策劃、微信小程序開(kāi)發(fā)、電子商務(wù)建設(shè)、網(wǎng)絡(luò)推廣、移動(dòng)互聯(lián)開(kāi)發(fā)、研究、服務(wù)為一體的技術(shù)型公司。公司成立十多年以來(lái),已經(jīng)為數(shù)千家垃圾桶各業(yè)的企業(yè)公司提供互聯(lián)網(wǎng)服務(wù)?,F(xiàn)在,服務(wù)的數(shù)千家客戶與我們一路同行,見(jiàn)證我們的成長(zhǎng);未來(lái),我們一起分享成功的喜悅。
開(kāi)發(fā)環(huán)境:AndroidStudio2.1.2+gradle-2.10
涉及知識(shí):1.沉浸式狀態(tài)欄,2.單位精度計(jì)算(價(jià)格),3.List之Iterator。
部分代碼:
public class MainActivity extends AppCompatActivity implements FoodAdapter.FoodActionCallback { private ListView listView; private TextView goods_all_pricetv; private TextView good_numtv; private Listlist; private List selectList = new ArrayList<>(); private FoodAdapter adapter = null; @Override protected void onCreate(@Nullable final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //沉浸式狀態(tài)欄 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { //透明狀態(tài)欄 getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); //透明導(dǎo)航欄 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, "你一共加入購(gòu)物車" + good_numtv.getText() + "份商品,總價(jià)格為:" + goods_all_pricetv.getText(), Toast.LENGTH_SHORT).show(); } else { Toast.makeText(MainActivity.this, "你的購(gòu)物車為空", Toast.LENGTH_SHORT).show(); } } }); initData(); } private void initData() { list = AppConfig.factoryFoods(); adapter = new FoodAdapter(this, list, this); listView.setAdapter(adapter); } //添加商品動(dòng)畫(huà) @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(); } //購(gòu)物車份數(shù)+總價(jià)格計(jì)算 private void calculatePrice() { selectList.clear(); double price = 0; int num = 0; Iterator 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 + ""); } }
關(guān)于怎么在Android應(yīng)用中添加一個(gè)添加物品動(dòng)畫(huà)就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。