Image view is used to display images in android. The code here is from the example in the book. It is easy to use. Now I think android development is really convenient.
The Code is as follows:
Xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<TextView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"/>
<ImageView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: id = "@ + id/image1"
/>
</LinearLayout>
Java code
Package zziss. android. imagetest;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. OS. Message;
Import android. widget. ImageView;
Public class ImageTestActivity extends Activity {
/** Called when the activity is first created .*/
Private ImageView iv;
Private int img_alpha = 0;
Handler iHandler = new Handler ();
Private boolean iIsRun = false;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Iv = (ImageView) this. findViewById (R. id. image1 );
Iv. setImageResource (R. drawable. q2 );
Iv. setAlpha (img_alpha );
IIsRun = true;
New Thread (new Runnable ()
{
@ Override
Public void run (){
// TODO Auto-generated method stub
While (iIsRun)
{
Try
{
Thread. sleep (200 );
UpdateAlpha ();
}
Catch (InterruptedException e)
{
E. printStackTrace ();
}
}
}
} // End new runnable
). Start (); // end new thread
IHandler = new Handler ()
{
@ Override
Public void handleMessage (Message msg ){
// TODO Auto-generated method stub
Super. handleMessage (msg );
Iv. setAlpha (img_alpha );
Iv. invalidate ();
}
};
}
Private void updateAlpha ()
{
If (img_alpha + 7 <= 255)
{
Img_alpha + = 7;
} Else
{
Img_alpha = 255;
IIsRun = false;
}
IHandler. sendMessage (iHandler. obtainMessage ());
}
}