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