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