转 :  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. while (b < 2) {
  28. try {
  29. if (b == 0) {
  30. Thread.sleep(2000);
  31. b = 1;
  32. } else {
  33. Thread.sleep(50);
  34. }
  35. updateApp();
  36. } catch (InterruptedException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. }
  41. }).start();
  42. mHandler = new Handler() {
  43. @Override
  44. public void handleMessage(Message msg) {
  45. super.handleMessage(msg);
  46. imageview.setAlpha(alpha);
  47. imageview.invalidate();
  48. }
  49. };
  50. }
  51. public void updateApp() {
  52. alpha -= 5;
  53. if (alpha <= 0) {
  54. b = 2;
  55. Intent in = new Intent(this, com.cola.ui.Frm_Addbills.class);
  56. startActivity(in);
  57. }
  58. mHandler.sendMessage(mHandler.obtainMessage());
  59. }
  60. public void initApp(){
  61. }
  62. }

更多相关文章

  1. Android(安卓)应用初始化及窗体事件的分发
  2. 阿里路由框架ARouter简介
  3. Android中ScrollView布局初始化显示的时候使页面置顶
  4. Android(安卓)应用程序 生命周期
  5. 【Android】TV端项目开发挖坑总结
  6. 手把手教你给Eclipse配制Android
  7. Android(安卓)编译,打包、签程名详细教
  8. 让Python在Android系统上飞一会儿
  9. 基于.NET/C#开发跨平台Windows Phone 7、iPhone及Android应用程

随机推荐

  1. Android今日头条UI适配完善版
  2. Android之Binder浅析
  3. Android之NDK开发 环境配置
  4. [置顶] android 进程的优先级
  5. 解决Android的ListView控件滚动时背景变
  6. Android Studio简记
  7. Android接口回调总结,以及运用到弹窗PopWi
  8. [Android实例] 基于android的远程视频监
  9. Android(安卓)Dalvik虚拟机概述
  10. 零碎知识点回顾——常用数据结构