First:
This is accomplished by clicking on the button above and moving the textview randomly to the position of the bottom button
First, put the bottom button in the list to make it easy for you to take a random value later
List = new arraylist<button> (); List.add (BTN1); List.add (BTN2); List.add (BTN3); List.add (BTN4);
Then it's the parabolic animation after clicking the button.
After clicking the button, write an array to store the x, y coordinates of the click button, then new a control to display the parabola, the landlord here with the TextView, can also be replaced by any other control
int[] start_location = new int[2];//An integer array to store the button's x, y coordinates V.getlocationinwindow (start_location) on the screen;// This is the x, y coordinate of the button that gets the current click on the screen (this is also the coordinate at which the animation starts) TextView te = new TextView (this); Te.settext ("Ah");
Next, the control you want to move is placed in an animation layer
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); 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);
The next thing is to write the animation of the parabolic line.
Parabolic is actually two displacement animation, a lateral movement, a vertical movement, two animations simultaneously executed, there is the effect of parabolic line
<span style= "color: #000000;" >int EndX = 0-start_location[0] + list.get (Ra.nextint (List.size ())). GetLeft ();//animation displacement x-coordinate int endY = end_location[1]- start_location[1];//Animation Displacement y-coordinate translateanimation Translateanimationx = new Translateanimation (0,endX, 0, 0); Translateanimationx.setinterpolator (New Linearinterpolator ()); Translateanimationx.setrepeatcount (0);// The number of times the animation repeats Translateanimationx.setfillafter (true); Translateanimation translateanimationy = new Translateanimation (0,0, 0, EndY); Translateanimationy.setinterpolator ( New Accelerateinterpolator ()); Translateanimationy.setrepeatcount (0);//The number of times the animation repeats Translateanimationx.setfillafter (true); Animationset set = new Animationset (false); Set.setfillafter (false); set.addanimation (Translateanimationy); Set.addanimation (Translateanimationx); set.setduration (800);//Animation Execution Time View.startanimation (set);</span>
Finally, listen to the animation, the animation will be moved when the control is displayed, the animation is over to hide it
The complete code is as follows:
Package Com.example.movetest;import Java.util.arraylist;import Java.util.list;import java.util.random;import Android.app.activity;import Android.os.bundle;import Android.view.view;import Android.view.View.OnClickListener; Import Android.view.viewgroup;import Android.view.animation.accelerateinterpolator;import Android.view.animation.animation;import Android.view.animation.animation.animationlistener;import Android.view.animation.animationset;import Android.view.animation.linearinterpolator;import Android.view.animation.translateanimation;import Android.widget.button;import Android.widget.LinearLayout;import android.widget.textview;/*** * * @author brim can't cover the sun * * @date 2016/5/9 * */public class Mainactivity extends Activity imple ments Onclicklistener {private list<button> List = null; private ViewGroup viewgroup;//animation layer private Button btn1, BT N2, Btn3, Btn4; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.aCtivity_main); Findviewbyid (R.ID.BTN). Setonclicklistener (this); BTN1 = (Button) Findviewbyid (R.ID.BTN1); BTN2 = (Button) Findviewbyid (R.ID.BTN2); Btn3 = (Button) Findviewbyid (R.ID.BTN3); Btn4 = (Button) Findviewbyid (R.ID.BTN4); List = new arraylist<button> (); List.add (BTN1); List.add (BTN2); List.add (BTN3); List.add (BTN4); } @Override public void OnClick (View v) {//TODO auto-generated Method Stub switch (V.getid ()) {case r.id.btn:int[ ] Start_location = new int[2];//An integer array to store the x, y coordinates V.getlocationinwindow (start_location) of the button on the screen;//This is the button that gets the current click on the X of the screen, The y-coordinate (which is also the coordinate at which the animation starts) TextView te = new TextView (this); Te.settext ("Ah"); Move (TE, start_location); Break }} private void Move (final View V, int[] start_location) {viewgroup = null; ViewGroup = Createanimlayout (); Viewgroup.addview (v);//Add the control you want to move to the animation layer final View view = Addviewtoanimlayout (ViewGroup, V, start_location); int[] End_location = new int[2];//This is the x, y coordinate where the end of the animation is stored random ra = new Random (); for (int i = 0; I < list.size (); i++) {list.get (Ra.nextint (List.size ())). Getlocationinwindow (end_location); calculate displacement int endx = 0-start_location[0] + list.get (Ra.nextint (List.size ())). GetLeft ();//animation displacement x-coordinate int endY = End_ LOCATION[1]-start_location[1];//The y-coordinate of the animation displacement translateanimation Translateanimationx = new Translateanimation (0, EndX, 0, 0); Translateanimationx.setinterpolator (New Linearinterpolator ()); Translateanimationx.setrepeatcount (0);//The number of times the animation repeats Translateanimationx.setfillafter (true); Translateanimation translateanimationy = new Translateanimation (0, 0, 0, EndY); Translateanimationy.setinterpolator (New Accelerateinterpolator ()); Translateanimationy.setrepeatcount (0);//The number of times the animation repeats Translateanimationx.setfillafter (true); Animationset set = new Animationset (false); Set.setfillafter (FALSE); Set.addanimation (Translateanimationy); Set.addanimation (Translateanimationx); Set.setduration (800);//Animation Execution Time view.startanimation (set); Animated Listener Events Set.setanimationlistener (New Animationlistener () {///start of animation @Override public void Onanimationstart (Animation ani mation) {v.setvisibility (view.visible); } @Override public void Onanimationrepeat (Animation Animation) {//TODO auto-generated Method stub}//Move The end of the drawing @Override public void Onanimationend (Animation Animation) {v.setvisibility (view.gone); } }); Break }}/** * Create animation Layer * * @return */private ViewGroup createanimlayout () {ViewGroup Rootview = (viewgroup) This.getwindo W (). Getdecorview (); LinearLayout animlayout = new LinearLayout (this); Linearlayout.layoutparams LP = new Linearlayout.layoutparams (LinearLayout.LayoutParams.MATCH_PARENT, LINEARLAYOUT.L Ayoutparams.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.L Ayoutparams.wrap_content); Lp.leftmargin = x; Lp.topmargin = y; VIEW.SETLAYOUTPARAMS (LP); return view; }}
: http://download.csdn.net/detail/qq_18612815/9514682
Android animation effect (i) parabolic animation between any two points