這篇文章將為大家詳細講解有關(guān)怎么在Android應用中添加一個添加物品動畫,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
成都創(chuàng)新互聯(lián)主要從事網(wǎng)站設(shè)計制作、成都網(wǎng)站制作、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務。立足成都服務蕪湖,10年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務專業(yè),歡迎來電咨詢建站服務:028-86922220開發(fā)環(huán)境:AndroidStudio2.1.2+gradle-2.10
涉及知識:1.沉浸式狀態(tài)欄,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 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); //透明導航欄 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(); } //購物車份數(shù)+總價格計算 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應用中添加一個添加物品動畫就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。