Android 实例

发布时间:2024-11-29 20:11

如何在Android设备上调整屏幕比例适应Kindle阅读? #生活技巧# #数码产品使用技巧# #电子阅读窍门#

启动界面的主要功能就是显示一幅启动图像,后台进行系统初始化.

如果是第一次使用本程序,需要初始化本程序的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(){              } }

网址:Android 实例 https://www.yuejiaxmz.com/news/view/315947

相关内容

Android语音识别技术详解及实例代码
Android养生管理app设计源码案例详解
cordova报错:ANDROID
Android日程管理系统实训报告.docx资源
如何在android中实现阴影(环境光)背景?
android app实现多种语音,基于Android的语音助手APP
揭秘Android人脸识别技术:实时捕捉,安全便捷,如何实现?
基于Android的家庭理财系统的设计与实现
Android家庭收纳APP设计与实现教程
基于Android平台的生活小助手app的设计与实现

随便看看