Setting the control value in the android thread is generally used with handler, as follows:
Package com. Yarin. Android. examples_04_15;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. OS. Handler;
Import Android. OS. message;
Import Android. widget. imageview;
Import Android. widget. textview;
Public class activity01 extends Activity
{
// Declare the imageview object
Imageview;
Textview;
// The Alpha value of imageview,
Int image_alpha = 255;
Handler mhandler = new handler ();
// Control thread
Boolean isrung = false;
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate)
{
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );
Isrung = true;
// Obtain the imageview object
Imageview = (imageview) This. findviewbyid (R. Id. imageview01 );
Textview = (textview) This. findviewbyid (R. Id. textview01 );
// Set the image resource of the imageview. Similarly, you can write data as follows in the XML layout.
// Android: src = "@ drawable/logo"
Imageview. setimageresource (R. drawable. logo );
// Set the Alpha value of imageview
Imageview. setalpha (image_alpha );
// Start a thread to reduce the Alpha Value
New thread (New runnable (){
Public void run ()
{
While (isrung)
{
Try
{
Thread. Sleep (200 );
// Update the Alpha Value
Updatealpha ();
// If you use Code An error is reported when you directly set the transparency of the imageview and the value of textview. Because the control cannot be set in the thread, you must use a handler to set the relevant value.
// If (image_alpha-7> = 0)
//{
// Image_alpha-= 7;
//}
// Else
//{
// Image_alpha = 0;
// Isrung = false;
//}
// Imageview. setalpha (image_alpha );
// Textview. settext ("the current alpha value is:" + integer. tostring (image_alpha ));
}
Catch (interruptedexception E)
{
E. printstacktrace ();
}
}
}
}). Start ();
// update the imageview view after receiving the message
mhandler = new handler () {
@ override
Public void handlemessage (Message MSG)
{< br> super. handlemessage (MSG);
imageview. setalpha (image_alpha);
textview. settext ("the current alpha value is:" + integer. tostring (image_alpha);
// update
imageview. invalidate ();
}< BR >};
}< br>
Public void updatealpha ()
{< br> If (image_alpha-7> = 0)
{< br> image_alpha-= 7;
}< br> else
{< br> image_alpha = 0;
isrung = false;
}< br> // send the message to update the imageview
mhandler. sendmessage (mhandler. obtainmessage ();
}< BR >}