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

本文介绍了一个简单的Android启动界面设计方案,利用RelativeLayout实现视图的垂直和水平居中,并通过Handler和多线程技术实现了图片淡出效果及界面切换。此外还探讨了SQLite数据库的初始化过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

启动界面的主要功能就是显示一幅启动图像,后台进行系统初始化.
如果是第一次使用本程序,需要初始化本程序的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. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值