Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android啟動界面

android啟動界面

編輯:關於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;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		main_imageview = (LoadingView) findViewById(R.id.main_imageview);
		initLoadingImages();

		new Thread() {
			@Override
			public 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);
	}
	@Override
	public 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就是前面定義的控件,完整的包名加上類名。




    

        
        

        
    


下載地址:http://download.csdn.net/detail/l1976135784/7281483

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved