android啟動介面

來源:互聯網
上載者:User

標籤:android   介面   

啟動介面的:

實現的原理:在一個線程中讓幾幅圖片進行迴圈顯示,實現了動畫的效果。
主要的實作類別LoadingView是自訂控制項,繼承了ImageView,實現了Runnable介面 :

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(imageIds[index]);                }        }        @Override        public void run()        {                while(!isStop)                {                        //計算要顯示圖片的位置                        index = ++index % length;                        postInvalidate();                        try                        {                                Thread.sleep(400);                        }                        catch (InterruptedException e)                        {                                e.printStackTrace();                        }                }        }              //啟動圖片輪換線程        public void startAnim()        {                new Thread(this).start();        }}

MainActivity:當中的LoadingView 就是我們自訂的控制項,在布局中使用時跟其他控制項一樣。

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.drawable.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 present.getMenuInflater().inflate(R.menu.main, menu);return true;}}

布局檔案main.xml 當中使用的wht.android.loading.LoadingView就是前面定義的控制項,完整的包名加上類名。

<?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"    android:gravity="center"    android:orientation="vertical" >    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="65dip"        android:orientation="vertical" >        <wht.android.loading.LoadingView            android:id="@+id/main_imageview"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_horizontal"                        android:src="@drawable/loader_frame_1" >        </wht.android.loading.LoadingView>        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center_horizontal"            android:layout_marginTop="10dip"            android:text="啟動中..."            android:textColor="#666666"            android:textSize="20sp" />    </LinearLayout></LinearLayout>

:http://download.csdn.net/detail/l1976135784/7281483

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.