添加购物车动画实现

http://www.oschina.net/code/snippet_203635_27426

以前写的demo,最近比较清闲,把它贴出来
个人比较排斥第三方的类库,所以基本属于喜欢用最基本的代码来写,觉得作为一个
程序猿追求代码的绝对控制才是最好的
标签: <无>

代码片段(5)[全屏查看所有代码]

1. [图片] 5218449C-AECE-4E5C-A23D-E94244C4B0BD.png    

2. [代码]商品列表Adapter     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package com.example.addshopcart;
 
 
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
/**
  * 商品列表Adapter
  * @author antkingwei
  *
  */
public class GoodAdapter extends BaseAdapter{
     private Context mContext;
     private LayoutInflater layoutInflater;
     private HolderClickListener mHolderClickListener;
     final class ViewHolder {
         ImageView imgview;
         Button button;
     }
     public GoodAdapter(Context context){
         this .mContext = context;
         layoutInflater = LayoutInflater.from(mContext);
     }
 
     
     @Override
     public int getCount() {
         // TODO Auto-generated method stub
         return 16 ;
     }
 
     @Override
     public Object getItem( int position) {
         // TODO Auto-generated method stub
         return null ;
     }
 
     @Override
     public long getItemId( int position) {
         // TODO Auto-generated method stub
         return position;
     }
 
     @Override
     public View getView( int position, View convertView, ViewGroup parent) {
         // TODO Auto-generated method stub
         final int selectedId = position;
         final ViewHolder viewHolder;
         if (convertView == null ){
             viewHolder = new ViewHolder();
             convertView = layoutInflater.inflate(R.layout.adapter_listview, null );
             viewHolder.imgview = (ImageView)convertView.findViewById(R.id.item_img);
             viewHolder.button = (Button)convertView.findViewById(R.id.item_button);
             convertView.setTag(viewHolder);
         } else {
             viewHolder =(ViewHolder)convertView.getTag();
         }
         viewHolder.button.setOnClickListener( new View.OnClickListener() {
             
             @Override
             public void onClick(View v) {
                 // TODO Auto-generated method stub
                 if (mHolderClickListener!= null ){
                     int [] start_location = new int [ 2 ];
                     viewHolder.imgview.getLocationInWindow(start_location); //获取点击商品图片的位置
                     Drawable drawable = viewHolder.imgview.getDrawable(); //复制一个新的商品图标
                     mHolderClickListener.onHolderClick(drawable,start_location);
                 }
             }
         });
         return convertView;
     }
     public void SetOnSetHolderClickListener(HolderClickListener holderClickListener){
         this .mHolderClickListener = holderClickListener;
     }
     public interface HolderClickListener{
         public void onHolderClick(Drawable drawable, int [] start_location);
     }
 
}

3. [代码]活动类     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package com.example.addshopcart;
import com.example.addshopcart.GoodAdapter.HolderClickListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
/**
  *
  * @author antkingwei
  *
  */
public class MainActivity extends Activity {
     private ListView listView;
     private Button cart_btn;
     private GoodAdapter goodAdapter;
     //动画时间
     private int AnimationDuration = 1000 ;
     //正在执行的动画数量
     private int number = 0 ;
     //是否完成清理
     private boolean isClean = false ;
     private FrameLayout animation_viewGroup;
     private Handler myHandler = new Handler(){
       public void handleMessage(Message msg){
           switch (msg.what){
           case 0 :
               //用来清除动画后留下的垃圾
               try {
                   animation_viewGroup.removeAllViews();
                   } catch (Exception e){
                       
                   }
                         
                   isClean = false ;
                   
               break ;
            default :
                   break ;
           }
       }
     };
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);
         listView = (ListView) this .findViewById(R.id.listview);
         cart_btn = (Button) this .findViewById(R.id.button);
         animation_viewGroup = createAnimLayout();
         goodAdapter = new GoodAdapter( this );
         goodAdapter.SetOnSetHolderClickListener( new HolderClickListener(){
 
             @Override
             public void onHolderClick(Drawable drawable, int [] start_location) {
                 // TODO Auto-generated method stub
                  doAnim(drawable,start_location);
                       
                 
             }
             
         });
         listView.setAdapter(goodAdapter);
     }
     
     private void doAnim(Drawable drawable, int [] start_location){
         if (!isClean){
             setAnim(drawable,start_location);
         } else {
             try {
               animation_viewGroup.removeAllViews();
               isClean = false ;
               setAnim(drawable,start_location);
             } catch (Exception e){
                 e.printStackTrace();
             }
             finally {
                 isClean = true ;
             }
         }
     }
     /**
      * @Description: 创建动画层
      * @param
      * @return void
      * @throws
      */
     private FrameLayout createAnimLayout(){
         ViewGroup rootView = (ViewGroup) this .getWindow().getDecorView();
         FrameLayout animLayout = new FrameLayout( this );
         FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.MATCH_PARENT);
         animLayout.setLayoutParams(lp);
         animLayout.setBackgroundResource(android.R.color.transparent);
         rootView.addView(animLayout);
         return animLayout;
         
     }
 
     /**
      * @deprecated 将要执行动画的view 添加到动画层
      * @param vg
      *        动画运行的层 这里是frameLayout
      * @param view
      *        要运行动画的View
      * @param location
      *        动画的起始位置
      * @return
      */
     private View addViewToAnimLayout(ViewGroup vg,View view, int [] location){
         int x = location[ 0 ];
         int y = location[ 1 ];
         vg.addView(view);
         FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
                 dip2px( this , 90 ),dip2px( this , 90 ));
         lp.leftMargin = x;
         lp.topMargin = y;
         view.setPadding( 5 , 5 , 5 , 5 );
         view.setLayoutParams(lp);
         
         return view;
     }
     /**
      * dip,dp转化成px 用来处理不同分辨路的屏幕
      * @param context
      * @param dpValue
      * @return
      */
     private int dip2px(Context context, float dpValue){
         float scale = context.getResources().getDisplayMetrics().density;
         return ( int )(dpValue*scale + 0 .5f);
     }
     
    /**
     * 动画效果设置
     * @param drawable
     *       将要加入购物车的商品
     * @param start_location
     *        起始位置
     */
    private void setAnim(Drawable drawable, int [] start_location){
       
       
        Animation mScaleAnimation = new ScaleAnimation( 1 .5f, 0 .0f, 1 .5f, 0 .0f,Animation.RELATIVE_TO_SELF, 0 .1f,Animation.RELATIVE_TO_SELF, 0 .1f);
        mScaleAnimation.setDuration(AnimationDuration);
        mScaleAnimation.setFillAfter( true );
        
 
        final ImageView iview = new ImageView( this );
        iview.setImageDrawable(drawable);
        final View view = addViewToAnimLayout(animation_viewGroup,iview,start_location);
        view.setAlpha( 0 .6f);
        
        int [] end_location = new int [ 2 ];
        cart_btn.getLocationInWindow(end_location);
        int endX = end_location[ 0 ];
        int endY = end_location[ 1 ]-start_location[ 1 ];
        
        Animation mTranslateAnimation = new TranslateAnimation( 0 ,endX, 0 ,endY);
        Animation mRotateAnimation = new RotateAnimation( 0 , 180 , Animation.RELATIVE_TO_SELF, 0 .5f, Animation.RELATIVE_TO_SELF, 0 .5f);
        mRotateAnimation.setDuration(AnimationDuration);
        mTranslateAnimation.setDuration(AnimationDuration);
        AnimationSet mAnimationSet = new AnimationSet( true );
 
        mAnimationSet.setFillAfter( true );
        mAnimationSet.addAnimation(mRotateAnimation);
        mAnimationSet.addAnimation(mScaleAnimation);
        mAnimationSet.addAnimation(mTranslateAnimation);
        
        mAnimationSet.setAnimationListener( new AnimationListener(){
         
         @Override
         public void onAnimationStart(Animation animation) {
             // TODO Auto-generated method stub
             number++;
         }
 
         @Override
         public void onAnimationEnd(Animation animation) {
             // TODO Auto-generated method stub
           
             number--;
             if (number== 0 ){
                 isClean = true ;
                 myHandler.sendEmptyMessage( 0 );
             }
             
         }
 
         @Override
         public void onAnimationRepeat(Animation animation) {
             // TODO Auto-generated method stub
             
         }
            
        });
        view.startAnimation(mAnimationSet);
       
    }
    /**
     * 内存过低时及时处理动画产生的未处理冗余
     */
     @Override
    public void onLowMemory() {
     // TODO Auto-generated method stub
         isClean = true ;
         try {
             animation_viewGroup.removeAllViews();
         } catch (Exception e){
             e.printStackTrace();
         }
         isClean = false ;
      super .onLowMemory();
    }
     
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
         // Inflate the menu; this adds items to the action bar if it is present.
         getMenuInflater().inflate(R.menu.main, menu);
         return true ;
     }
 
}

4. [代码]活动界面布局     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
< RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
     xmlns:tools = "http://schemas.android.com/tools"
     android:layout_width = "match_parent"
     android:layout_height = "match_parent"
     android:paddingBottom = "@dimen/activity_vertical_margin"
     android:paddingLeft = "@dimen/activity_horizontal_margin"
     android:paddingRight = "@dimen/activity_horizontal_margin"
     android:paddingTop = "@dimen/activity_vertical_margin"
     tools:context = ".MainActivity" >
 
     < ListView
         android:layout_width = "match_parent"
         android:layout_height = "match_parent"
         android:id = "@+id/listview"
         ></ ListView >
     < Button
         android:layout_alignBottom = "@+id/listview"
         android:layout_centerHorizontal = "true"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:id = "@+id/button"
         android:text = "购物车"
         />
 
</ RelativeLayout >

5. [代码]AdapterItem布局     

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<? xml version = "1.0" encoding = "utf-8" ?>
< RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
     android:layout_width = "match_parent"
     android:layout_height = "match_parent"
      >
     < ImageView
         android:id = "@+id/item_img"
         android:layout_width = "wrap_content"
         android:layout_height = "wrap_content"
         android:src = "@drawable/ic_launcher"
         android:layout_alignParentLeft = "true"
         />
     < Button
        android:id = "@+id/item_button"
        android:layout_width = "wrap_content"
        android:layout_height = "wrap_content"
        android:layout_alignParentRight = "true"
        android:text = "添加"
        >
     </ Button >
 
</ RelativeLayout >

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
js加入购物车抛物线动画购物车效果特效,亲测可用, 当您在电商购物网站浏览中意的商品时,您可以点击页面中的“加入购物车”按钮即可将商品加入的购物车中。本文介绍借助一款基于jQuery的动画插件,点击加入购物车按钮时,实现商品将飞入到右侧的购物车中的效果。 HTML 首先载入jQuery库文件和jquery.fly.min.js插件。 复制代码 代码如下: 接着,将商品信息html结构布置好,本例中,我们用四个商品并排布置,每个商品box中包括有商品图片、价格、名称以及加入购物车按钮等信息。 复制代码 代码如下: ¥3499.00 LG 49LF5400-CA 49寸IPS硬屏富贵招财铜钱设计 加入购物车 ¥3799.00 Hisense/海信 LED50T1A 海信电视官方旗舰店 加入购物车 ¥¥3999.00 Skyworth/创维 50E8EUS 8核4Kj极清酷开系统智能液晶电视 加入购物车 ¥6969.00 乐视TV Letv X60S 4核1080P高清3D安卓智能超级电视 加入购物车 然后,我们还需要在页面的右侧加上购物车以及提示信息。 复制代码 代码如下: 购物车 已成功加入购物车! CSS 我们使用CSS先将商品排列美化,然后设置右侧购物车样式,具体请看代码: 复制代码 代码如下: .box{float:left; width:198px; height:320px; margin-left:5px; border:1px solid #e0e0e0; text-align:center} .box p{line-height:20px; padding:4px 4px 10px 4px; text-align:left} .box:hover{border:1px solid #f90} .box h4{line-height:32px; font-size:14px; color:#f30;font-weight:500} .box h4 span{font-size:20px} .u-flyer{display: block;width: 50px;height: 50px;border-radius: 50px;position: fixed;z-index: 9999;} .m-sidebar{position: fixed;top: 0;right: 0;background: #000;z-index: 2000;width: 35px;height: 100%;font-size: 12px;color: #fff;} .cart{color: #fff;t

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值