Go to restart.
Interface effect:
The interface is very simple. How does one imageview and one textview vertically center two views? I cannot center vertically or horizontally using linearlayout. later, we used relativelayout to solve the problem of horizontal center. the specific XML of the interface is as follows: 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>
Here, I will use a tips: after the program Initialization is complete, let the image fade out and then display the next interface.
At first, I plan to use a timer to update the image's Alpha value. Later, the program throws an exception only the original thread that created a view hierarchy can touch its views. only then can we find that the android UI control is thread-safe. here we need to open another thread to update the image on the Interface outside the main thread. you can use imageview. the code for invalidate on how to open another thread update interface is as follows.
- // Send a message to the main thread to update the 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 {
- // The imageview is updated every 50 milliseconds after a delay of 2 seconds
- If (B = 0 ){
- Thread. Sleep (2000 );
- B = 1;
- } Else {
- Thread. Sleep (50 );
- }
- Updateapp ();
- } Catch (interruptedexception e ){
- E. printstacktrace ();
- }
- }
- }
- }). Start ();
- Public void updateapp (){
- Alpha-= 5; // reduce alpha 5 each time
- If (alpha <= 0 ){
- B = 2;
- Intent in = new intent (this, Com. Cola. UI. frm_addbills.class );
- Startactivity (in); // start the next interface
- }
- Mhandler. sendmessage (mhandler. obtainmessage ());
- }
Through this code, we can understand how to update the UI view in Android.
In the next article, let's take a look at the usage of SQLite and how to initialize the program.
About handler and invalidate usage,
You can also refer to this Article. 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;
- 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 (); // initialization program
- 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 (){
- }
- }