Android启动界面实现

阅读更多

转载自:http://www.eoeandroid.com/thread-112229-1-1.html

启动界面的效果图: 

Android启动界面实现_第1张图片
 
启动界面

主要的实现类LoadingView :

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:

package wht.android.loading;

import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity
{
    
    private LoadingView main_imageview;
    
    @Override
    public 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);
    }
}

 

布局文件main.xml 



        


  

  • LoadingView.zip (78.7 KB)
  • 下载次数: 288
  • Android启动界面实现_第2张图片
  • 大小: 11.9 KB
  • 查看图片附件

你可能感兴趣的:(android,启动界面)