First, show the animation testing interface:
The layout file main. XML is:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="30dp" android:src="@drawable/naruto" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="30dp" android:text="click me to show" /></LinearLayout>
Next, create a custom animation class customanimation inherited from animation. You need to rewrite the applytransformation and initialize methods. The specific explanations are given in the Code. So, here we will not repeat O (operator _ operator) O. The custom class manmanimation code is as follows:
Package COM. android. animation; import android. graphics. matrix; import android. view. animation. animation; import android. view. animation. anticipateovershootinterpolator; import android. view. animation. transformation; public class customanimation extends animation {int centerx, centery; Public customanimation () {}@ override protected void applytransformation (float interpolatedtime, transformation T) {// todo auto-generated method stub super. applytransformation (interpolatedtime, T); matrix = T. getmatrix (); // use interpolatedtime to set matrix scaling. setscale (interpolatedtime, interpolatedtime); matrix. pretranslate (-centerx,-centery); matrix. posttranslate (centerx, centery);} // initialize the animation @ override public void initialize (INT width, int height, int parentwidth, int parentheight) {// todo auto-generated method stub super. initialize (width, height, parentwidth, parentheight); // initialize the midpoint coordinate centerx = width/2; centery = height/2; // The animation duration setduration (2000 ); // set whether to retain the animation at the ending position after the animation ends setfillafter (true);/*** set the animation acceleration curve. The introduced sub-classes of interpolator include: * anticipateovershootinterpolator changes start and backward, and then the change speed beyond the screen * acceleratedecelerateinterpolator starts and ends slowly, but the change speed between acceleration and * accelerateinterpolator starts slowly, then, the acceleration * anticipateinterpolator changes start to backward, and then shot forward * bounceinterpolator changes to the final rebound * cycleinterpolator repeated animation specified number of cycle cycleinterpolator (float cycles) you need to input the number of cycles * decelerateinterpolator starts to change quickly, and then starts to slow down * linearinterpolator linear change * overshootinterpolator emits forward, exceeds the screen and returns ** PS-> poor text skills, the description is not very accurate. Sorry, sorry. I still need to try it by myself */setinterpolator (New anticipateovershootinterpolator ());}}
Finally, use the Custom Animation in the main activity:
Package COM. android. animation; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. imageview; public class customanimationactivity extends activity {/** called when the activity is first created. */private customanimation animation; private imageview image; private button; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); // instantiate animation = new customanimation (); image = (imageview) findviewbyid (R. id. image); button = (button) findviewbyid (R. id. button); button. setonclicklistener (New onclicklistener () {@ override public void onclick (view arg0) {// todo auto-generated method stub // set the animation effect for the image. setanimation (animation );}});}}