The example of this article describes the implementation of the interface bounce-tip animation effect of Android programming. Share to everyone for your reference, specific as follows:
Previous Effect chart:
First on the layout:
<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 "> < Relativelayout android:id= "@+id/red" android:layout_width= "match_parent" android:layout_height= "Match_parent" Andr oid:background= "#ff0000" > <textview android:layout_width= "wrap_content android:layout_height=" wrap_content "Android:layout_above=" @+id/switch2blue "android:layout_centerhorizontal=" true "android:text=" Home "/> <Bu Tton android:id= "@+id/switch2blue" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" an Droid:layout_centerinparent= "true" android:text= "displacement bit blue"/> </RelativeLayout> <relativelayout Android:id = "@+id/blue" android:layout_width= "match_parent" android:layout_height= "match_parent" android:background= "#0000ff "> <textview android:layout_width=" wrap_content"Android:layout_height=" wrap_content "android:layout_above=" @+id/switch2red "android:layout_centerhorizontal=" tr UE "android:text=" second page "/> <button android:id=" @+id/switch2red "android:layout_width=" Wrap_content "and
roid:layout_height= "Wrap_content" android:layout_alignparentbottom= "true" android:layout_centerhorizontal= "true"
android:text= "Replacement bit red"/> </RelativeLayout> </RelativeLayout>
The
Code is as follows:
Import Java.lang.reflect.Field;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.util.DisplayMetrics;
Import Android.view.GestureDetector;
Import android.view.MotionEvent;
Import Android.view.View;
Import Android.view.GestureDetector.OnGestureListener;
Import Android.view.View.OnClickListener;
Import Android.view.View.OnTouchListener;
Import Android.view.animation.AccelerateDecelerateInterpolator;
Import Android.view.animation.AccelerateInterpolator;
Import android.view.animation.Animation;
Import Android.view.animation.AnimationSet;
Import android.view.animation.TranslateAnimation;
Import Android.view.animation.Animation.AnimationListener;
Import Android.widget.Button;
Import Android.widget.RelativeLayout; public class Mainactivity extends activity implements Onclicklistener, Ontouchlistener, Ongesturelistener {private Rel
Ativelayout red, blue;
Private Button switch2blue, switch2red;
private float Thisdelta = 0.05f; Private StatiC Boolean haseverpulled = false;
Private Boolean pulled = false;
private static int height;
Private Gesturedetector Gesturedetector;
private static int statusheight = 0;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Displaymetrics metrics = getresources (). Getdisplaymetrics ();
Height = metrics.heightpixels;
Statusheight = Getstatusheight ();
Initview ();
Bounce hint can pull final Handler Handler = new Handler ();
Handler.postdelayed (New Runnable () {@Override public void run () {jump (Thisdelta);
Handler.postdelayed (this, 3000);
}, 3000);
private void Initview () {red = (relativelayout) Findviewbyid (r.id.red);
Blue = (relativelayout) Findviewbyid (R.id.blue);
Switch2blue = (Button) Findviewbyid (R.id.switch2blue);
switch2red = (Button) Findviewbyid (r.id.switch2red);
Switch2blue.setonclicklistener (this);
Switch2red.setonclicklistener (this); Blue. Setontouchlistener (this);
Blue.setlongclickable (TRUE);
Gesturedetector = new Gesturedetector (this, this); @Override public void OnClick (View arg0) {//TODO auto-generated a stub switch (Arg0.getid ()) {case R.id.
Switch2blue:red2blueui ();
Break
Case R.id.switch2red:blue2redui ();
Break
}//from the Red page to the blue page private void Red2blueui () {Blue.bringtofront ();
Blue.requestlayout ();
Blue.invalidate ();
//from the Blue page to the red page private void Blue2redui () {Red.bringtofront ();
Red.requestlayout ();
Red.invalidate ();
//Get the height of the status bar private int getstatusheight () {class<?> c = null;
Object obj = null;
Field field = NULL;
int x = 0;
int height = 0;
try {c = class.forname ("Com.android.internal.r$dimen");
obj = C.newinstance ();
field = C.getfield ("Status_bar_height");
x = Integer.parseint (Field.get (obj). toString ());
Height = getresources (). getdimensionpixelsize (x);
catch (Exception e) {e.printstacktrace (); RetUrn height;
}//home page beats private void jump (float delta) {if (thisdelta-0.03f < 0.001f) {Thisdelta = 0.05f;
Return
} Thisdelta = Delta;
if (haseverpulled) {return;
} playjumpanimation (Thisdelta);
}//Oberra/dropdown homepage private void Pull (Boolean upward) {if (upward && pulled) {return;
} if (!upward &&!pulled) {return;
float originaly;
float finaly;
if (!pulled) {originaly = 0;
finaly = (float) (0-height + 0.4 * height);
else {originaly = (float) (0-height + 0.4 * height);
finaly = 0;
} pulled =!pulled;
Animationset animationset = new Animationset (true);
Animationset.addanimation (New translateanimation (0, 0, originaly, finaly));
Animationset.setduration (300);
Animationset.setinterpolator (New Acceleratedecelerateinterpolator ());
Animationset.setfillafter (TRUE); Animationset.setanimationlistener (New Animationlistener () {@Override public void Onanimationstart (Animation animatiON) {if (!pulled) {Red2blueui (); @Override public void Onanimationrepeat (Animation Animation) {} @Override public void Onanimationen
D (Animation Animation) {//if (pulled) {//Blue2redui ();
// }
}
});
Blue.startanimation (Animationset);
Haseverpulled = true;
///Jump animation private void Playjumpanimation (final float delta) {float originaly = 0;
float finaly = 0-height * DELTA;
Animationset animationset = new Animationset (true);
Animationset.addanimation (New translateanimation (0, 0, originaly, finaly));
Animationset.setduration (300);
Animationset.setinterpolator (New Acceleratedecelerateinterpolator ());
Animationset.setfillafter (TRUE); Animationset.setanimationlistener (New Animationlistener () {@Override public void Onanimationstart (Animation animati ON) {} @Override public void Onanimationrepeat (Animation Animation) {} @Override public void Onanimati OnEnd (Animation Animation) {PLAylandanimation (delta);
}
});
Blue.startanimation (Animationset);
}//Falling animation private void Playlandanimation (final float delta) {Float originaly = 0-height * DELTA;
float finaly = 0;
Animationset animationset = new Animationset (true);
Animationset.addanimation (New translateanimation (0, 0, originaly, finaly));
Animationset.setduration (200);
Animationset.setinterpolator (New Accelerateinterpolator ());
Animationset.setfillafter (TRUE); Animationset.setanimationlistener (New Animationlistener () {@Override public void Onanimationstart (Animation animati ON) {} @Override public void Onanimationrepeat (Animation Animation) {} @Override public void Onanimati
OnEnd (Animation Animation) {jump (0.03f);
}
});
Blue.startanimation (Animationset);
@Override public boolean Ondown (Motionevent e) {pull (false);
return false; @Override public boolean onfling (Motionevent E1, motionevent E2, float Velocityx, float velocityy) {//gesture sliding up to 100 to trigger if (e1.gety ()-e2.gety () >) {pull (true);
else if (e2.gety () >= e1.gety ()) {pull (false);
return false; @Override public void onlongpress (Motionevent e) {} @Override public boolean onscroll (Motionevent E1, motionevent
E2, float Distancex, float Distancey) {return false; The @Override public void onshowpress (Motionevent e) {} @Override public boolean onsingletapup (Motionevent e) {Retu
RN false; @Override public boolean Ontouch (View V, motionevent event) {if (pulled) {//First page Touch point if (Event.gety () > H
Eight * 0.4-statusheight) {return false;
} return Gesturedetector.ontouchevent (event); }
}
I hope this article will help you with your Android programming.