Android 執行個體-個人財務工具 之一 啟動介面實現

來源:互聯網
上載者:User
轉 : 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. }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.