Android learning: Image view)

Source: Internet
Author: User

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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.