轉 : http://hi.baidu.com/android%5Ffans/blog/item/299c0dfa37a60c116c22eb33.html啟動介面的主要功能就是顯示一幅啟動映像,後台進行系統初始化.如果是第一次使用本程式,需要初始化本程式的sqlite資料庫,建庫,建Table,初始化賬目資料.如果不是第一次使用,就進入登記收支記錄介面.
介面效果:
介面很簡單,一個imageview 和一個textview可是如何是2個view 垂直置中顯示,我開始使用linearlayout就沒法完成垂直和橫向置中.後來使用RelativeLayout 才搞定了橫向置中.介面的具體xml如下:main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout android:id="@+id/RelativeLayout01" xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_gravity="center_vertical|center_horizontal"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content">
- <ImageView android:id="@+id/ImageView01"
- android:src="@drawable/logo3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- </ImageView>
- <TextView android:id="@+id/TextView01"
- android:text="@string/welcome"
- android:layout_below="@id/ImageView01"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content">
- </TextView>
- </RelativeLayout>
在這兒我來使用一個小技巧,就是在程式初始化完成後,讓圖片淡出,然後顯示下一個介面.
開始我準備使用一個timer來更新圖片的alpha值,後來程式拋出異常Only the original thread that created a view hierarchy can touch its views.這才發現android 的ui 控制項是安全執行緒的.這裡需要我們在主線程外,再開一個線程更新介面上的圖片.可以使用imageview.invalidate關於如何另開一個線程更新介面的相關代碼如下.
- //給主線程發送訊息更新imageview
- mHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- imageview.setAlpha(alpha);
- imageview.invalidate();
- }
- };
- new Thread(new Runnable() {
- public void run() {
- while (b < 2) {
- try {
- //延時2秒後,每50毫秒更新一次imageview
- if (b == 0) {
- Thread.sleep(2000);
- b = 1;
- } else {
- Thread.sleep(50);
- }
- updateApp();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }).start();
- public void updateApp() {
- alpha -= 5;//每次減少alpha 5
- if (alpha <= 0) {
- b = 2;
- Intent in = new Intent(this, com.cola.ui.Frm_Addbills.class);
- startActivity(in);//啟動下個介面
- }
- mHandler.sendMessage(mHandler.obtainMessage());
- }
通過這段代碼,我們能夠理解android 裡面如何對ui視圖進行更新.
下篇文章我們來看看sqlite的使用.如何初始化程式.
關於handler,invalidate 的用法,
大家還可以參考這篇文章.http://www.blogjava.net/gooogle/archive/2008/03/05/184030.html
附ColaBox.java:
- package com.cola.ui;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.util.Log;
- import android.view.KeyEvent;
- import android.widget.ImageView;
- import android.widget.TextView;
- public class ColaBox extends Activity {
- private Handler mHandler = new Handler();
- ImageView imageview;
- TextView textview;
- int alpha = 255;
- int b = 0;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- imageview = (ImageView) this.findViewById(R.id.ImageView01);
- textview = (TextView) this.findViewById(R.id.TextView01);
- Log.v("ColaBox", "ColaBox start ...");
- imageview.setAlpha(alpha);
- new Thread(new Runnable() {
- public void run() {
- initApp(); //初始化程式
-
- while (b < 2) {
- try {
- if (b == 0) {
- Thread.sleep(2000);
- b = 1;
- } else {
- Thread.sleep(50);
- }
- updateApp();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }).start();
- mHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- super.handleMessage(msg);
- imageview.setAlpha(alpha);
- imageview.invalidate();
- }
- };
- }
- public void updateApp() {
- alpha -= 5;
- if (alpha <= 0) {
- b = 2;
- Intent in = new Intent(this, com.cola.ui.Frm_Addbills.class);
- startActivity(in);
- }
- mHandler.sendMessage(mHandler.obtainMessage());
- }
-
- public void initApp(){
-
- }
- }