Skip to content

Commit a51fc9f

Browse files
author
miaocong
committedOct 20, 2014
the moving ball with animation when we add something into my shopping cart
1 parent 0333ef5 commit a51fc9f

24 files changed

+448
-0
lines changed
 

Diff for: ‎.classpath

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="src" path="gen"/>
5+
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
6+
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.LIBRARIES"/>
7+
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
8+
<classpathentry kind="output" path="bin/classes"/>
9+
</classpath>

Diff for: ‎.project

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>TestBall</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>com.android.ide.eclipse.adt.ResourceManagerBuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>com.android.ide.eclipse.adt.PreCompilerBuilder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>org.eclipse.jdt.core.javabuilder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
<buildCommand>
24+
<name>com.android.ide.eclipse.adt.ApkBuilder</name>
25+
<arguments>
26+
</arguments>
27+
</buildCommand>
28+
</buildSpec>
29+
<natures>
30+
<nature>com.android.ide.eclipse.adt.AndroidNature</nature>
31+
<nature>org.eclipse.jdt.core.javanature</nature>
32+
</natures>
33+
</projectDescription>

Diff for: ‎AndroidManifest.xml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.example.testball"
4+
android:versionCode="1"
5+
android:versionName="1.0" >
6+
7+
<uses-sdk
8+
android:minSdkVersion="8"
9+
android:targetSdkVersion="14" />
10+
11+
<application
12+
android:allowBackup="true"
13+
android:icon="@drawable/ic_launcher"
14+
android:label="@string/app_name"
15+
android:theme="@style/AppTheme" >
16+
<activity
17+
android:name=".MainActivity"
18+
android:label="@string/app_name" >
19+
<intent-filter>
20+
<action android:name="android.intent.action.MAIN" />
21+
22+
<category android:name="android.intent.category.LAUNCHER" />
23+
</intent-filter>
24+
</activity>
25+
</application>
26+
27+
</manifest>

Diff for: ‎ic_launcher-web.png

50.2 KB
Loading

Diff for: ‎libs/android-support-v4.jar

741 KB
Binary file not shown.

Diff for: ‎proguard-project.txt

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# To enable ProGuard in your project, edit project.properties
2+
# to define the proguard.config property as described in that file.
3+
#
4+
# Add project specific ProGuard rules here.
5+
# By default, the flags in this file are appended to flags specified
6+
# in ${sdk.dir}/tools/proguard/proguard-android.txt
7+
# You can edit the include path and order by changing the ProGuard
8+
# include property in project.properties.
9+
#
10+
# For more details, see
11+
# http://developer.android.com/guide/developing/tools/proguard.html
12+
13+
# Add any project specific keep options here:
14+
15+
# If your project uses WebView with JS, uncomment the following
16+
# and specify the fully qualified class name to the JavaScript interface
17+
# class:
18+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
19+
# public *;
20+
#}

Diff for: ‎project.properties

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This file is automatically generated by Android Tools.
2+
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
3+
#
4+
# This file must be checked in Version Control Systems.
5+
#
6+
# To customize properties used by the Ant build system edit
7+
# "ant.properties", and override values to adapt the script to your
8+
# project structure.
9+
#
10+
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
11+
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt
12+
13+
# Project target.
14+
target=android-14
15+
android.library.reference.1=../appcompat_v7

Diff for: ‎res/drawable-hdpi/addshopcart_shopping_cart.png

6.35 KB
Loading

Diff for: ‎res/drawable-hdpi/addshopcart_sign.png

369 Bytes
Loading

Diff for: ‎res/drawable-hdpi/ic_launcher.png

7.48 KB
Loading

Diff for: ‎res/drawable-mdpi/ic_launcher.png

3.69 KB
Loading

Diff for: ‎res/drawable-xhdpi/ic_launcher.png

12.2 KB
Loading

Diff for: ‎res/drawable-xxhdpi/ic_launcher.png

24.2 KB
Loading

Diff for: ‎res/layout/activity_main.xml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
tools:context="com.example.testball.MainActivity" >
6+
7+
<ListView
8+
android:id="@+id/shop_listview"
9+
android:layout_width="match_parent"
10+
android:layout_height="match_parent"
11+
android:listSelector="@android:color/transparent"
12+
android:cacheColorHint="@android:color/transparent"
13+
/>
14+
<ImageView
15+
android:id="@+id/buy_car"
16+
android:layout_width="wrap_content"
17+
android:layout_height="wrap_content"
18+
android:background="@drawable/addshopcart_shopping_cart"
19+
android:layout_alignParentLeft="true"
20+
android:layout_alignParentBottom="true"
21+
android:layout_marginLeft="20dp"
22+
android:layout_marginBottom="20dp"
23+
/>
24+
</RelativeLayout>

Diff for: ‎res/layout/listitem_layout.xml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:orientation="horizontal"
6+
android:padding="10dp"
7+
android:gravity="center_vertical"
8+
android:descendantFocusability="blocksDescendants">
9+
<TextView
10+
android:id="@+id/listitem_title"
11+
android:layout_width="0dp"
12+
android:layout_height="wrap_content"
13+
android:layout_weight="5"
14+
/>
15+
<Button
16+
android:id="@+id/listitem_btn"
17+
android:layout_width="0dp"
18+
android:layout_height="wrap_content"
19+
android:layout_weight="2"
20+
android:text="购买"
21+
/>
22+
</LinearLayout>

Diff for: ‎res/menu/main.xml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<menu xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:app="http://schemas.android.com/apk/res-auto"
3+
xmlns:tools="http://schemas.android.com/tools"
4+
tools:context="com.example.testball.MainActivity" >
5+
6+
<item
7+
android:id="@+id/action_settings"
8+
android:orderInCategory="100"
9+
android:title="@string/action_settings"
10+
app:showAsAction="never"/>
11+
12+
</menu>

Diff for: ‎res/values-v11/styles.xml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<resources>
2+
3+
<!--
4+
Base application theme for API 11+. This theme completely replaces
5+
AppBaseTheme from res/values/styles.xml on API 11+ devices.
6+
-->
7+
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
8+
<!-- API 11 theme customizations can go here. -->
9+
</style>
10+
11+
</resources>

Diff for: ‎res/values-v14/styles.xml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<resources>
2+
3+
<!--
4+
Base application theme for API 14+. This theme completely replaces
5+
AppBaseTheme from BOTH res/values/styles.xml and
6+
res/values-v11/styles.xml on API 14+ devices.
7+
-->
8+
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
9+
<!-- API 14 theme customizations can go here. -->
10+
</style>
11+
12+
</resources>

Diff for: ‎res/values-w820dp/dimens.xml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<resources>
2+
3+
<!--
4+
Example customization of dimensions originally defined in res/values/dimens.xml
5+
(such as screen margins) for screens with more than 820dp of available width. This
6+
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively).
7+
-->
8+
<dimen name="activity_horizontal_margin">64dp</dimen>
9+
10+
</resources>

Diff for: ‎res/values/dimens.xml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<resources>
2+
3+
<!-- Default screen margins, per the Android Design guidelines. -->
4+
<dimen name="activity_horizontal_margin">16dp</dimen>
5+
<dimen name="activity_vertical_margin">16dp</dimen>
6+
7+
</resources>

Diff for: ‎res/values/strings.xml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
4+
<string name="app_name">TestBall</string>
5+
<string name="hello_world">Hello world!</string>
6+
<string name="action_settings">Settings</string>
7+
8+
</resources>

Diff for: ‎res/values/styles.xml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<resources>
2+
3+
<!--
4+
Base application theme, dependent on API level. This theme is replaced
5+
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
6+
-->
7+
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
8+
<!--
9+
Theme customizations available in newer API levels can go in
10+
res/values-vXX/styles.xml, while customizations related to
11+
backward-compatibility can go here.
12+
-->
13+
</style>
14+
15+
<!-- Application theme. -->
16+
<style name="AppTheme" parent="AppBaseTheme">
17+
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
18+
</style>
19+
20+
</resources>

Diff for: ‎src/com/example/testball/MainActivity.java

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.example.testball;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import android.support.v7.app.ActionBarActivity;
7+
import android.os.Bundle;
8+
import android.view.Menu;
9+
import android.view.MenuItem;
10+
import android.widget.ImageView;
11+
import android.widget.ListView;
12+
13+
public class MainActivity extends ActionBarActivity {
14+
15+
private List<String> mDataList;
16+
private ListView listView;
17+
private ShopItemAdapter adapter;
18+
private ImageView buyCarImg;
19+
20+
private int[] buyCarLocation;
21+
22+
@Override
23+
protected void onCreate(Bundle savedInstanceState) {
24+
super.onCreate(savedInstanceState);
25+
setContentView(R.layout.activity_main);
26+
27+
listView = (ListView)findViewById(R.id.shop_listview);
28+
buyCarImg = (ImageView)findViewById(R.id.buy_car);
29+
getDataList();
30+
adapter = new ShopItemAdapter(this, mDataList);
31+
listView.setAdapter(adapter);
32+
}
33+
34+
private void getDataList(){
35+
mDataList = new ArrayList<String>();
36+
for (int i = 0; i < 20; i++) {
37+
mDataList.add("第" + i + "个title");
38+
}
39+
40+
}
41+
42+
public int[] getLocation(){
43+
buyCarLocation = new int[2];
44+
buyCarImg.getLocationInWindow(buyCarLocation);
45+
return buyCarLocation;
46+
}
47+
48+
@Override
49+
public boolean onCreateOptionsMenu(Menu menu) {
50+
// Inflate the menu; this adds items to the action bar if it is present.
51+
getMenuInflater().inflate(R.menu.main, menu);
52+
return true;
53+
}
54+
55+
@Override
56+
public boolean onOptionsItemSelected(MenuItem item) {
57+
// Handle action bar item clicks here. The action bar will
58+
// automatically handle clicks on the Home/Up button, so long
59+
// as you specify a parent activity in AndroidManifest.xml.
60+
int id = item.getItemId();
61+
if (id == R.id.action_settings) {
62+
return true;
63+
}
64+
return super.onOptionsItemSelected(item);
65+
}
66+
}

Diff for: ‎src/com/example/testball/ShopItemAdapter.java

+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package com.example.testball;
2+
3+
import java.util.List;
4+
5+
import android.app.Activity;
6+
import android.content.Context;
7+
import android.util.Log;
8+
import android.util.Property;
9+
import android.view.LayoutInflater;
10+
import android.view.View;
11+
import android.view.View.OnClickListener;
12+
import android.view.ViewGroup;
13+
import android.view.animation.AccelerateInterpolator;
14+
import android.view.animation.Animation;
15+
import android.view.animation.Animation.AnimationListener;
16+
import android.view.animation.AnimationSet;
17+
import android.view.animation.LinearInterpolator;
18+
import android.view.animation.TranslateAnimation;
19+
import android.widget.BaseAdapter;
20+
import android.widget.Button;
21+
import android.widget.ImageView;
22+
import android.widget.LinearLayout;
23+
import android.widget.LinearLayout.LayoutParams;
24+
import android.widget.TextView;
25+
26+
public class ShopItemAdapter extends BaseAdapter {
27+
28+
private Context context;
29+
private List<String> list;
30+
private ImageView ballImage;
31+
private ViewGroup animationLayer;
32+
33+
34+
public ShopItemAdapter(Context context, List<String> list) {
35+
super();
36+
this.context = context;
37+
this.list = list;
38+
}
39+
40+
@Override
41+
public int getCount() {
42+
// TODO Auto-generated method stub
43+
return list == null ? 0 : list.size();
44+
}
45+
46+
@Override
47+
public Object getItem(int position) {
48+
// TODO Auto-generated method stub
49+
return null;
50+
}
51+
52+
@Override
53+
public long getItemId(int position) {
54+
// TODO Auto-generated method stub
55+
return 0;
56+
}
57+
58+
@Override
59+
public View getView(int position, View convertView, ViewGroup parent) {
60+
ViewHolder viewHolder;
61+
if (convertView == null) {
62+
viewHolder = new ViewHolder();
63+
convertView = LayoutInflater.from(context).inflate(R.layout.listitem_layout, null);
64+
viewHolder.titleText = (TextView)convertView.findViewById(R.id.listitem_title);
65+
viewHolder.buyBtn = (Button)convertView.findViewById(R.id.listitem_btn);
66+
convertView.setTag(viewHolder);
67+
} else {
68+
viewHolder = (ViewHolder)convertView.getTag();
69+
}
70+
viewHolder.titleText.setText(list.get(position));
71+
viewHolder.buyBtn.setOnClickListener(new OnClickListener() {
72+
73+
@Override
74+
public void onClick(View v) {
75+
int[] startLocation = new int[2];
76+
v.getLocationInWindow(startLocation);
77+
ballImage = new ImageView(context);
78+
ballImage.setBackgroundResource(R.drawable.addshopcart_sign);
79+
setAnimation(ballImage,startLocation);
80+
}
81+
});
82+
return convertView;
83+
}
84+
85+
private void setAnimation(final View v,int[] startLoc){
86+
// animationLayer = null;
87+
animationLayer = createAnimationLayer();
88+
animationLayer.addView(v);
89+
final View view = addBallToLayer(v,startLoc);
90+
int[] endLoc = ((MainActivity)context).getLocation();
91+
int xDelt = startLoc[0] - endLoc[0];
92+
int yDelt = startLoc[1] -endLoc[1];
93+
TranslateAnimation transformationX = new TranslateAnimation(0, -xDelt, 0, 0);
94+
transformationX.setInterpolator(new LinearInterpolator());
95+
transformationX.setRepeatCount(0);
96+
transformationX.setFillAfter(true);
97+
TranslateAnimation transformationY = new TranslateAnimation(0, 0, 0, -yDelt);
98+
transformationY.setInterpolator(new AccelerateInterpolator());
99+
transformationY.setRepeatCount(0);
100+
transformationY.setFillAfter(true);
101+
102+
AnimationSet animationSet = new AnimationSet(false);
103+
animationSet.addAnimation(transformationX);
104+
animationSet.addAnimation(transformationY);
105+
animationSet.setDuration(800);
106+
animationSet.setFillAfter(true);
107+
view.startAnimation(animationSet);
108+
animationSet.setAnimationListener(new AnimationListener() {
109+
110+
@Override
111+
public void onAnimationStart(Animation animation) {
112+
v.setVisibility(View.VISIBLE);
113+
}
114+
115+
@Override
116+
public void onAnimationRepeat(Animation animation) {
117+
// TODO Auto-generated method stub
118+
119+
}
120+
121+
@Override
122+
public void onAnimationEnd(Animation animation) {
123+
Log.v("mickey", "--onAnimationEnd---isShown:" + v.isShown());
124+
view.clearAnimation();
125+
v.setVisibility(View.GONE);
126+
}
127+
});
128+
}
129+
130+
private ViewGroup createAnimationLayer(){
131+
ViewGroup rootView = (ViewGroup) ((Activity)context).getWindow().getDecorView();
132+
LinearLayout animationLayer = new LinearLayout(context);
133+
LinearLayout.LayoutParams lParams = new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
134+
animationLayer.setLayoutParams(lParams);
135+
rootView.addView(animationLayer);
136+
return animationLayer;
137+
}
138+
139+
private View addBallToLayer(View view,int[] ballLoc){
140+
LinearLayout.LayoutParams lParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
141+
lParams.leftMargin = ballLoc[0];
142+
lParams.topMargin = ballLoc[1];
143+
view.setLayoutParams(lParams);
144+
return view;
145+
}
146+
147+
static class ViewHolder{
148+
TextView titleText;
149+
Button buyBtn;
150+
151+
}
152+
}

0 commit comments

Comments
 (0)
Please sign in to comment.