本文介紹了Android實(shí)現(xiàn)購物車頁面及購物車效果(點(diǎn)擊動(dòng)畫),分享給大家,具體如下:
效果圖如下:

思路:
(1)思考每個(gè)條目中的數(shù)字的更新原理。
(2)購物車的動(dòng)畫效果。
(3)購物清單怎么顯示(這個(gè)我暫時(shí)沒有寫,如果需要的話,可以在我的簡(jiǎn)書下給我留言)。
1.因?yàn)檫M(jìn)入頁面,所有的商品個(gè)數(shù)都顯示為零,所以我用 ArrayList<HashMap<String, Object>> data,把商品集合都附上零:
//下面把data都添加0,為了剛開始顯示時(shí),顯示的是0 for (int i = 0; i < list.size(); i++) { HashMap<String, Object> myhashmap = new HashMap<String, Object>(); myhashmap.put("number", "" + 0); data.add(myhashmap); }然后把data傳入Adapter:
adapter = new MyAdapter(data);
當(dāng)我們對(duì)商品進(jìn)行增減時(shí),我們可以通過hashmap來更改,如下是增加商品的部分代碼:
b = Integer.parseInt((String) data.get(position).get( "number")); data.get(position).put("number", "" + (b + 1));2.購物車動(dòng)畫效果:
首先獲取點(diǎn)擊時(shí)的XY坐標(biāo),并且設(shè)置動(dòng)畫圖片:
// ball是個(gè)imageview startLocation = new int[2];// 一個(gè)整型數(shù)組,用來存儲(chǔ)按鈕的在屏幕的X、Y坐標(biāo) view.getLocationInWindow(startLocation);// 這是獲取購買按鈕的在屏幕的X、Y坐標(biāo)(這也是動(dòng)畫開始的坐標(biāo)) ball = new ImageView(MainActivity.this); ball.setImageResource(R.mipmap.sign);// 設(shè)置動(dòng)畫的圖片我的是一個(gè)小球(R.mipmap.sign)
然后是開始執(zhí)行動(dòng)畫:
private void setAnim(final View v, int[] startLocation) { anim_mask_layout = null; anim_mask_layout = createAnimLayout(); //創(chuàng)建動(dòng)畫層 anim_mask_layout.addView(v);//把動(dòng)畫小球添加到動(dòng)畫層 final View view = addViewToAnimLayout(anim_mask_layout, v, startLocation); int[] endLocation = new int[2];// 存儲(chǔ)動(dòng)畫結(jié)束位置的X、Y坐標(biāo) re_zhongcai_tanchu.getLocationInWindow(endLocation);// re_zhongcai_tanchu是那個(gè)拋物線最后掉落的控件 // 計(jì)算位移 int endX = 0 - startLocation[0] + 40;// 動(dòng)畫位移的X坐標(biāo) int endY = endLocation[1] - startLocation[1];// 動(dòng)畫位移的y坐標(biāo) TranslateAnimation translateAnimationX = new TranslateAnimation(0, endX, 0, 0); translateAnimationX.setInterpolator(new LinearInterpolator()); translateAnimationX.setRepeatCount(0);// 動(dòng)畫重復(fù)執(zhí)行的次數(shù) translateAnimationX.setFillAfter(true); TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0, 0, endY); translateAnimationY.setInterpolator(new AccelerateInterpolator()); translateAnimationY.setRepeatCount(0);// 動(dòng)畫重復(fù)執(zhí)行的次數(shù) translateAnimationX.setFillAfter(true); final AnimationSet set = new AnimationSet(false); set.setFillAfter(false); set.addAnimation(translateAnimationY); set.addAnimation(translateAnimationX); set.setDuration(800);// 動(dòng)畫的執(zhí)行時(shí)間 view.startAnimation(set); // 動(dòng)畫監(jiān)聽事件 set.setAnimationListener(new Animation.AnimationListener() { // 動(dòng)畫的開始 @Override public void onAnimationStart(Animation animation) { v.setVisibility(View.VISIBLE); // Log.e("動(dòng)畫","asdasdasdasd"); } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } // 動(dòng)畫的結(jié)束 @Override public void onAnimationEnd(Animation animation) { v.setVisibility(View.GONE); set.cancel(); animation.cancel(); } }); }需要注意的是,當(dāng)動(dòng)畫結(jié)束必須關(guān)閉動(dòng)畫:
v.setVisibility(View.GONE); set.cancel(); animation.cancel();
購物車的彈出清單功能,我沒有寫,需要的話,可以去我的簡(jiǎn)書留言.
github地址:https://github.com/javaexception/ShoppingCart
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選