文章目录


主界面

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
<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">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/product_list"
android:cacheColorHint="# 00000000"
/>
<RelativeLayout
android:id="@+id/shopping_cart"
android:layout_width="wrap_content"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_marginLeft="20dp"
android:paddingBottom="22dp"
android:gravity="center_vertical">
<ImageView
android:id="@+id/shopping_img_cart"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="1dp"
android:layout_alignParentLeft="true"
android:scaleType="centerInside"
android:src="@drawable/shopping_cart"/>
</RelativeLayour>
</RelativeLayout>

item项

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
<RlativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp">
<Button
android:id="@+id/product_buy_btn"
android:layout_width="40dp"
android:layout_height="48dp"
android:layout_alignParentRight="true"
android:layout_gravity="center_vertical"
android:layout_margin="10dp"
android:text="购买"
android:textColor="# F8720D"
android:textSize="12sp" />
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="48dp"
android:paddingLeft="10dp"
android:text="商品"
android:textSize="18sp"
android:gravity="center_vertical"
android:layout_toLeftOf="@id/product_buy_btn"
/>
</RlativeLayout>

代码

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
public class MainActivity extends Activity {

private ListView mListView;
private ImageView shopCart;
private ImageView buyImg;
private ViewGroup anim_mask_layout;//动画层

private Context mContext;
private ProductAdapter productAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
initView();
}

private void initView() {
shopCart = (ImageView) findViewById(R.id.shopping_img_cart);
mListView = (ListView) findViewById(R.id.product_list);
productAdapter = new ProductAdapter();
mListView.setAdapter(productAdapter);
}

private class ProductAdapter extends BaseAdapter {

public ProductAdapter() {
}

@Override
public int getCount() {
return 20;
}

@Override
public String getItem(int position) {
return "商品" + (position + 1);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView,
final ViewGroup parent) {
String name = getItem(position);
ViewHolder holder = null;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(
R.layout.item, null);
holder = new ViewHolder();
holder.nameTxt = (TextView) convertView.findViewById(R.id.name);
holder.buyBtn = (Button) convertView
.findViewById(R.id.product_buy_btn);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.nameTxt.setText(name);
holder.buyBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
int[] start_location = new int[2];
v.getLocationInWindow(start_location);
buyImg = new ImageView(mContext);
buyImg.setImageResource(R.drawable.sign);
setAnim(buyImg, start_location);
}
});
return convertView;
}

class ViewHolder {
TextView nameTxt;
Button buyBtn;
}

}

private ViewGroup createAnimLayout() {
ViewGroup rootView = (ViewGroup) this.getWindow().getDecorView();
LinearLayout animLayout = new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
animLayout.setLayoutParams(lp);
animLayout.setId(Integer.MAX_VALUE);
animLayout.setBackgroundResource(android.R.color.transparent);
rootView.addView(animLayout);
return animLayout;
}

private View addViewToAnimLayout(final ViewGroup vg, final View view,
int[] location) {
int x = location[0];
int y = location[1];
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
lp.leftMargin = x;
lp.topMargin = y;
view.setLayoutParams(lp);
return view;
}

private void setAnim(final View v, int[] start_location) {
anim_mask_layout = null;
anim_mask_layout = createAnimLayout();
anim_mask_layout.addView(v);//把动画小球添加到动画层
final View view = addViewToAnimLayout(anim_mask_layout, v,
start_location);
int[] end_location = new int[2];// 这是用来存储动画结束位置的X、Y坐标
shopCart.getLocationInWindow(end_location);// shopCart是那个购物车



// 计算位移
int endX = 0 - start_location[0] + 40;
int endY = end_location[1] - start_location[1];
Log.d("---", endX+""+endY);
TranslateAnimation translateAnimationX = new TranslateAnimation(0,
endX, 0, 0);
translateAnimationX.setInterpolator(new LinearInterpolator());
translateAnimationX.setRepeatCount(0);// 动画重复执行的次数
translateAnimationX.setFillAfter(true);

TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0,
0, endY);
translateAnimationY.setInterpolator(new AccelerateInterpolator());
translateAnimationY.setRepeatCount(0);// 动画重复执行的次数
translateAnimationX.setFillAfter(true);

AnimationSet set = new AnimationSet(false);
set.setFillAfter(false);
set.addAnimation(translateAnimationY);
set.addAnimation(translateAnimationX);
set.setDuration(Math.abs(endY/1));// 动画的执行时间
v.startAnimation(set);
set.setAnimationListener(new AnimationListener() {
// 动画的开始
@Override
public void onAnimationStart(Animation animation) {
v.setVisibility(View.VISIBLE);
}

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

// 动画的结束
@Override
public void onAnimationEnd(Animation animation) {
v.setVisibility(View.GONE);
}
});
}
}


例子

https://github.com/SeniorZhai/ShoppingAnimation

文章目录