java.lang.Object | ||
↳ | android.view.View | |
↳ | android.widget.ImageView |
Known Direct Subclasses |
Known Indirect Subclasses |
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.
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
ImageView.ScaleType | Options for scaling the bounds of an image to the bounds of this view. |
XML Attributes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
android:adjustViewBounds | setAdjustViewBounds(boolean) | Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable. | |||||||||
android:baseline | setBaseline(int) | The offset of the baseline within this view. | |||||||||
android:baselineAlignBottom | setBaselineAlignBottom(boolean) | If true, the image view will be baseline aligned with based on its bottom edge. | |||||||||
android:cropToPadding | setCropToPadding(boolean) | If true, the image will be cropped to fit within its padding. | |||||||||
android:maxHeight | setMaxHeight(int) | An optional argument to supply a maximum height for this view. | |||||||||
android:maxWidth | setMaxWidth(int) | An optional argument to supply a maximum width for this view. | |||||||||
android:scaleType | setScaleType(ImageView.ScaleType) | Controls how the image should be resized or moved to match the size of this ImageView. | |||||||||
android:src | setImageResource(int) | Sets a drawable as the content of this ImageView. | |||||||||
android:tint | setImageTintList(ColorStateList) | Set a tinting color for the image. | |||||||||
android:tintMode | setImageTintMode(PorterDuff.Mode) | Blending mode used to apply the image tint. |
[Expand]
Inherited XML Attributes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
android.view.View
|
[Expand]
Inherited Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
android.view.View
|
[Expand]
Inherited Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
android.view.View
|
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
This function is called whenever the view hotspot changes and needs to
be propagated to drawables or child views managed by the view.
| |||||||||||
True when ImageView is adjusting its bounds
to preserve the aspect ratio of its drawable
| |||||||||||
Return the offset of the widget's text baseline from the widget's top boundary. | |||||||||||
Return whether this view's baseline will be considered the bottom of the view.
| |||||||||||
Returns the active color filter for this ImageView.
| |||||||||||
Return whether this ImageView crops to padding.
| |||||||||||
Return the view's drawable, or null if no drawable has been
assigned.
| |||||||||||
Returns the alpha that will be applied to the drawable of this ImageView.
| |||||||||||
Return the view's optional matrix.
| |||||||||||
The maximum height of this view.
| |||||||||||
The maximum width of this view.
| |||||||||||
Return the current scale type in use by this ImageView.
| |||||||||||
Returns whether this View has content which overlaps.
| |||||||||||
Invalidates the specified Drawable.
| |||||||||||
Indicates whether this View is opaque.
| |||||||||||
Call
Drawable.jumpToCurrentState()
on all Drawable objects associated with this view.
| |||||||||||
Generate the new
Drawable state for
this view.
| |||||||||||
Initializes an
AccessibilityEvent with information about
this View which is the event source.
| |||||||||||
Initializes an
AccessibilityNodeInfo with information about this view.
| |||||||||||
Called from
dispatchPopulateAccessibilityEvent(AccessibilityEvent)
giving a chance to this View to populate the accessibility event with its
text content.
| |||||||||||
Called when any RTL property (layout direction or text direction or text alignment) has
been changed.
| |||||||||||
Set this to true if you want the ImageView to adjust its bounds
to preserve the aspect ratio of its drawable.
| |||||||||||
This method was deprecated
in API level 16.
use #setImageAlpha(int) instead
| |||||||||||
Set the offset of the widget's text baseline from the widget's top boundary. | |||||||||||
Set whether to set the baseline of this view to the bottom of the view.
| |||||||||||
Apply an arbitrary colorfilter to the image.
| |||||||||||
Set a tinting option for the image.
| |||||||||||
Set a tinting option for the image.
| |||||||||||
Sets whether this ImageView will crop to padding.
| |||||||||||
Sets the alpha value that should be applied to the image.
| |||||||||||
Sets a Bitmap as the content of this ImageView.
| |||||||||||
Sets a drawable as the content of this ImageView.
| |||||||||||
Sets the image level, when it is constructed from a
LevelListDrawable .
| |||||||||||
Sets a drawable as the content of this ImageView.
| |||||||||||
Applies a tint to the image drawable.
| |||||||||||
Specifies the blending mode used to apply the tint specified by
setImageTintList(ColorStateList) } to the image drawable.
| |||||||||||
Sets the content of this ImageView to the specified Uri.
| |||||||||||
An optional argument to supply a maximum height for this view.
| |||||||||||
An optional argument to supply a maximum width for this view.
| |||||||||||
Controls how the image should be resized or moved to match the size
of this ImageView.
| |||||||||||
Changes the selection state of this view.
| |||||||||||
Set the enabled state of this view.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
This function is called whenever the state of the view changes in such
a way that it impacts the state of drawables being shown.
| |||||||||||
This is called when the view is attached to a window.
| |||||||||||
This is called when the view is detached from a window.
| |||||||||||
Implement this to do your drawing.
| |||||||||||
Measure the view and its content to determine the measured width and the measured height. | |||||||||||
Assign a size and position to this view.
| |||||||||||
If your view subclass is displaying its own Drawable objects, it should
override this function and return true for any Drawable it is
displaying.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
android.view.View
| |||||||||||
From class
java.lang.Object
| |||||||||||
From interface
android.graphics.drawable.Drawable.Callback
| |||||||||||
From interface
android.view.KeyEvent.Callback
| |||||||||||
From interface
android.view.accessibility.AccessibilityEventSource
|
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.
This corresponds to the global attribute
resource symbol adjustViewBounds
.
The offset of the baseline within this view. See {see android.view.View#getBaseline} for details
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).
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.
This corresponds to the global attribute
resource symbol baseline
.
If true, the image view will be baseline aligned with based on its bottom edge.
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.
This corresponds to the global attribute
resource symbol baselineAlignBottom
.
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.
This corresponds to the global attribute
resource symbol cropToPadding
.
An optional argument to supply a maximum height for this view. See {see android.widget.ImageView#setMaxHeight} for details.
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).
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.
This corresponds to the global attribute
resource symbol maxHeight
.
An optional argument to supply a maximum width for this view. See {see android.widget.ImageView#setMaxWidth} for details.
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).
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.
This corresponds to the global attribute
resource symbol maxWidth
.
Controls how the image should be resized or moved to match the size of this ImageView.
Must be one of the following constant values.
Constant | Value | Description |
---|---|---|
matrix | 0 | |
fitXY | 1 | |
fitStart | 2 | |
fitCenter | 3 | |
fitEnd | 4 | |
center | 5 | |
centerCrop | 6 | |
centerInside | 7 |
This corresponds to the global attribute
resource symbol scaleType
.
Sets a drawable as the content of this ImageView.
May be a reference to another resource, in the form "@[+][package:]type:name
"
or to a theme attribute in the form "?[package:][type:]name
".
May be a color value, in the form of "#rgb
", "#argb
",
"#rrggbb
", or "#aarrggbb
".
This corresponds to the global attribute
resource symbol src
.
Set a tinting color for the image. By default, the tint will blend using SRC_ATOP mode.
Must be a color value, in the form of "#rgb
", "#argb
",
"#rrggbb
", or "#aarrggbb
".
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.
This corresponds to the global attribute
resource symbol tint
.
Blending mode used to apply the image tint.
Must be one of the following constant values.
Constant | Value | Description |
---|---|---|
src_over | 3 | The tint is drawn on top of the drawable. [Sa + (1 - Sa)*Da, Rc = Sc + (1 - Sa)*Dc] |
src_in | 5 | The tint is masked by the alpha channel of the drawable. The drawable’s color channels are thrown out. [Sa * Da, Sc * Da] |
src_atop | 9 | The tint is drawn above the drawable, but with the drawable’s alpha channel masking the result. [Da, Sc * Da + (1 - Sa) * Dc] |
multiply | 14 | Multiplies the color and alpha channels of the drawable with those of the tint. [Sa * Da, Sc * Dc] |
screen | 15 | [Sa + Da - Sa * Da, Sc + Dc - Sc * Dc] |
add | 16 | Combines the tint and drawable color and alpha channels, clamping the result to valid color values. Saturate(S + D) |
This corresponds to the global attribute
resource symbol tintMode
.
This function is called whenever the view hotspot changes and needs to be propagated to drawables or child views managed by the view.
Dispatching to child views is handled by
dispatchDrawableHotspotChanged(float, float)
.
Be sure to call through to the superclass when overriding this function.
x | hotspot x coordinate |
---|---|
y | hotspot y coordinate |
True when ImageView is adjusting its bounds to preserve the aspect ratio of its drawable
Return the offset of the widget's text baseline from the widget's top boundary.
Return whether this view's baseline will be considered the bottom of the view.
Returns the active color filter for this ImageView.
Return whether this ImageView crops to padding.
Return the view's drawable, or null if no drawable has been assigned.
Returns the alpha that will be applied to the drawable of this ImageView.
Return the view's optional matrix. This is applied to the view's drawable when it is drawn. If there is no matrix, this method will return an identity matrix. Do not change this matrix in place but make a copy. If you want a different matrix applied to the drawable, be sure to call setImageMatrix().
The maximum height of this view.
The maximum width of this view.
Return the current scale type in use by this ImageView.
Returns whether this View has content which overlaps.
This function, intended to be overridden by specific View types, is an optimization when alpha is set on a view. If rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer and then composited into place, which can be expensive. If the view has no overlapping rendering, the view can draw each primitive with the appropriate alpha value directly. An example of overlapping rendering is a TextView with a background image, such as a Button. An example of non-overlapping rendering is a TextView with no background, or an ImageView with only the foreground image. The default implementation returns true; subclasses should override if they have cases which can be optimized.
The current implementation of the saveLayer and saveLayerAlpha methods in Canvas
necessitates that a View return true if it uses the methods internally without passing the
CLIP_TO_LAYER_SAVE_FLAG
.
Invalidates the specified Drawable.
dr | the drawable to invalidate |
---|
Indicates whether this View is opaque. An opaque View guarantees that it will draw all the pixels overlapping its bounds using a fully opaque color. Subclasses of View should override this method whenever possible to indicate whether an instance is opaque. Opaque Views are treated in a special way by the View hierarchy, possibly allowing it to perform optimizations during invalidate/draw passes.
Call Drawable.jumpToCurrentState()
on all Drawable objects associated with this view.
Also calls jumpToCurrentState()
if there is a StateListAnimator
attached to this view.
Generate the new Drawable
state for
this view. This is called by the view
system when the cached Drawable state is determined to be invalid. To
retrieve the current state, you should use getDrawableState()
.
extraSpace | if non-zero, this is the number of extra entries you would like in the returned array in which you can place your own states. |
---|
Drawable
state of
the view.Initializes an AccessibilityEvent
with information about
this View which is the event source. In other words, the source of
an accessibility event is the view whose state change triggered firing
the event.
Example: Setting the password property of an event in addition to properties set by the super implementation:
public void onInitializeAccessibilityEvent(AccessibilityEvent event) { super.onInitializeAccessibilityEvent(event); event.setPassword(true); }
If an View.AccessibilityDelegate
has been specified via calling
setAccessibilityDelegate(AccessibilityDelegate)
its
onInitializeAccessibilityEvent(View, AccessibilityEvent)
is responsible for handling this call.
Note: Always call the super implementation before adding information to the event, in case the default implementation has basic information to add.
event | The event to initialize. |
---|
Initializes an AccessibilityNodeInfo
with information about this view.
The base implementation sets:
setParent(View)
,setBoundsInParent(Rect)
,setBoundsInScreen(Rect)
,setPackageName(CharSequence)
,setClassName(CharSequence)
,setContentDescription(CharSequence)
,setEnabled(boolean)
,setClickable(boolean)
,setFocusable(boolean)
,setFocused(boolean)
,setLongClickable(boolean)
,setSelected(boolean)
,Subclasses should override this method, call the super implementation, and set additional attributes.
If an View.AccessibilityDelegate
has been specified via calling
setAccessibilityDelegate(AccessibilityDelegate)
its
onInitializeAccessibilityNodeInfo(View, AccessibilityNodeInfo)
is responsible for handling this call.
info | The instance to initialize. |
---|
Called from dispatchPopulateAccessibilityEvent(AccessibilityEvent)
giving a chance to this View to populate the accessibility event with its
text content. While this method is free to modify event
attributes other than text content, doing so should normally be performed in
onInitializeAccessibilityEvent(AccessibilityEvent)
.
Example: Adding formatted date string to an accessibility event in addition to the text added by the super implementation:
public void onPopulateAccessibilityEvent(AccessibilityEvent event) { super.onPopulateAccessibilityEvent(event); final int flags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY; String selectedDateUtterance = DateUtils.formatDateTime(mContext, mCurrentDate.getTimeInMillis(), flags); event.getText().add(selectedDateUtterance); }
If an View.AccessibilityDelegate
has been specified via calling
setAccessibilityDelegate(AccessibilityDelegate)
its
onPopulateAccessibilityEvent(View, AccessibilityEvent)
is responsible for handling this call.
Note: Always call the super implementation before adding information to the event, in case the default implementation has basic information to add.
event | The accessibility event which to populate. |
---|
Called when any RTL property (layout direction or text direction or text alignment) has been changed. Subclasses need to override this method to take care of cached information that depends on the resolved layout direction, or to inform child views that inherit their layout direction. The default implementation does nothing.
layoutDirection | the direction of the layout |
---|
Set this to true if you want the ImageView to adjust its bounds to preserve the aspect ratio of its drawable.
Note: If the application targets API level 17 or lower,
adjustViewBounds will allow the drawable to shrink the view bounds, but not grow
to fill available measured space in all cases. This is for compatibility with
legacy MeasureSpec
and
RelativeLayout
behavior.
adjustViewBounds | Whether to adjust the bounds of this view to preserve the original aspect ratio of the drawable. |
---|
This method was deprecated
in API level 16.
use #setImageAlpha(int) instead
Sets the alpha value that should be applied to the image.
alpha | the alpha value that should be applied to the image |
---|
Set the offset of the widget's text baseline from the widget's top
boundary. This value is overridden by the setBaselineAlignBottom(boolean)
property.
baseline | The baseline to use, or -1 if none is to be provided. |
---|
Set whether to set the baseline of this view to the bottom of the view. Setting this value overrides any calls to setBaseline.
aligned | If true, the image view will be baseline aligned with based on its bottom edge. |
---|
Apply an arbitrary colorfilter to the image.
cf | the colorfilter to apply (may be null) |
---|
Set a tinting option for the image. Assumes
SRC_ATOP
blending mode.
color | Color tint to apply. |
---|
Set a tinting option for the image.
color | Color tint to apply. |
---|---|
mode | How to apply the color. The standard mode is
SRC_ATOP |
Sets whether this ImageView will crop to padding.
cropToPadding | whether this ImageView will crop to padding |
---|
Sets the alpha value that should be applied to the image.
alpha | the alpha value that should be applied to the image |
---|
Sets a Bitmap as the content of this ImageView.
bm | The bitmap to set |
---|
Sets a drawable as the content of this ImageView.
drawable | The drawable to set |
---|
Sets the image level, when it is constructed from a
LevelListDrawable
.
level | The new level for the image. |
---|
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.
resId | the resource identifier of the drawable |
---|
Applies a tint to the image drawable. Does not modify the current tint
mode, which is SRC_IN
by default.
Subsequent calls to setImageDrawable(Drawable)
will automatically
mutate the drawable and apply the specified tint and tint mode using
setTintList(ColorStateList)
.
tint | the tint to apply, may be null to clear tint |
---|
Specifies the blending mode used to apply the tint specified by
setImageTintList(ColorStateList)
} to the image drawable. The default
mode is SRC_IN
.
tintMode | the blending mode used to apply the tint, may be
null to clear tint |
---|
Sets the content of this ImageView to the specified Uri.
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.
uri | The Uri of an image |
---|
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.
maxHeight | maximum height for this view |
---|
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.
maxWidth | maximum width for this view |
---|
Controls how the image should be resized or moved to match the size of this ImageView.
scaleType | The desired scaling mode. |
---|
Changes the selection state of this view. A view can be selected or not. Note that selection is not the same as focus. Views are typically selected in the context of an AdapterView like ListView or GridView; the selected view is the view that is highlighted.
selected | true if the view must be selected, false otherwise |
---|
This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown.
If the View has a StateListAnimator, it will also be called to run necessary state change animations.
Be sure to call through to the superclass when overriding this function.
This is called when the view is attached to a window. At this point it
has a Surface and will start drawing. Note that this function is
guaranteed to be called before onDraw(android.graphics.Canvas)
,
however it may be called any time before the first onDraw -- including
before or after onMeasure(int, int)
.
This is called when the view is detached from a window. At this point it no longer has a surface for drawing.
Implement this to do your drawing.
canvas | the canvas on which the background will be drawn |
---|
Measure the view and its content to determine the measured width and the
measured height. This method is invoked by measure(int, int)
and
should be overriden by subclasses to provide accurate and efficient
measurement of their contents.
CONTRACT: When overriding this method, you
must call setMeasuredDimension(int, int)
to store the
measured width and height of this view. Failure to do so will trigger an
IllegalStateException
, thrown by
measure(int, int)
. Calling the superclass'
onMeasure(int, int)
is a valid use.
The base class implementation of measure defaults to the background size,
unless a larger size is allowed by the MeasureSpec. Subclasses should
override onMeasure(int, int)
to provide better measurements of
their content.
If this method is overridden, it is the subclass's responsibility to make
sure the measured height and width are at least the view's minimum height
and width (getSuggestedMinimumHeight()
and
getSuggestedMinimumWidth()
).
widthMeasureSpec | horizontal space requirements as imposed by the parent.
The requirements are encoded with
View.MeasureSpec . |
---|---|
heightMeasureSpec | vertical space requirements as imposed by the parent.
The requirements are encoded with
View.MeasureSpec . |
Assign a size and position to this view. This is called from layout.
l | Left position, relative to parent |
---|---|
t | Top position, relative to parent |
r | Right position, relative to parent |
b | Bottom position, relative to parent |
If your view subclass is displaying its own Drawable objects, it should override this function and return true for any Drawable it is displaying. This allows animations for those drawables to be scheduled.
Be sure to call through to the super class when overriding this function.
dr | The Drawable to verify. Return true if it is one you are displaying, else return the result of calling through to the super class. |
---|