Android 实例-个人理财工具 之一 启动界面实现

来源:互联网 发布:注销的淘宝号还能用吗 编辑:程序博客网 时间:2024/04/29 02:58
转 : http://hi.baidu.com/android%5Ffans/blog/item/299c0dfa37a60c116c22eb33.html
启动界面的主要功能就是显示一幅启动图像,后台进行系统初始化.
如果是第一次使用本程序,需要初始化本程序的sqlite数据库,建库,建Table,初始化账目数据.
如果不是第一次使用,就进入登记收支记录界面.

界面效果如图:

界面很简单,一个imageview 和一个textview
可是如何是2个view 垂直居中显示,我开始使用linearlayout就没法完成垂直和横向居中.
后来使用RelativeLayout 才搞定了横向居中.
界面的具体xml如下:
main.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout android:id="@+id/RelativeLayout01" xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_gravity="center_vertical|center_horizontal"
  4. android:layout_height="wrap_content"
  5. android:layout_width="wrap_content">
  6. <ImageView android:id="@+id/ImageView01"
  7. android:src="@drawable/logo3"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content">
  10. </ImageView>
  11. <TextView android:id="@+id/TextView01"
  12. android:text="@string/welcome"
  13. android:layout_below="@id/ImageView01"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content">
  16. </TextView>
  17. </RelativeLayout>
在这儿我来使用一个小技巧,就是在程序初始化完成后,让图片淡出,然后显示下一个界面.
开始我准备使用一个timer来更新图片的alpha值,后来程序抛出异常Only the original thread that created a view hierarchy can touch its views.
这才发现android 的ui 控件是线程安全的.
这里需要我们在主线程外,再开一个线程更新界面上的图片.可以使用imageview.invalidate
关于如何另开一个线程更新界面的相关代码如下.
  1. //给主线程发送消息更新imageview
  2. mHandler = new Handler() {
  3. @Override
  4. public void handleMessage(Message msg) {
  5. super.handleMessage(msg);
  6. imageview.setAlpha(alpha);
  7. imageview.invalidate();
  8. }
  9. };
  10. new Thread(new Runnable() {
  11. public void run() {
  12. while (b < 2) {
  13. try {
  14. //延时2秒后,每50毫秒更新一次imageview
  15. if (b == 0) {
  16. Thread.sleep(2000);
  17. b = 1;
  18. } else {
  19. Thread.sleep(50);
  20. }
  21. updateApp();
  22. } catch (InterruptedException e) {
  23. e.printStackTrace();
  24. }
  25. }
  26. }
  27. }).start();
  28. public void updateApp() {
  29. alpha -= 5;//每次减少alpha 5
  30. if (alpha <= 0) {
  31. b = 2;
  32. Intent in = new Intent(this, com.cola.ui.Frm_Addbills.class);
  33. startActivity(in);//启动下个界面
  34. }
  35. mHandler.sendMessage(mHandler.obtainMessage());
  36. }

 

通过这段代码,我们能够理解android 里面如何对ui视图进行更新.

下篇文章我们来看看sqlite的使用.如何初始化程序.

关于handler,invalidate 的用法,

大家还可以参考这篇文章.http://www.blogjava.net/gooogle/archive/2008/03/05/184030.html

 

附ColaBox.java:
  1. package com.cola.ui;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.os.Handler;
  6. import android.os.Message;
  7. import android.util.Log;
  8. import android.view.KeyEvent;
  9. import android.widget.ImageView;
  10. import android.widget.TextView;
  11. public class ColaBox extends Activity {
  12.     private Handler mHandler = new Handler();
  13.      ImageView imageview;
  14.      TextView textview;
  15.     int alpha = 255;
  16.     int b = 0;
  17.     public void onCreate(Bundle savedInstanceState) {
  18.         super.onCreate(savedInstanceState);
  19.          setContentView(R.layout.main);
  20.          imageview = (ImageView) this.findViewById(R.id.ImageView01);
  21.          textview = (TextView) this.findViewById(R.id.TextView01);
  22.          Log.v("ColaBox", "ColaBox start ...");
  23.          imageview.setAlpha(alpha);
  24.         new Thread(new Runnable() {
  25.             public void run() {
  26.                  initApp(); //初始化程序
  27.                 
  28.                 while (b < 2) {
  29.                     try {
  30.                         if (b == 0) {
  31.                              Thread.sleep(2000);
  32.                              b = 1;
  33.                          } else {
  34.                              Thread.sleep(50);
  35.                          }
  36.                          updateApp();
  37.                      } catch (InterruptedException e) {
  38.                          e.printStackTrace();
  39.                      }
  40.                  }
  41.              }
  42.          }).start();
  43.          mHandler = new Handler() {
  44.             @Override
  45.             public void handleMessage(Message msg) {
  46.                 super.handleMessage(msg);
  47.                  imageview.setAlpha(alpha);
  48.                  imageview.invalidate();
  49.              }
  50.          };
  51.      }
  52.     public void updateApp() {
  53.          alpha -= 5;
  54.         if (alpha <= 0) {
  55.              b = 2;
  56.              Intent in = new Intent(this, com.cola.ui.Frm_Addbills.class);
  57.              startActivity(in);
  58.          }
  59.          mHandler.sendMessage(mHandler.obtainMessage());
  60.      }
  61.     
  62.     public void initApp(){
  63.         
  64.      }
  65. }