This article uses frame-by-box animations and motion tweens to implement a sample, first we look at the motion tweens in Android.
Using animation in Android represents an abstract animation class that contains several subclasses:
Alphaanimation: Transparent change animation.
Scaleanimation: Size scale animation.
Translateanimation: Animation of displacement change.
Rotateanimation: Rotate the animation.
Here we use displacement animations and frame-by-frames animations to implement this sample. Let's take a look at the effect:
The butterfly-waving wing-by-frame animation file:
<?xml version= "1.0" encoding= "utf-8"?><!--definition animation loop playback--><animation-list xmlns:android= "/http Schemas.android.com/apk/res/android "android:oneshot=" false "><item android:drawable=" @drawable/butterfly_ F01 "android:duration="/><item android:drawable= "@drawable/butterfly_f02" android:duration= "/>" <item android:drawable= "@drawable/butterfly_f03" android:duration= "/><item android:drawable=" @ Drawable/butterfly_f04 "android:duration="/><item android:drawable= "@drawable/butterfly_f05" Android: duration= "/><item android:drawable=" @drawable/butterfly_f06 "android:duration=" "/></" Animation-list>
interface layout file:
<?xml version= "1.0" encoding= "Utf-8"?><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:orientation= "vertical" Android:layout_width= "Fill_parent" android:layout_height= "fill_parent" android:background= "@drawable/background" ><imageviewandroid:id= "@+id/butterfly" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content "android:background=" @anim/butteryfly "/></linearlayout>
detailed logic and displacement animations:
Package Com.example.butteryfly;import Java.util.timer;import Java.util.timertask;import android.app.Activity; Import Android.graphics.drawable.animationdrawable;import Android.os.bundle;import Android.os.handler;import Android.view.display;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.animation.translateanimation;import Android.widget.imageview;public class MainActivity extends Activity {private float CurX = 0;private float CurY = 30;private float nextx = 0;private float nexty = 0;private int Windo WW = 0;private int windowh = 0;private ImageView ImageView; Handler Handler = new Handler () {public void Handlemessage (Android.os.Message msg) {if (Msg.what = = 0x123) {if (Nextx > win Doww | | Nexty > Windowh) {curx = NEXTX = 0;} ELSE{NEXTX + = 8;} Nexty = CurY + (float) (Math.random () * 20-10); Translateanimation anim = new Translateanimation (CurX, NEXTX, CurY, nexty); CurX = Nextx;cury = Nexty;anim.setduration (200 ); Imageview.startanimation (anim);};}; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main) ImageView = (ImageView) Findviewbyid (r.id.butterfly);D isplay Display = Getwindowmanager (). Getdefaultdisplay (); WINDOWW = Display.getwidth (); windowh = Display.getheight (); final animationdrawable butterfly = (animationdrawable) Imageview.getbackground (); Imageview.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View arg0) {Butterfly.start (); new Timer (). Schedule (new TimerTask () {@Overridepublic void run () {Handler.sendemptymessage ( 0x123);}}, 0, 200)}});}}
Animation in Android specific explanation series "2"-Dancing Butterflies