免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2360 | 回复: 1
打印 上一主题 下一主题

Android启动界面实现 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-13 17:28 |只看该作者 |倒序浏览
Android启动界面实现




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

启动界面的效果图:  



启动界面

主要的实现类LoadingView :

Java代码
  1. 1.package wht.android.loading;   
  2. 2.  
  3. 3.import android.content.Context;   
  4. 4.import android.graphics.Canvas;   
  5. 5.import android.util.AttributeSet;   
  6. 6.import android.widget.ImageView;   
  7. 7.  
  8. 8.public class LoadingView extends ImageView implements Runnable   
  9. 9.{   
  10. 10.        private boolean isStop = false;   
  11. 11.           
  12. 12.        private int[] imageIds;   
  13. 13.        private int index = 0;   
  14. 14.        private int length = 1;   
  15. 15.           
  16. 16.        public LoadingView(Context context)   
  17. 17.        {   
  18. 18.                this(context, null);   
  19. 19.        }   
  20. 20.  
  21. 21.        public LoadingView(Context context, AttributeSet attrs)   
  22. 22.        {   
  23. 23.                super(context, attrs);   
  24. 24.        }   
  25. 25.           
  26. 26.        public void setImageIds(int[] imageId)   
  27. 27.        {   
  28. 28.                this.imageIds = imageId;   
  29. 29.                if(imageIds != null && imageIds.length > 0)   
  30. 30.                {   
  31. 31.                        length = imageIds.length;   
  32. 32.                }   
  33. 33.        }   
  34. 34.           
  35. 35.                @Override  
  36. 36.        protected void onDetachedFromWindow()   
  37. 37.        {   
  38. 38.                // TODO Auto-generated method stub   
  39. 39.                super.onDetachedFromWindow();   
  40. 40.                isStop = true;   
  41. 41.        }   
  42. 42.  
  43. 43.        @Override  
  44. 44.        protected void onDraw(Canvas canvas)   
  45. 45.        {   
  46. 46.                // TODO Auto-generated method stub   
  47. 47.                super.onDraw(canvas);   
  48. 48.                if(imageIds != null && imageIds.length > 0)   
  49. 49.                {   
  50. 50.                        this.setImageResource(imageIds[index]);   
  51. 51.                }   
  52. 52.        }   
  53. 53.  
  54. 54.        @Override  
  55. 55.        public void run()   
  56. 56.        {   
  57. 57.                while(!isStop)   
  58. 58.                {   
  59. 59.                        index = ++index % length;   
  60. 60.                        postInvalidate();   
  61. 61.                        try  
  62. 62.                        {   
  63. 63.                                Thread.sleep(400);   
  64. 64.                        }   
  65. 65.                        catch (InterruptedException e)   
  66. 66.                        {   
  67. 67.                                e.printStackTrace();   
  68. 68.                        }   
  69. 69.                }   
  70. 70.        }   
  71. 71.           
  72. 72.        public void startAnim()   
  73. 73.        {   
  74. 74.                new Thread(this).start();   
  75. 75.        }   
  76. 76.  
  77. 77.}  
  78. package wht.android.loading;

  79. import android.content.Context;
  80. import android.graphics.Canvas;
  81. import android.util.AttributeSet;
  82. import android.widget.ImageView;

  83. public class LoadingView extends ImageView implements Runnable
  84. {
  85.         private boolean isStop = false;
  86.         
  87.         private int[] imageIds;
  88.         private int index = 0;
  89.         private int length = 1;
  90.         
  91.         public LoadingView(Context context)
  92.         {
  93.                 this(context, null);
  94.         }

  95.         public LoadingView(Context context, AttributeSet attrs)
  96.         {
  97.                 super(context, attrs);
  98.         }
  99.         
  100.         public void setImageIds(int[] imageId)
  101.         {
  102.                 this.imageIds = imageId;
  103.                 if(imageIds != null && imageIds.length > 0)
  104.                 {
  105.                         length = imageIds.length;
  106.                 }
  107.         }
  108.         
  109.                 @Override
  110.         protected void onDetachedFromWindow()
  111.         {
  112.                 // TODO Auto-generated method stub
  113.                 super.onDetachedFromWindow();
  114.                 isStop = true;
  115.         }

  116.         @Override
  117.         protected void onDraw(Canvas canvas)
  118.         {
  119.                 // TODO Auto-generated method stub
  120.                 super.onDraw(canvas);
  121.                 if(imageIds != null && imageIds.length > 0)
  122.                 {
  123.                         this.setImageResource(imageIds[index]);
  124.                 }
  125.         }

  126.         @Override
  127.         public void run()
  128.         {
  129.                 while(!isStop)
  130.                 {
  131.                         index = ++index % length;
  132.                         postInvalidate();
  133.                         try
  134.                         {
  135.                                 Thread.sleep(400);
  136.                         }
  137.                         catch (InterruptedException e)
  138.                         {
  139.                                 e.printStackTrace();
  140.                         }
  141.                 }
  142.         }
  143.         
  144.         public void startAnim()
  145.         {
  146.                 new Thread(this).start();
  147.         }

  148. }  
复制代码
MainActivity:

Java代码
  1. 1.package wht.android.loading;   
  2. 2.  
  3. 3.import android.app.Activity;   
  4. 4.import android.os.Bundle;   
  5. 5.  
  6. 6.public class MainActivity extends Activity   
  7. 7.{   
  8. 8.      
  9. 9.    private LoadingView main_imageview;   
  10. 10.      
  11. 11.    @Override  
  12. 12.    public void onCreate(Bundle savedInstanceState)   
  13. 13.    {   
  14. 14.        super.onCreate(savedInstanceState);   
  15. 15.        setContentView(R.layout.main);   
  16. 16.        main_imageview = (LoadingView)findViewById(R.id.main_imageview);   
  17. 17.        initLoadingImages();   
  18. 18.           
  19. 19.        new Thread()   
  20. 20.        {   
  21. 21.            @Override  
  22. 22.            public void run()   
  23. 23.            {   
  24. 24.                main_imageview.startAnim();   
  25. 25.            }   
  26. 26.        }.start();   
  27. 27.           
  28. 28.    }   
  29. 29.      
  30. 30.    private void initLoadingImages()   
  31. 31.    {   
  32. 32.        int[] imageIds = new int[6];   
  33. 33.        imageIds[0] = R.drawable.loader_frame_1;   
  34. 34.        imageIds[1] = R.drawable.loader_frame_2;   
  35. 35.        imageIds[2] = R.drawable.loader_frame_3;   
  36. 36.        imageIds[3] = R.drawable.loader_frame_4;   
  37. 37.        imageIds[4] = R.drawable.loader_frame_5;   
  38. 38.        imageIds[5] = R.drawable.loader_frame_6;   
  39. 39.           
  40. 40.        main_imageview.setImageIds(imageIds);   
  41. 41.    }   
  42. 42.}  
  43. package wht.android.loading;

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

  46. public class MainActivity extends Activity
  47. {
  48.    
  49.     private LoadingView main_imageview;
  50.    
  51.     @Override
  52.     public void onCreate(Bundle savedInstanceState)
  53.     {
  54.         super.onCreate(savedInstanceState);
  55.         setContentView(R.layout.main);
  56.         main_imageview = (LoadingView)findViewById(R.id.main_imageview);
  57.         initLoadingImages();
  58.         
  59.         new Thread()
  60.         {
  61.             @Override
  62.             public void run()
  63.             {
  64.                 main_imageview.startAnim();
  65.             }
  66.         }.start();
  67.         
  68.     }
  69.    
  70.     private void initLoadingImages()
  71.     {
  72.         int[] imageIds = new int[6];
  73.         imageIds[0] = R.drawable.loader_frame_1;
  74.         imageIds[1] = R.drawable.loader_frame_2;
  75.         imageIds[2] = R.drawable.loader_frame_3;
  76.         imageIds[3] = R.drawable.loader_frame_4;
  77.         imageIds[4] = R.drawable.loader_frame_5;
  78.         imageIds[5] = R.drawable.loader_frame_6;
  79.         
  80.         main_imageview.setImageIds(imageIds);
  81.     }
  82. }
复制代码
布局文件main.xml

Java代码
  1. 1.<?xml version="1.0" encoding="utf-8"?>   
  2. 2.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3. 3.        android:orientation="vertical" android:layout_width="fill_parent"  
  4. 4.        android:layout_height="fill_parent" android:background="#e1e1e1">   
  5. 5.        <wht.android.loading.LoadingView   
  6. 6.                android:layout_gravity="center_horizontal" android:layout_height="wrap_content"  
  7. 7.                android:id="@+id/main_imageview" android:src="@drawable/loader_frame_1"  
  8. 8.                android:layout_marginTop="190dp" android:layout_width="wrap_content"  
  9. 9.                ></wht.android.loading.LoadingView>   
  10. 10.  
  11. 11.<TextView   
  12. 12.        android:layout_width="wrap_content"  
  13. 13.        android:layout_height="wrap_content"  
  14. 14.        android:text="启动中..."  
  15. 15.        android:layout_marginTop="10dip"  
  16. 16.        android:textColor="#666666"  
  17. 17.        android:layout_gravity="center_horizontal"  
  18. 18.        android:textSize="20sp"  
  19. 19./>   
  20. 20.</LinearLayout>  
  21. <?xml version="1.0" encoding="utf-8"?>
  22. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  23.         android:orientation="vertical" android:layout_width="fill_parent"
  24.         android:layout_height="fill_parent" android:background="#e1e1e1">
  25.         <wht.android.loading.LoadingView
  26.                 android:layout_gravity="center_horizontal" android:layout_height="wrap_content"
  27.                 android:id="@+id/main_imageview" android:src="@drawable/loader_frame_1"
  28.                 android:layout_marginTop="190dp" android:layout_width="wrap_content"
  29.                 ></wht.android.loading.LoadingView>

  30. <TextView
  31.         android:layout_width="wrap_content"
  32.         android:layout_height="wrap_content"
  33.         android:text="启动中..."
  34.         android:layout_marginTop="10dip"
  35.         android:textColor="#666666"
  36.         android:layout_gravity="center_horizontal"
  37.         android:textSize="20sp"
  38. />
  39. </LinearLayout>  
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-11-14 09:46 |只看该作者
ll辛苦了...谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP