仿餓了么購物車下單效果

仿餓了么購物車下單效果

前一段由于新項目需要,開發(fā)一個類似餓了么購物車下單效果,電商類、外賣類、點餐類項目都可以用的上,廢話不多說請看效果。
效果圖如下:

購物車下單效果
購物車下單效果

主要的功能:
就是左側(cè)展示分類,右側(cè)展示分類下商品的,點擊右側(cè)分類下的商品,如果商品是套餐類型的話,點擊可以看套餐詳情,下單選擇完商品后,可以在購物車?yán)锩嫣砑踊驕p少商品數(shù)量。

主要功能實現(xiàn):
1:分類及商品及購物車?yán)锩嫔唐窋?shù)量的聯(lián)動效果
2:底部購物車商品列表
3:選擇左側(cè)分類效果
4:添加商品是有0到1,減少商品有1到0動畫效果
5:下單動畫

1:分類及商品及購物車?yán)锩嫔唐窋?shù)量的聯(lián)動效果

商品的分類和商品展示分別是兩個列表,每一個商品的數(shù)量取商品的number、商品分類是取每一個分類下的商品list里面遍歷商品取商品數(shù)量之和。然后商品列表的
GoodsAdapter里面有一個參數(shù)傳的是商品分類的CatograyAdapter,當(dāng)下單時減少或則添加商品時,都會調(diào) MainActivityhandlerCarNum方法。
減少時type傳0,添加時傳1。通過一個SparseArray,key是商品的product_id,value為商品的下單個數(shù)。1:當(dāng)添加商品都會傳入一個商品對象goodsBean,通過product_id取到這個商品對象,如果商品對象為空時,給傳入的對象下單數(shù)量goodsBean.setNum(1),否則goodsBean.setNum(++i)。2:當(dāng)減少商品時,通過product_id取到這個商品對象,通過對象取對象的下單數(shù)量如果小與2的時候goodsBean.setNum(0),并將該對象從SparseArray中remove否goodsBean.setNum(--i)。最后調(diào)update方法更新GoodsAdapter,CatograyAdapter.

    public void handlerCarNum(int type, GoodsBean goodsBean, boolean refreshGoodList){
        if (type == 0) {
            GoodsBean temp = selectedList.get(goodsBean.getProduct_id());
            if(temp!=null){
                if(temp.getNum()<2){
                    goodsBean.setNum(0);
                    selectedList.remove(goodsBean.getProduct_id());
                }else{
                    int i =  goodsBean.getNum();
                    goodsBean.setNum(--i);
                }
            }
        } else if (type == 1) {
            GoodsBean temp = selectedList.get(goodsBean.getProduct_id());
            if(temp==null){
                goodsBean.setNum(1);
                selectedList.append(goodsBean.getProduct_id(), goodsBean);
            }else{
                int i= goodsBean.getNum();
                goodsBean.setNum(++i);
            }
        }
        update(refreshGoodList);
    }
 //刷新布局 總價、購買數(shù)量等
    private void update(boolean refreshGoodList){
        int size = selectedList.size();
        int count =0;
        for(int i=0;i<size;i++){
            GoodsBean item = selectedList.valueAt(i);
            count += item.getNum();
            totleMoney += item.getNum()*Double.parseDouble(item.getPrice());
        }
        tv_totle_money.setText("¥"+String.valueOf(df.format(totleMoney)));
        totleMoney = 0.00;
        if(count<1){
            bv_unm.setVisibility(View.GONE);
        }else{
            bv_unm.setVisibility(View.VISIBLE);
        }

        bv_unm.setText(String.valueOf(count));

        if(productAdapter!=null){
            productAdapter.notifyDataSetChanged();
        }

        if(goodsAdapter!=null){
            goodsAdapter.notifyDataSetChanged();
        }

        if(catograyAdapter!=null){
            catograyAdapter.notifyDataSetChanged();
        }

        if(bottomSheetLayout.isSheetShowing() && selectedList.size()<1){
            bottomSheetLayout.dismissSheet();
        }
    }

2:底部購物車商品列表

點擊購物車查看購物車列表時,就是給含有下單商品的HashMap作為參數(shù)傳入ProductAdapter中,在購物車中添加或減少商品數(shù)量同樣是調(diào) MainActivityhandlerCarNum方法。
除此之外,購物車列表彈起的效果可以用一個bottomsheet效果。as直接集成
compile'com.flipboard:bottomsheet-core:1.5.1'即可。

3:選擇左側(cè)分類效果

由于選擇分類時,既要改變分類的背景色也要改變選擇分類文字的顏色,就不能直接在xml里面設(shè)置了,需要在getview里面根據(jù)外面?zhèn)鬟M(jìn)來選擇的selection與position比較后設(shè)置。

 public void setSelection(int selection) {
        this.selection = selection;
    }
 if (position == selection) {
            viewholder.tv_catogray.setBackgroundResource(R.drawable.rec_red_left_stroke);
            viewholder.tv_catogray.setTextColor(context.getResources().getColor(R.color.black));
        } else {
            viewholder.tv_catogray.setBackgroundResource(R.drawable.empty);
            viewholder.tv_catogray.setTextColor(context.getResources().getColor(R.color.gray));
        }

4:添加商品是有0到1,減少商品有1到0動畫效果

其實動畫效果做起來也不是特別復(fù)雜,首先要搞清android的幾種動畫效果,分為在Android3.0(即API Level11)以前,Android僅支持2種動畫:分別是Frame Animation(逐幀動畫)和Tween Animation(補(bǔ)間動畫),在3.0之后Android支持了一種新的動畫系統(tǒng),稱為:Property Animation(屬性動畫)。
最常用的補(bǔ)間動畫最常用的4中效果如下:
漸變透明度動畫效果
漸變尺寸伸縮動畫效果
畫面轉(zhuǎn)換位置移動動畫效果
畫面轉(zhuǎn)移旋轉(zhuǎn)動畫效果

添加商品是有0到1,減少商品有1到0動畫效果其實就是利用了translate、rotate水平位移和旋轉(zhuǎn)動畫效果。

 //顯示減號的動畫
    private Animation getShowAnimation(){
        AnimationSet set = new AnimationSet(true);
        RotateAnimation rotate = new RotateAnimation(0,720,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f);
        set.addAnimation(rotate);
        TranslateAnimation translate = new TranslateAnimation(
                TranslateAnimation.RELATIVE_TO_SELF,2f
                ,TranslateAnimation.RELATIVE_TO_SELF,0
                ,TranslateAnimation.RELATIVE_TO_SELF,0
                ,TranslateAnimation.RELATIVE_TO_SELF,0);
        set.addAnimation(translate);
        AlphaAnimation alpha = new AlphaAnimation(0,1);
        set.addAnimation(alpha);
        set.setDuration(500);
        return set;
    }
    //隱藏減號的動畫
    private Animation getHiddenAnimation(){
        AnimationSet set = new AnimationSet(true);
        RotateAnimation rotate = new RotateAnimation(0,720,RotateAnimation.RELATIVE_TO_SELF,0.5f,RotateAnimation.RELATIVE_TO_SELF,0.5f);
        set.addAnimation(rotate);
        TranslateAnimation translate = new TranslateAnimation(
                TranslateAnimation.RELATIVE_TO_SELF,0
                ,TranslateAnimation.RELATIVE_TO_SELF,2f
                ,TranslateAnimation.RELATIVE_TO_SELF,0
                ,TranslateAnimation.RELATIVE_TO_SELF,0);
        set.addAnimation(translate);
        AlphaAnimation alpha = new AlphaAnimation(1,0);
        set.addAnimation(alpha);
        set.setDuration(500);
        return set;
    }

5:下單動畫

下單動畫首先要獲取添加商品時的初始位置坐標(biāo),通過getLocationInWindow方法獲取初始位置的心x,y坐標(biāo),然后獲取添加商品要消失的地方的坐標(biāo),一般都選擇購物車logo的坐標(biāo)。然后計算x,y的位移值后,通過TranslateAnimation轉(zhuǎn)換移動效果,x,y同時移動就實現(xiàn)了拋物線的效果。最后不要忘記設(shè)置當(dāng)動畫結(jié)束的時候隱藏拋過來的小圖標(biāo)。

public void setAnim(final View v, int[] startLocation) {
        anim_mask_layout = null;
        anim_mask_layout = createAnimLayout();
        anim_mask_layout.addView(v);//把動畫小球添加到動畫層
        final View view = addViewToAnimLayout(anim_mask_layout, v, startLocation);
        int[] endLocation = new int[2];// 存儲動畫結(jié)束位置的X、Y坐標(biāo)
        tv_car.getLocationInWindow(endLocation);
        // 計算位移
        int endX = 0 - startLocation[0] + 40;// 動畫位移的X坐標(biāo)
        int endY = endLocation[1] - startLocation[1];// 動畫位移的y坐標(biāo)

        TranslateAnimation translateAnimationX = new TranslateAnimation(0,endX, 0, 0);
        translateAnimationX.setInterpolator(new LinearInterpolator());
        translateAnimationX.setRepeatCount(0);// 動畫重復(fù)執(zhí)行的次數(shù)
        translateAnimationX.setFillAfter(true);

        TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0, 0, endY);
        translateAnimationY.setInterpolator(new AccelerateInterpolator());
        translateAnimationY.setRepeatCount(0);// 動畫重復(fù)執(zhí)行的次數(shù)
        translateAnimationY.setFillAfter(true);

        AnimationSet set = new AnimationSet(false);
        set.setFillAfter(false);
        set.addAnimation(translateAnimationY);
        set.addAnimation(translateAnimationX);
        set.setDuration(800);// 動畫的執(zhí)行時間
        view.startAnimation(set);
        // 動畫監(jiān)聽事件
        set.setAnimationListener(new Animation.AnimationListener() {
            // 動畫的開始
            @Override
            public void onAnimationStart(Animation animation) {
                v.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                // TODO Auto-generated method stub
            }

            // 動畫的結(jié)束
            @Override
            public void onAnimationEnd(Animation animation) {
                v.setVisibility(View.GONE);
            }
        });

    }

最后肯定少不了源碼下載地址:shopcar 感覺需要的朋友歡迎star,fork,有問題可以提issue。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容