Pro Android learning notes (16): user interface and control (4): imageview Control

Source: Internet
Author: User

Imageview is a basic control that inherits Android. widget. imageview.

XML snippets

<Linearlayout Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: Orientation = "horizontal">
<! -- Specify the resource id: @ drawable/xxxxx -->
<ImageViewAndroid: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: Id = "@ + ID/ui_image1"
Android: src = "@ drawable/ic_launcher"/>

<! -- Display color block -->
<Imageview Android: layout_width = "125dip"
Android: layout_height = "25dip"
Android: id = "@ + id/ui_image2"
Android: src = "#555555"
Android: contentDescription = "set pure color"/>
</LinearLayout>

<LinearLayout android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: orientation = "horizontal">
<! -- The three images are from the same 128x128 png icon. the length and width of the first two images are smaller than those of the source image. The system uses the proportional reduction method to adapt to the specified size. -->
<ImageView android: layout_width = "25dip"
Android: layout_height = "25dip"
Android: src = "@ drawable/png0441"/>
<ImageView android: layout_width = "48dip"
Android: layout_height = "48dip"
Android: src = "@ drawable/png0441"/>
<ImageView android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/png0441"/>
</LinearLayout>
<! -- For the following figure, we set the image shrinking method, fitXY, to fill up the entire size. In addition, I tried to set layout_width and layout_height to wrap_content, and set maxWidth and maxHeight. However, I found that maxWidth/Height does not work and the original image size is still displayed, this is different from what is mentioned in Pro Android 4.0. Use this function with caution.
-->

<ImageView android: layout_width = "60dip"
Android: layout_height = "30dip"
Android: src = "@ drawable/png0441"
Android: scaleType = "fitXY"/>
<! -- Android: src is not set here, but an ID number is provided for setting in the Code. --> 
<ImageView android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: id = "@ + id/ui_image3"/>

Code to set ImageView

In XML, ui_image3 does not set the specific src. You can set it in the Code through several methods.

Method 1: Set the resource ID

ImageView image = (ImageView) findViewById (R. id. ui_image3 );
Image.SetImageResource (R. drawable. ic_launcher);

Method 2: Use Bitmap

ImageView image = (ImageView) findViewById (R. id. ui_image3 );
Bitmap bm = BitmapFactory. decodeResource (this. getResources (), R.drawable.png 02 );
// Here you can add the Bitmap Processing code ......
Image.SetImageBitmap (bm );

Method 3: File

For the simulator, we place the image file in a certain position of the file system by using the adb push command line, such as sdcard, as shown in:

ImageView image = (ImageView) findViewById (R. id. ui_image3 );
Try {
String filename = Environment. getExternalStorageDirectory () + "/wei/sunflower.jpg ";
Image.SetImageDrawable (Drawable. createFromPath (filename ));
} Catch (Exception e ){
Log. e ("wei", e. toString ());
}

Method 4: Uri

ImageView image = (ImageView) findViewById (R. id. ui_image3 );
Image.SetImageURI (Uri. parse ("file: // mnt/sdcard/wei/logo.jpg "));
// Only local storage is supported

Note that the URI method is limited to local storage but not remote storage. If you set a web URI, the system reports the following error:

Others

If you want the image to come from remote, you can use BitmapFactory. decodeStream (InputStream is) and put Bitmap into ImageView.

Related links:
My Android development articles

Related Article

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.