http://www.oschina.net/code/snippet_203635_27426
代码片段(5)[全屏查看所有代码]
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
>
|