To display an image on the screen, you must first create an image display object. In Android, this object is an imageview object, and then use setimageresource
Method To set the image resource index to be displayed. You can also perform other operations on the image, such as setting its alpha value. Here, we will use an example to analyze the usage of imageview.
In this example, a thread is used to continuously update the Alpha value of the image.
Mainactivity. Java
Package COM. example. examples_04_15; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. app. activity; import android. view. menu; import android. widget. imageview; import android. widget. textview; public class mainactivity extends activity {imageview; textview; int image_alpha = 255; handler mhandler = new handler (); Boolean isrun = false; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); isrun = true; imageview = (imageview) findviewbyid (R. id. imageview01); textview = (textview) findviewbyid (R. id. textview01); imageview. setimageresource (R. drawable. log); imageview. setalpha (image_alpha); // start a thread to reduce the Alpha value by new thread (New runnable () {@ overridepublic void run () {// todo auto-generated method stubwhile (isrun) {try {thread. sleep (200); updatealpha ();} catch (interruptedexception e) {e. printstacktrace ();}}}}). start (); // update the imageview view mhandler = new handler () {@ overridepublic void handlemessage (Message MSG) {// todo auto-generated method stubsuper. handlemessage (MSG); imageview. setalpha (image_alpha); textview. settext ("the current alpha value is:" + integer. tostring (image_alpha); imageview. invalidate () ;}};} public void updatealpha () {If (image_alpha-7> = 0) {image_alpha-= 7 ;}else {image_alpha = 0; isrun = false;} mhandler. sendmessage (mhandler. obtainmessage ());}}
Activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ImageView android:id="@+id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/TextView01" android:layout_below="@id/ImageView01" android:layout_width="wrap_content" android:layout_height="wrap_content" /></RelativeLayout>