Pages

Wednesday, September 19, 2012

ImageView attributes & methods

ImageView

Displays an arbitrary image, such as an icon. The ImageView class can load images from various sources (such as resources or content providers), takes care of computing its measurement from the image so that it can be used in any layout manager, and provides various display options such as scaling and tinting.


XML Attributes

android:adjustViewBounds

Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable. 
Must be a boolean value, either "true" or "false".
This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:]type:name" containing a value of this type. 

android:cropToPadding

If true, the image will be cropped to fit within its padding.  
Must be a boolean value, either "true" or "false".
This may also be a reference to a resource (in the form "@[package:]type:name") or theme attribute (in the form "?[package:]type:name" containing a value of this type. 


android:maxHeight

An optional argument to supply a maximum height for this view. 

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters). 


android:maxWidth

An optional argument to supply a maximum width for this view.

Must be a dimension value, which is a floating point number appended with a unit such as "14.5sp". Available units are: px (pixels), dp (density-independent pixels), sp (scaled pixels based on preferred font size), in (inches), mm (millimeters).


android:src

Sets a drawable as the content of this ImageView.
May beme attribute in the form "?[package:]type:name".
May be a color value, in the form of "#rgb", "#argb","#rrggbb","#aarrggbb".



Methods 

public void setAdjustViewBounds(boolean adjustViewBounds)

Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.
Parameters
adjustViewBounds   Whether to adjust the bounds of this view to presrve the original aspect ratio of the drawable

public void setCropToPadding(boolean cropToPadding)

Sets whether this ImageView will crop to padding.
Parameters
cropToPadding whether this ImageView will crop to padding

public void setImageBitmap(Bitmap bm)

Sets a Bitmap as the content of this ImageView.
Parameters
bm The bitmap to set 

public void setImageDrawble(Drawble drawble)


Sets a drawable as the content of this ImageView. 
Parameters
drawable The drawable to set







public void setMaxHeight(int maxHeight)


An optional argument to supply a maximum height for this view. Only valid if setAdjustViewBounds(boolean) has been set to true. To set an image to be a maximum of 100 x 100 while preserving the original aspect ratio, do the following: 1) set adjustViewBounds to true 2) set maxWidth and maxHeight to 100 3) set the height and width layout params to WRAP_CONTENT.

Note that this view could be still smaller than 100 x 100 using this approach if the original image is small. To set an image to a fixed size, specify that size in the layout params and then use setScaleType(android.widget.ImageView.ScaleType) to determine how to fit the image within the bounds.

Parameters
maxHeight maximum height for this view

public void setMaxWidth(int maxWidth)


An optional argument to supply a maximum width for this view. Only valid if setAdjustViewBounds(boolean) has been set to true. To set an image to be a maximum of 100 x 100 while preserving the original aspect ratio, do the following: 1) set adjustViewBounds to true 2) set maxWidth and maxHeight to 100 3) set the height and width layout params to WRAP_CONTENT.

Note that this view could be still smaller than 100 x 100 using this approach if the original image is small. To set an image to a fixed size, specify that size in the layout params and then use setScaleType(android.widget.ImageView.ScaleType) to determine how to fit the image within the bounds.
Parameters
maxWidth maximum width for this view

public void setImageResource(int resid)


Sets a drawable as the content of this ImageView.

This does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup. If that's a concern, consider using setImageDrawable(android.graphics.drawable.Drawable) or setImageBitmap(android.graphics.Bitmap) and BitmapFactory instead.
Parameters
resId  the resource identifier of the the drawable










3 comments:

  1. I know this is one of the most meaningful information for me. And I'm animated reading your article. But should remark on some general things, the website style is perfect; the articles are great. Thanks for the ton of tangible and attainable help.
    Check here

    ReplyDelete