Start the interface:
Implementation of the principle: in a thread to let a few pictures of the loop display, to achieve the effect of animation.
The main implementation class Loadingview is a custom control that inherits the ImageView and implements the Runnable interface:
Package Wht.android.loading;import Android.content.context;import Android.graphics.canvas;import Android.util.attributeset;import Android.widget.imageview;public class Loadingview extends ImageView implements runnable{Private Boolean isstop = false; Private int[] Imageids; private int index = 0; private int length = 1; Public Loadingview (Context context) {This (context, NULL); } public Loadingview (context context, AttributeSet Attrs) {Super (context, attrs); } public void Setimageids (int[] imageId) {this.imageids = ImageId; if (imageids! = null && imageids.length > 0) {length = Imageids.length; }} @Override protected void Ondetachedfromwindow () { TODO auto-generated Method Stub sUper.ondetachedfromwindow (); Isstop = true; } @Override protected void OnDraw (canvas canvas) {//TODO auto-generated method stub Super.ondraw (canvas); if (imageids! = null && imageids.length > 0) {this.setimageresource (image Ids[index]); }} @Override public void Run () {while (!isstop) { Calculate the position of the picture to display index = ++index% length; Postinvalidate (); try {Thread.Sleep (400); } catch (Interruptedexception e) {E.printsta Cktrace (); }}}//Start picture rotation thread public void Startanim () { New Thread (This). Start (); }}
mainactivity: The Loadingview in it is our custom control, which is used in layouts like other controls.
Package Wht.android.loading;import Android.os.bundle;import Android.app.activity;import android.view.Menu;public Class Mainactivity extends Activity {private Loadingview main_imageview; @Overrideprotected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main); Main_imageview = ( Loadingview) Findviewbyid (R.id.main_imageview); Initloadingimages (); new Thread () {@Overridepublic void run () {Main_ Imageview.startanim ();}}. Start ();} private void Initloadingimages () {int[] imageids = new Int[6];imageids[0] = r.drawable.loader_frame_1;imageids[1] = R.dra WABLE.LOADER_FRAME_2;IMAGEIDS[2] = r.drawable.loader_frame_3;imageids[3] = r.drawable.loader_frame_4;imageids[4] = R.DRAWABLE.LOADER_FRAME_5;IMAGEIDS[5] = R.drawable.loader_frame_6;main_imageview.setimageids (IMAGEIDS);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return True;}}
the Wht.android.loading.LoadingView used in the layout file Main.xml is the previously defined control, with the full package name plus the class name.
<?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:background=" #e1e1e1 "a ndroid:gravity= "center" android:orientation= "vertical" > <linearlayout android:layout_width= "Fill_paren T "android:layout_height=" 65dip "android:orientation=" vertical "> <WHT.ANDROID.LOADING.LOADINGV Iew android:id= "@+id/main_imageview" android:layout_width= "Wrap_content" Android:layout_h eight= "Wrap_content" android:layout_gravity= "Center_horizontal" android:src= "@drawable/L Oader_frame_1 "> </wht.android.loading.LoadingView> <textview android:layout_width=" W Rap_content "android:layout_height=" wrap_content "android:layout_gravity=" Center_horizontal " Android:layout_margintop= "10dip"android:text=" Start ... "android:textcolor=" #666666 "android:textsize=" 20sp "/> < /linearlayout></linearlayout>
: http://download.csdn.net/detail/l1976135784/7281483